1. 程式人生 > 資料庫 >單機啟動兩個 mysql 並設定主備

單機啟動兩個 mysql 並設定主備

參考

https://www.jianshu.com/p/c994d13753ab
https://dev.mysql.com/doc/refman/8.0/en/mysqld-multi.html

下載

cd /home/shicai.xsc
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
tar -xvf mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
mv /home/shicai.xsc/mysql-5.7.18-linux-glibc2.5-x86_64 /home/shicai.xsc/mysql

建立目錄

mkdir -p /home/shicai.xsc/mysql/data
mkdir /home/shicai.xsc/mysql/data/mysql1
mkdir /home/shicai.xsc/mysql/data/mysql2
mkdir -p /home/shicai.xsc/mysql/data/log
mkdir -p /home/shicai.xsc/mysql/data/log/mysql1
mkdir -p /home/shicai.xsc/mysql/data/log/mysql2

建立 mysql 使用者和組

groupadd -r mysql
useradd -r -g mysql mysql

直接用 shicai.xsc 執行,不用建立 mysql 賬戶

授權

chown -R shicai.xsc /home/shicai.xsc/mysql

新增環境變數

echo ‘export PATH=$PATH:/home/shicai.xsc/mysql/bin’ >> /home/shicai.xsc/.profile
source /home/shicai.xsc/.profile

新增 /home/shicai.xsc/mysql/my.cnf

[client]
port=3306
socket=/home/shicai.xsc/mysql/mysql1.sock
[mysqld_multi]
mysqld = /home/shicai.xsc/mysql/bin/mysqld_safe
mysqladmin = /home/shicai.xsc/mysql/bin/mysqladmin
user=shicai.xsc
log = /home/shicai.xsc/mysql/data/log/mysqld_multi.log
[mysqld]
user=shicai.xsc
basedir =/home/shicai.xsc/mysql
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
[mysqld1]
server_id = 1
#skip-grant-tables=1
log_bin = mysql_bin
basedir =/home/shicai.xsc/mysql
user=shicai.xsc
mysqld=mysqld
mysqladmin=mysqladmin
datadir=/home/shicai.xsc/mysql/data/mysql1
port=3306
socket=/home/shicai.xsc/mysql/mysql1.sock
log-output=file
innodb_data_file_path=ibdata1:12M;ibdata2:10M:autoextend
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /home/shicai.xsc/mysql/data/log/mysql1/slow.log
log-error = /home/shicai.xsc/mysql/data/log/mysql1/error.log
[mysqld2]
server_id = 2
log_bin = mysql_bin
relay_log = mysql-relay-bin
user=shicai.xsc
#skip-grant-tables=1
basedir =/home/shicai.xsc/mysql
mysqld=mysqld
mysqladmin=mysqladmin
datadir=/home/shicai.xsc/mysql/data/mysql2
port=3307
innodb_data_file_path=ibdata1:12M;ibdata2:10M:autoextend
socket=/home/shicai.xsc/mysql/mysql2.sock
log-output=file
slow_query_log = 1
long_query_time = 1
slow_query_log_file = /home/shicai.xsc/mysql/data/log/mysql2/slow.log
log-error = /home/shicai.xsc/mysql/data/log/mysql2/error.log

初始化資料庫

以下步驟必須按順序執行
cd /home/shicai.xsc/mysql

bin/mysqld --initialize --user=shicai.xsc --basedir=/home/shicai.xsc/mysql --datadir=/home/shicai.xsc/mysql/data/mysql1

bin/mysqld --initialize --user=shicai.xsc --basedir=/home/shicai.xsc/mysql --datadir=/home/shicai.xsc/mysql/data/mysql2

bin/mysql_ssl_rsa_setup --basedir=/home/shicai.xsc/mysql --datadir=/home/shicai.xsc/mysql/data/mysql1 --defaults-file=/home/shicai.xsc/mysql/my.cnf

bin/mysql_ssl_rsa_setup --basedir=/home/shicai.xsc/mysql --datadir=/home/shicai.xsc/mysql/data/mysql2 --defaults-file=/home/shicai.xsc/mysql/my.cnf

啟動

/home/shicai.xsc/mysql/bin/mysqld_multi --defaults-file=/home/shicai.xsc/mysql/my.cnf start

檢視狀態

/home/shicai.xsc/mysql/bin/mysqld_multi --defaults-file=/home/shicai.xsc/mysql/my.cnf report
返回:
Reporting MySQL servers
MySQL server from group: mysqld1 is running
MySQL server from group: mysqld2 is running

修改 root 初始密碼

/home/shicai.xsc/mysql/bin/mysqld_multi --defaults-file=/home/shicai.xsc/mysql/my.cnf stop

修改 /home/shicai.xsc/mysql/my.cnf

[mysqld1]
skip-grant-tables=1
[mysqld2]
skip-grant-tables=1

/home/shicai.xsc/mysql/bin/mysqld_multi --defaults-file=/home/shicai.xsc/mysql/my.cnf start

mysql -h127.0.0.1 -uroot -P3306

update mysql.user set authentication_string=PASSWORD(‘123456’) where User=‘root’;
flush privileges;

mysql -h127.0.0.1 -uroot -P3307
update mysql.user set authentication_string=PASSWORD(‘123456’) where User=‘root’;
flush privileges;

然後再從 /home/shicai.xsc/mysql/my.cnf 去掉 skip-grant-tables 並重啟 mysql。

mysql -h127.0.0.1 -uroot -P3306 -p123456
mysql -h127.0.0.1 -uroot -P3307 -p123456

排錯

如果遇到 /home/shicai.xsc/mysql/data/log/mysql2/error.log 一直報錯:
2020-11-25T14:10:54.791228Z 0 [Note] InnoDB: Check that you do not already have another mysqld process using the same InnoDB data or log files.
2020-11-25T14:10:55.791311Z 0 [ERROR] InnoDB: Unable to lock ./ibdata1 error: 11
則:ps aux | grep mysql* 並殺死所有程序,然後重啟 mysql。

如果先修改 my.cnf 再呼叫 /home/shicai.xsc/mysql/bin/mysqld_multi --defaults-file=/home/shicai.xsc/mysql/my.cnf stop,則停止沒有生效,每次 /home/shicai.xsc/mysql/bin/mysqld_multi --defaults-file=/home/shicai.xsc/mysql/my.cnf start 都會碰到此問題。需要先停止再修改 my.cnf。

如果遇到 /home/shicai.xsc/mysql/data/log/mysql2/error.log 一直報錯:
Could not open or create the system tablespace. If you tried to add new data files to the system tablespace, and it failed here, you should now edit innodb_data_file_path in my.cnf back to what it was, and remove the new ibdata files InnoDB created in this failed attempt. InnoDB only wrote those files full of zeros, but did not yet use them in any way. But be careful: do not remove old data files which contain your precious data!

則:修改 /home/shicai.xsc/mysql/my.cnf 並重啟 mysql

[mysqld2]
innodb_data_file_path=ibdata1:12M;ibdata2:10M:autoextend 

設定主從同步

參考:http://www.tianshouzhi.com/api/tutorials/mysql/363

  • 主從庫都建立使用者
    GRANT REPLICATION SLAVE, REPLICATION CLIENT ON . TO slave@‘localhost’ IDENTIFIED BY ‘slave’;

  • 檢驗 server_id 是否生效
    SHOW GLOBAL VARIABLES like ‘server_id’;
    返回值要符合 my.cnf 的配置,否則無法設定主從

  • 在叢庫執行
    CHANGE MASTER TO
    MASTER_HOST=‘localhost’,
    MASTER_USER=‘slave’,
    MASTER_PORT=3306,
    MASTER_PASSWORD=‘slave’,
    MASTER_LOG_FILE=‘mysql_bin.000001’,
    MASTER_LOG_POS=0;

  • 在從庫驗證主庫 binlog 資訊
    mysql> show master status \G;
    *************************** 1. row ***************************
    File: mysql_bin.000002
    Position: 154
    Binlog_Do_DB:
    Binlog_Ignore_DB:
    Executed_Gtid_Set:
    1 row in set (0.00 sec)

  • 在叢庫驗證叢庫同步狀態
    mysql> show slave status\G;
    返回:Slave_IO_Running 和 Slave_SQL_Running 都為 0,說明叢庫同步的關鍵兩個執行緒 IO 和 SQL 都沒有啟動。
    *************************** 1. row ***************************
    Slave_IO_State:
    Master_Host: localhost
    Master_User: slave
    Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: mysql-bin.000001
    Read_Master_Log_Pos: 4
    Relay_Log_File: mysql-relay-bin.000001
    Relay_Log_Pos: 4
    Relay_Master_Log_File: mysql-bin.000001
    Slave_IO_Running: No
    Slave_SQL_Running: No

  • 叢庫執行 start slave;

  • 再叢庫驗證叢庫同步狀態
    mysql> show slave status\G;
    發現 Slave_SQL_Running 執行緒起來了,但是 Slave_IO_Running 沒有
    *************************** 1. row ***************************
    Slave_IO_State:
    Master_Host: localhost
    Master_User: slave
    Master_Port: 3306
    Connect_Retry: 60
    Master_Log_File: mysql-bin.000001
    Read_Master_Log_Pos: 4
    Relay_Log_File: mysql-relay-bin.000001
    Relay_Log_Pos: 4
    Relay_Master_Log_File: mysql-bin.000001
    Slave_IO_Running: No
    Slave_SQL_Running: Yes
    Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: ‘Could not find first log file name in binary log index file’
    其中 Last_IO_Error 提示錯誤如上。
    經查,發現是:CHANGE MASTER TO 命令的 MASTER_LOG_FILE 值寫錯了,找不到主庫 binlog。
    解決之後,Slave_SQL_Running 和 Slave_IO_Running 都為 YES。

  • 驗證主從同步生效
    修改主庫資料,驗證叢庫資料跟著修改。