1. 程式人生 > >mysql 5.7 主從同步 gtid

mysql 5.7 主從同步 gtid

back trying waiting maria nlog 創建 single lib t_sql

環境:1、(主) linux centOS 7 64位
2、(從) linux centOS 7 64位
3、(mysql)最好要求版本一致,從庫不能比主庫版本高 建議5.7

centos 7 默然安裝mariadb,安裝mysql 5.7參考如下鏈接文檔:
centos 7安裝mysql 5.7 :https://juejin.im/post/5c088b066fb9a049d4419985 (轉載)
一、主庫配置
1、更改主庫/etc/my.cnf 配置文件

vim /etc/my.cnf

[mysqld]
lower_case_table_names=1
max_connections=7000
group_concat_max_len = 202400

max_allowed_packet = 128M
#開啟gtid功能
gtid-mode=on
enforce-gtid-consistency=1
#設置server_id,一般設置為IP,註意要唯一
server_id=840
復制過濾:也就是指定哪個數據庫不用同步(mysql庫一般不同步)
binlog-ignore-db=mysql
#指定哪個數據庫同步,本次只同步newerp庫
binlog-do-db=newerp
#開啟二進制日誌功能,可以隨便取,最好有含義關鍵就是這裏了
log-bin=edu-mysql-bin
為每個session 分配的內存,在事務過程中用來存儲二進制日誌的緩存
binlog_cache_size=1M
主從復制的格式 mixed,statement,row,默認格式是statement
binlog_format=mixed
二進制日誌自動刪除/過期的天數。默認值為0,表示不自動刪除。
expire_logs_days=7
跳過主從復制中遇到的所有錯誤或指定類型的錯誤,避免slave端復制中斷。
如:1062錯誤是指一些主鍵重復,1032錯誤是因為主從數據庫數據不一致
slave_skip_errors=1062

2、重啟mysql數據庫

systemctl restart mysqld

3、創建同步賬戶和查看master信息

[[email protected] ~]$ msyql -uroot -p

Enter password:
mysql>GRANT REPLICATION SLAVE ON . to ‘ slave_account ‘@‘%‘ identified by ‘123456‘;
mysql>FLUSH PRIVILEGES;
mysql> SHOW MASTER STATUS\G;
1. row
File: edu-mysql-bin.000031
Position: 1210791
Binlog_Do_DB: newerp
Binlog_Ignore_DB: mysql
Executed_Gtid_Set: 2abeaffc-6158-11e7-8222-00163e03196b:1-16151
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>

註:‘slave_account‘ 為主庫創建同步用戶,‘123456‘為數據庫slave_account用戶密碼。
註:記錄下File: edu-mysql-bin.000031和 Position: 1210791,後面從庫會使用到。

4、mysqldump導出newerp庫到從庫

mysqldump -uroot -p123456 -hlocalhost --single-transaction --master-data=2 newerp > /data/newerp_back.sql

寫了一個腳本供參考

  1. > DB_USER="root"
  2. > DB_PASS="123456!"
  3. > DB_HOST="localhost"
  4. > DB_NAME="newerp"
  5. > BIN_DIR="/usr/bin"
  6. > BCK_DIR="/data"
  7. > DATE=date +%Y-%m-%d_%H-%M-%S
  8. > $BIN_DIR/mysqldump -u$DB_USER -p$DB_PASS -h$DB_HOST --single-transaction --master-data=2 $DB_NAME > $BCK_DIR/$DB_NAME.$DATE.sql

5、把備份文件scp傳到從庫

scp -P 22312 /data/newerp_back.sql [email protected]從庫IP:/root/
密碼:

主庫配置完成,開始配置從庫。

二、從庫配置
1、從庫 /etc/my.cnf 配置

[mysqld]

gtid-mode=on #開啟gtid,5.6版本之後的功能

enforce-gtid-consistency=1

server_id=3026 #id必填項,不要與主庫id相同

replicate-ignore-db=mysql #表示不同步mysql庫,可以寫多個

replicate-do-db=newerp #表示只同步newerp庫,可以寫多個

2、更改完重啟mysqld服務

systemctl restart mysqld

3、導入newerp_back.sql數據

[[email protected] ~]# mysql -uroot -p < /root/newerp_back.sql

4、進入mysql

mysql> change master to master_host=‘10.175.18.40‘,master_user=‘slave_account‘,master_password=‘123456‘,master_log_file=‘edu-mysql-bin.000031‘,master_log_pos=1210791;

mysql> start slave; #開啟同步 ,stop slave 停止同步,reset master 重置主庫信息

mysql> `show slave status\G;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id: 37
Current database: NONE

1. row
Slave_IO_State: Waiting for master to send event
Master_Host: 10.175.18.40
Master_User: slave_account
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: edu-mysql-bin.000032
Read_Master_Log_Pos: 1031964
Relay_Log_File: localhost-relay-bin.000004
Relay_Log_Pos: 1032185
Relay_Master_Log_File: edu-mysql-bin.000032
Slave_IO_Running: Yes #兩個都為YES表示成功開啟同步
Slave_SQL_Running: Yes #兩個都為YES表示成功開啟同步
Replicate_Do_DB: newerp
Replicate_Ignore_DB: mysql
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: 1031964
Relay_Log_Space: 1032606
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: 840
Master_UUID: 2abeaffc-6158-11e7-8222-00163e03196b
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: 2abeaffc-6158-11e7-8222-00163e03196b:10626-16016
Executed_Gtid_Set: 2abeaffc-6158-11e7-8222-00163e03196b:1-16016
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)

ERROR:
No query specified

mysql>

註:master_host=‘10.175.18.40‘為主庫IP,master_user=‘slave_account‘ 為主庫創建同步用戶,master_password=‘123456‘為數據庫slave_account用戶密碼,master_log_file=‘edu-mysql-bin.000031‘,master_log_pos=1210791;兩項從主庫獲取。從主庫執行sql命令:SHOW MASTER STATUS;

5、測試
在主庫創建新表或插入新數據,看看從庫是否同步。

mysql 5.7 主從同步 gtid