1、检查原主库和原备库是否正常
原主库:
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000009
Position: 4868
Binlog_Do_DB: test_slave_db
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
ERROR:
No query specified
原备库:
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.59.16
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 4868
Relay_Log_File: hhh-relay-bin.000003
Relay_Log_Pos: 2093
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test_slave_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: 4868
Relay_Log_Space: 3170
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: 1
Master_UUID: ae69ef2c-5334-11ea-a425-000c2980352c
Master_Info_File: /app/mysql/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:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
2、确保原备库启用二进制日志
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000007
Position: 13425
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
ERROR:
No query specified
mysql>
mysql>
mysql> show binary logs;
+------------------+-----------+
| Log_name | File_size |
+------------------+-----------+
| mysql-bin.000001 | 363 |
| mysql-bin.000002 | 177 |
| mysql-bin.000003 | 177 |
| mysql-bin.000004 | 177 |
| mysql-bin.000005 | 683 |
| mysql-bin.000006 | 683 |
| mysql-bin.000007 | 13425 |
+------------------+-----------+
7 rows in set (0.00 sec)
mysql>
mysql>
mysql>
mysql>
mysql> show variables like 'log-bin%';
Empty set (0.00 sec)
mysql>
mysql>
mysql> show variables like 'log_bin%';
+---------------------------------+-------------------------------------------+
| Variable_name | Value |
+---------------------------------+-------------------------------------------+
| log_bin | ON |
| log_bin_basename | /app/mysql/log/binary_log/mysql-bin |
| log_bin_index | /app/mysql/log/binary_log/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
+---------------------------------+-------------------------------------------+
5 rows in set (0.00 sec)
mysql>
mysql>
mysql> system ls - /app/mysql/binary_log
ls: cannot access -: No such file or directory
ls: cannot access /app/mysql/binary_log: No such file or directory
mysql> system ls -l /app/mysql/binary_log
ls: cannot access /app/mysql/binary_log: No such file or directory
mysql> system ls -l /app/mysql/log/binary_log
total 44
-rw-r----- 1 mysql mysql 363 Feb 29 15:05 mysql-bin.000001
-rw-r----- 1 mysql mysql 177 Feb 29 15:18 mysql-bin.000002
-rw-r----- 1 mysql mysql 177 Feb 29 15:37 mysql-bin.000003
-rw-r----- 1 mysql mysql 177 Feb 29 15:53 mysql-bin.000004
-rw-r----- 1 mysql mysql 683 Feb 29 16:01 mysql-bin.000005
-rw-r----- 1 mysql mysql 683 Feb 29 16:11 mysql-bin.000006
-rw-r----- 1 mysql mysql 13425 Feb 29 23:22 mysql-bin.000007
-rw-r----- 1 mysql mysql 301 Feb 29 16:11 mysql-bin.index
3、修改原从库相关参数(为被切换做为主库做准备)
mysql> system cat /etc/my.cnf
[mysqld]
#base configure
basedir=/app/mysql
datadir=/app/mysql/data
socket=/app/mysql/mysql.sock
log_error=/app/mysql/log/mysqld.log
server-id=2
explicit_defaults_for_timestamp=true
validate_password_policy=LOW
validate_password_length=4
autocommit = OFF
log_timestamps = SYSTEM
#enable binary log
log-bin = /app/mysql/log/binary_log/mysql-bin
binlog_format = ROW
expire_logs_days = 10
#configure master slave replication
##slave
replicate-do-db = test_slave_db
#replicate-ignore-table = test_slave_db.t5
#read_only = ON
skip_slave_start
##master
binlog-do-db = test_slave_db
log_slave_updates
mysql> select user,host from mysql.user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| root | % |
| mysql.session | localhost |
| mysql.sys | localhost |
+---------------+-----------+
3 rows in set (0.00 sec)
添加用户复制二进制日志到备库的用户slave
mysql> create user 'slave'@'192.168.59.%' identified by 'slave';
Query OK, 0 rows affected (0.01 sec)
mysql> grant replication slave on *.* to 'slave'@'192.168.59.%';
Query OK, 0 rows affected (0.00 sec)
mysql> select user,host from mysql.user;
+---------------+--------------+
| user | host |
+---------------+--------------+
| root | % |
| slave | 192.168.59.% |
| mysql.session | localhost |
| mysql.sys | localhost |
+---------------+--------------+
4 rows in set (0.00 sec)
重启原备库
mysql> system systemctl restart mysqld
转换原从库为主库
mysql> reset master;
Query OK, 0 rows affected (0.01 sec)
mysql> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 154
Binlog_Do_DB: test_slave_db
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
ERROR:
No query specified
4、修改原主库参数文件,添加相关从库参数,为转换为从库做准备
原主库:
[root@kkk ~]# cat /etc/my.cnf
[mysqld]
basedir=/app/mysql
datadir=/app/mysql/data
socket=/app/mysql/mysql.sock
log_error=/app/mysql/log/mysqld.log
server-id=1
explicit_defaults_for_timestamp=true
validate_password_policy = LOW
validate_password_length = 4
autocommit = OFF
log_timestamps = SYSTEM
#enable binary log
log-bin = /app/mysql/log/binary_log/mysql-bin
binlog_format = ROW
expire_logs_days = 10
max_binlog_size = 2G #default is 1G
#configure master slave replication
##master
binlog-do-db = test_slave_db
log_slave_updates
##slave
skip_slave_start
replicate-do-db = test_slave_db
重启原主库
[root@kkk ~]# systemctl stop mysqld
[root@kkk ~]# systemctl restart mysqld
5、转换为从库
mysql> change master to master_host = '192.168.59.17',master_user = 'salve',master_password = 'slave',master_log_file = 'mysql-bin.000001',master_log_pos = 154
-> ;
Query OK, 0 rows affected, 2 warnings (0.04 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State:
Master_Host: 192.168.59.17
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: kkk-relay-bin.000001
Relay_Log_Pos: 4
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: No
Slave_SQL_Running: No
Replicate_Do_DB: test_slave_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: 154
Relay_Log_Space: 154
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: NULL
Master_SSL_Verify_Server_Cert: No
Last_IO_Errno: 1045
Last_IO_Error: error connecting to master 'salve@192.168.59.17:3306' - retry-time: 60 retries: 2
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 0
Master_UUID:
Master_Info_File: /app/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State:
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp: 200229 23:43:47
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
启动IO_THREAD和SQL_THREAD
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.59.17
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Log_File: kkk-relay-bin.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: test_slave_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: 154
Relay_Log_Space: 525
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: 2
Master_UUID: c9ba1a6c-59f7-11ea-b542-000c29436c06
Master_Info_File: /app/mysql/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:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
6、测试:
主库:
mysql> select database();
+---------------+
| database() |
+---------------+
| test_slave_db |
+---------------+
1 row in set (0.00 sec)
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)
mysql>
mysql> show tables;
+-------------------------+
| Tables_in_test_slave_db |
+-------------------------+
| t1 |
| t2 |
| t3 |
| t4 |
| t5 |
| t6 |
| t8 |
+-------------------------+
7 rows in set (0.00 sec)
mysql>
mysql>
mysql> create table test1 as select * from t1;
Query OK, 2 rows affected (0.07 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | a |
| 2 | b |
+------+------+
2 rows in set (0.00 sec)
备库:
mysql> select database();
+---------------+
| database() |
+---------------+
| test_slave_db |
+---------------+
1 row in set (0.00 sec)
mysql> show tables;
+-------------------------+
| Tables_in_test_slave_db |
+-------------------------+
| t1 |
| t2 |
| t3 |
| t4 |
| t5 |
| t6 |
| t8 |
| test1 |
+-------------------------+
8 rows in set (0.00 sec)
mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | a |
| 2 | b |
+------+------+
2 rows in set (0.00 sec)
至此,转换完成...