1. 程式人生 > >mysql 多實例部署

mysql 多實例部署

b-s con cas cal pin 文件 file function mysq

Centos7.6 部署3個Mariadb 實例
[[email protected] ~]# yum install mariadb-server -y

# 創建對應的目錄文件
[[email protected] ~]# mkdir /mysql/{3306,3307,3308}/{data,etc,socket,log,bin,pid} -p

chown -R mysql.mysql /mysql

# 初始化數據庫文件
[[email protected] ~]# mysql_install_db --datadir=/mysql/3306/data/ --user=mysql
[[email protected] ~]# mysql_install_db --datadir=/mysql/3307/data/ --user=mysql
[[email protected] ~]# mysql_install_db --datadir=/mysql/3308/data/ --user=mysql

# 創建對應配置文件
[[email protected] ~]# cp /etc/my.cnf /mysql/3306/etc
# 修改對應的配置文件
[mysqld]
port=3306
datadir=/mysql/3306/data
socket=/mysql/3306/socket/mysql.sock
symbolic-links=0

[mysqld_safe]
log-error=/mysql/3306/log/mariadb.log
pid-file=/mysql/3306/pid/mariadb.pid

# 另外的2個實例調整對應的端口即可

# 準備啟動腳本 另外的實例修改對應的端口即可
[[email protected] ~]#  vim /mysql/3306/bin/mysqld
#!/bin/bash
#chkconfig: 345 80 2
port=3306
mysql_user="root"
mysql_pwd=""
cmd_path="/usr/bin"
mysql_basedir="/mysql"
mysql_sock="${mysql_basedir}/${port}/socket/mysql.sock"

function_start_mysql()
{
    if [ ! -e "$mysql_sock" ];then
      printf "Starting MySQL...\n"
      ${cmd_path}/mysqld_safe --defaults-file=${mysql_basedir}/${port}/etc/my.cnf  &> /dev/null  &
    else
      printf "MySQL is running...\n"
      exit
    fi
}

function_stop_mysql()
{
    if [ ! -e "$mysql_sock" ];then
       printf "MySQL is stopped...\n"
       exit
    else
       printf "Stoping MySQL...\n"
       ${cmd_path}/mysqladmin -u ${mysql_user} -p${mysql_pwd} -S ${mysql_sock} shutdown
   fi
}

function_restart_mysql()
{
    printf "Restarting MySQL...\n"
    function_stop_mysql
    sleep 2
    function_start_mysql
}

case $1 in
start)
    function_start_mysql
;;
stop)
    function_stop_mysql
;;
restart)
    function_restart_mysql
;;
*)
    printf "Usage: ${mysql_basedir}/${port}/bin/mysqld {start|stop|restart}\n"
esac

# 啟動服務
[[email protected] ~]# /mysql/3306/bin/mysqld start
Starting MySQL...
[[email protected] ~]# /mysql/3307/bin/mysqld start
Starting MySQL...
[[email protected] ~]# /mysql/3308/bin/mysqld start
Starting MySQL...

# 連接測試
mysql -S /mysql/3308/socket/mysql.sock -e ‘select version()‘
+----------------+
| version()      |
+----------------+
| 5.5.60-MariaDB |
+----------------+

# 關閉數據庫
[[email protected] ~]# /mysql/3306/bin/mysqld stop
Stoping MySQL...
Enter password:

mysql 多實例部署