同一條伺服器(centos7)配置mysql 主從同步
說明:以下的埠為3307的例項為主資料庫,3308 對應的例項為從庫
主資料庫 配置檔案
cd /home/multiMysql/
vim ./etc/3307.cnf
新增內容:
server-id=3307 log-bin=master-bin log-bin-index=master-bin.index
binlog-do-db=test1
[client] port = 3307 socket = /home/multiMysql/socket/mysql3307.sock [mysqld] datadir=/home/multiMysql/datadir/3307 port = 3307 socket = /home/multiMysql/socket/mysql3307.sock tmpdir=/home/multiMysql/tmp/3307 myisam-recover-options = BACKUP server-id=3307 log-bin=master-bin log-bin-index=master-bin.index binlog-do-db=test1
重啟主資料庫:
[[email protected] multiMysql]# ./bin/mysql3307 restart
Stoping MySQL...
Starting MySQL...
Start MySql OK! PORT:3307
[[email protected] multiMysql]#
檢視主庫的狀態:
[[email protected] multiMysql]# mysql -u root -p -S ./socket/mysql3307.sock Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 15 Server version: 10.3.8-MariaDB-log MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show master status; +-------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +-------------------+----------+--------------+------------------+ | master-bin.000002 | 771 | test1 | | +-------------------+----------+--------------+------------------+ 1 row in set (0.000 sec)
一定要記住:File,Position,等會用到
從資料庫 配置檔案
vim ./etc/3308.cnf
新增內容:
server-id=3308 relay-log-index=slave-relay-bin.index relay-log=slave-relay-bin replicate_do_db=test1
[client] port = 3308 socket = /home/multiMysql/socket/mysql3308.sock [mysqld] datadir=/home/multiMysql/datadir/3308 port = 3308 socket = /home/multiMysql/socket/mysql3308.sock tmpdir=/home/multiMysql/tmp/3308 myisam-recover-options = BACKUP server-id=3308 relay-log-index=slave-relay-bin.index relay-log=slave-relay-bin replicate_do_db=test1
重啟從庫:
[[email protected] multiMysql]# ./bin/mysql3308 restart
Stoping MySQL...
Starting MySQL...
Start MySql OK! PORT:3308
[[email protected] multiMysql]#
回到主庫:並且進入資料庫
mysql -u root -p -S ./socket/mysql3307.sock
在主庫裡面 設定從庫許可權:
GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.2.187:3308' IDENTIFIED BY 'XXXX';
CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000002',MASTER_LOG_POS=771;
---主庫配置基本完畢--
從庫繼續:進入從庫
[[email protected] multiMysql]# mysql -u root -p -S ./socket/mysql3308.sock
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 25
Server version: 10.3.8-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
設定主庫的路徑:注意版本 有的版本寫法不一樣,此處寫的是 change master
CHANGE MASTER TO MASTER_HOST='192.168.2.187',MASTER_USER='root',MASTER_PASSWORD='XXX',MASTER_PORT=3307,MASTER_CONNECT_RETRY=60,MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=107
啟動:(start slave),有的版本寫法為:slave start,根據自身情況確定
start slave
檢測結果:
MariaDB [(none)]> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 127.0.0.1
Master_User: root
Master_Port: 3307
Connect_Retry: 60
Master_Log_File: master-bin.000002
Read_Master_Log_Pos: 343
Relay_Log_File: slave-relay-bin.000004
Relay_Log_Pos: 643
Relay_Master_Log_File: master-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test1
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 343
Relay_Log_Space: 1252
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File:
Master_SSL_CA_Path:
Master_SSL_Cert:
Master_SSL_Cipher:
Master_SSL_Key:
Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 3307
Master_SSL_Crl:
Master_SSL_Crlpath:
Using_Gtid: No
Gtid_IO_Pos:
Replicate_Do_Domain_Ids:
Replicate_Ignore_Domain_Ids:
Parallel_Mode: conservative
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
Slave_DDL_Groups: 1
Slave_Non_Transactional_Groups: 0
Slave_Transactional_Groups: 1
1 row in set (0.001 sec)
ERROR: No query specified
其中:Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: test1
前兩個=yes說明,是OK,然後在主資料庫 新增表、寫入資料,修改資料,從庫都可以看到對應資料
另外說明遇到的問題;
1、Slave_IO_Running: NO,很多都是 住資料庫的日誌檔案+起始位置不一樣導致 NO
問題展現形式: 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: Got fatal error 1236 from master when reading data from binary log: 'bogus data in log event; the first event 'master-bin.000001' at 107, the last event read from 'master-bin.000001' 126.'
解決方案:
stop slave;
下方的master_log_file ,master_log_pos 是前方讓記住的檔案位置,一定不能錯
MariaDB [test1]> CHANGE MASTER TO MASTER_LOG_FILE='master-bin.000001',MASTER_LOG_POS=329;
Query OK, 0 rows affected (0.072 sec)
MariaDB [test1]> start slave;
Query OK, 0 rows affected (0.001 sec)
然後再:show slave status\G; 可以解決
2、本次只是處理了 具體資料庫test1,其他設定未驗證,大家可以嘗試:
主數其他據庫配置:
binlog-ignore-db=mysql //要忽略的資料庫
從數其他據庫配置:
replicate-ignore-db=mysql //要忽略的資料庫
readonly
slave-skip-errors=all
slave-net-timeout=
60