主從同步是分佈式mysql數據庫至關重要的配置,如今我在虛擬機上完成主從配置,系統是CenterOS6.5,mysql版本是5.7.13mysql
主服務器的ip是192.168.19.139 副服務器的ip是192.168.19.142sql
1.主服務器配置數據庫
(1)修改my.cnf(注意使用root)vim
1 vim /etc/my.cnf 2 3 4 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 5 log-bin=mysqlbin-log 6 server_id=1 7 basedir=/home/mysql 8 user=mysql 9 datadir=/home/mysql/data 10 port=3306 11 innodb_flush_log_at_trx_commit=1 12 sync_binlog=1 13 log-slave-updates
(2)進入mysql安裝目錄下的bin,啓動mysql服務器
1 cd /home/mysql/bin 2 ./mysqld 分佈式
(3)重開啓mysql客戶端,進入mysql安裝目錄下的bin,受權副服務器主從複製,用mysqld的好處是能夠查看錯誤。ide
1 ./mysql -uroot -p 2 mysql> grant replication slave on *.* to test@192.168.19.142 identified by '123'; spa
(4)顯示主服務器狀態code
1 mysql> show master status; 2 +---------------------+----------+--------------+------------------+-------------------+ 3 | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | 4 +---------------------+----------+--------------+------------------+-------------------+ 5 | mysqlbin-log.000019 | 1290 | | | | 6 +---------------------+----------+--------------+------------------+-------------------+
2.副服務器配置server
(1)修改my.cnf(注意使用root)
1 vim /etc/my.cnf 2 3 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLE 4 user=mysql 5 server_id = 2 6 replicate-do-db = sbfxd 7 master-info-file = master.info 8 relay-log = relay-relay-bin 9 relay-log-index = relay-relay-bin.index 10 relay-log-info-file=relay-relay-log.info
(2)進入mysql安裝目錄下的bin,啓動mysql
1 cd /home/mysql/bin 2 ./mysqld
(3)重開啓mysql客戶端,進入mysql安裝目錄下的bin,設置slave屬性並開始服務
1 ./mysql -uroot -p 2 mysql> change master to master_host='192.168.19.139',master_port= 3306, master_log_file='mysqlbin-log.000019', master_log_pos= 1290, master_bind='', master_user='test',master_password='123'; 3 mysql> start slave;
(4)顯示副服務器的狀態
1 show slave status \G; 2 3 Slave_IO_State: Waiting for master to send event 4 Master_Host: 192.168.19.139 5 Master_User: test 6 Master_Port: 3306 7 Connect_Retry: 60 8 Master_Log_File: mysqlbin-log.000019 9 Read_Master_Log_Pos: 1290 10 Relay_Log_File: relay-relay-bin.000002 11 Relay_Log_Pos: 323 12 Relay_Master_Log_File: mysqlbin-log.000019 13 Slave_IO_Running: Yes 14 Slave_SQL_Running: Yes 15 Replicate_Do_DB: sbfxd 16 Replicate_Ignore_DB: 17 Replicate_Do_Table: 18 Replicate_Ignore_Table: 19 Replicate_Wild_Do_Table: 20 Replicate_Wild_Ignore_Table: 21 Last_Errno: 0 22 Last_Error: 23 Skip_Counter: 0 24 Exec_Master_Log_Pos: 1290 25 Relay_Log_Space: 530 26 Until_Condition: None 27 Until_Log_File: 28 Until_Log_Pos: 0 29 Master_SSL_Allowed: No 30 Master_SSL_CA_File: 31 Master_SSL_CA_Path: 32 Master_SSL_Cert: 33 Master_SSL_Cipher: 34 Master_SSL_Key: 35 Seconds_Behind_Master: 0 36 Master_SSL_Verify_Server_Cert: No 37 Last_IO_Errno: 0 38 Last_IO_Error: 39 Last_SQL_Errno: 0 40 Last_SQL_Error: 41 Replicate_Ignore_Server_Ids: 42 Master_Server_Id: 1 43 Master_UUID: 05408021-5ab2-11e6-869b-000c293990c1 44 Master_Info_File: /home/mysql/data/master.info 45 SQL_Delay: 0 46 SQL_Remaining_Delay: NULL 47 Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates 48 Master_Retry_Count: 86400 49 Master_Bind: 50 Last_IO_Error_Timestamp: 51 Last_SQL_Error_Timestamp: 52 Master_SSL_Crl: 53 Master_SSL_Crlpath: 54 Retrieved_Gtid_Set: 55 Executed_Gtid_Set: 56 Auto_Position: 0 57 Replicate_Rewrite_DB: 58 Channel_Name: 59 Master_TLS_Version:
出現兩個yes表示slave能夠跑了
3.注意事項:
(1)樓主的虛擬機是經過克隆產生的,因此不須要對mysql進行備份,而後在副服務器上恢復,這步對於數據庫極其重要,不然就會出現以下錯誤:
1 2016-08-17T03:11:27.579193Z 5 [ERROR] Slave SQL for channel '': Error executing row event: 'Table 'sbfxd.sbtable' doesn't exist', Error_code: 1146 2 2016-08-17T03:11:27.584268Z 5 [Warning] Slave: Table 'sbfxd.sbtable' doesn't exist Error_code: 1146
(2)若是將主服務器的屬性配錯了須要stop slave,而後設置好以後start slave