主从复制搭建
主库操作:
1、添加复制权限
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl'@'192.168.26.2' identified by 'repl';
Query OK, 0 rows affected (0.01 sec)
2. 查看 master 库的状态
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000020 | 814 | | | 7e354a2c-6f5f-11e6-997d-005056a36f08:1-3 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
3.master 端备份全库
#mysqldump -uroot -p -S /home/mysql/mysql3306.sock --single-transaction --master-data=2 -A >/tmp/full_backup.sql
Enter password:
Warning: A partial dump from a server that has GTIDs will by default include the GTIDs of all transactions, even those that changed suppressed parts of the database.
If you don't want to restore GTIDs, pass --set-gtid-purged=OFF. To make a complete dump, pass --all-databases --triggers --routines --events.
会有一个警告:大概意思是说备份里有 GTID 的信息,如果不需要 GTID 的信息,可以加上后面提示的参数过滤掉,这个警告不用管
4. 将 master 端的备份到 scp 到 slave 端
# scp full_backup.sql root@192.168.26.2:/tmp
从库操作:
1.slave 端的参数文件
gtid-mode = on
enforce-gtid-consistency = 1
注意:server-id 不能一样
2. 恢复到 slave 端
# mysql -uroot -p -S /home/mysql/mysql/mysql3306.sock < /tmp/full_backup.sql
Enter password:
3.reset slave 信息
mysql> reset master;
Query OK, 0 rows affected (0.11 sec)
mysql> reset slave all;
Query OK, 0 rows affected (0.11 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> show global variables like '%gtid%';
+----------------------------------+-------+
| Variable_name | Value |
+----------------------------------+-------+
| binlog_gtid_simple_recovery | ON |
| enforce_gtid_consistency | ON |
| gtid_executed | |
| gtid_executed_compression_period | 1000 |
| gtid_mode | ON |
| gtid_owned | |
| gtid_purged | |
| session_track_gtids | OFF |
+----------------------------------+-------+
8 rows in set (0.01 sec)
可以看到 gtid_executed 和 gtid_purged 已经没有 gtid 的信息了
4. 然后 change master
mysql>change master to master_host='192.168.26.1', master_port=3306, master_user='repl',master_password='repl', master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.23 sec)
5. 启动 slave
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.26.233
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000020
Read_Master_Log_Pos: 814
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 414
Relay_Master_Log_File: mysql-bin.000020
Slave_IO_Running: Yes 状态正常!
Slave_SQL_Running: Yes 状态正常!
Replicate_Do_DB:
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: 814
Relay_Log_Space: 655
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: 3306100
Master_UUID: 7e354a2c-6f5f-11e6-997d-005056a36f08
Master_Info_File: /data/mysql/mysql_3306/data/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:
Executed_Gtid_Set: 7e354a2c-6f5f-11e6-997d-005056a36f08:1-3 状态正常!
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
状态正常!