首先准备两台服务器(或两个mysql)一个主一个从
mysql> select version();
+-------------------------+
| version() |
+-------------------------+
| 8.0.27-0ubuntu0.20.04.1 |
+-------------------------+
1 row in set (0.00 sec)
主服务器
server-id = 1
从服务器
server-id = 2
mysql> CREATE USER 'repl2'@'%' IDENTIFIED BY 'repl2';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repl2'@'%';
Query OK, 0 rows affected (0.01 sec)
如果需要修改用户密码加密方式,也可以在创建用户时使用WITH mysql_native_password
mysql> ALTER USER 'repl2'@'%' IDENTIFIED WITH mysql_native_password BY 'repl2';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select Host,User,plugin from user;
+-------------+------------------+-----------------------+
| Host | User | plugin |
+-------------+------------------+-----------------------+
| % | repl1 | caching_sha2_password |
| % | root | mysql_native_password |
| 192.168.2.* | repl2 | mysql_native_password |
| localhost | debian-sys-maint | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session | caching_sha2_password |
| localhost | mysql.sys | caching_sha2_password |
+-------------+------------------+-----------------------+
7 rows in set (0.00 sec)
主节点执行 加锁
mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)
mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000006 | 196 | | | cfcb5034-5ca9-11ec-8b10-0800275be603:2-3 |
+------------------+----------+--------------+------------------+------------------------------------------+
1 row in set (0.00 sec)
root@mysql-master:/etc/mysql# mysqldump -uroot -p --all-databases --master-data > dbdump.db
Enter password:
释放锁
```bash
mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)
将备份数据拷贝到从节点,然后导入数据库
root@mysql-master:/etc/mysql# mysql -uroot -p < dbdump.sql
Enter password:
在 MySQL 8.0.23 之前
mysql> CHANGE MASTER TO
-> MASTER_HOST='source_host_name',
-> MASTER_USER='replication_user_name',
-> MASTER_PASSWORD='replication_password',
-> MASTER_LOG_FILE='recorded_log_file_name',
-> MASTER_LOG_POS=recorded_log_position;
8.0.23之后
mysql> CHANGE REPLICATION SOURCE TO
-> SOURCE_HOST='source_host_name',
-> SOURCE_USER='replication_user_name',
-> SOURCE_PASSWORD='replication_password',
-> SOURCE_LOG_FILE='recorded_log_file_name',
-> SOURCE_LOG_POS=recorded_log_position;
CHANGE REPLICATION SOURCE TO SOURCE_HOST='192.168.2.161', SOURCE_USER='repl2', SOURCE_PASSWORD='repl2', SOURCE_LOG_FILE='mysql-bin.000006', SOURCE_LOG_POS=196 ;
从节点开始复制
mysql> start replica;
Query OK, 0 rows affected, 1 warning (0.00 sec)
查看复制状态
mysql> start replica;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> SHOW REPLICA STATUS\G
*************************** 1. row ***************************
Replica_IO_State: Waiting for source to send event
Source_Host: 192.168.2.161
Source_User: repl3
Source_Port: 3306
Connect_Retry: 60
Source_Log_File: mysql-bin.000006
Read_Source_Log_Pos: 2534
Relay_Log_File: mysql-node1-relay-bin.000003
Relay_Log_Pos: 324
Relay_Source_Log_File: mysql-bin.000006
Replica_IO_Running: Yes
Replica_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_Source_Log_Pos: 2534
Relay_Log_Space: 3045
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Source_SSL_Allowed: No
Source_SSL_CA_File:
Source_SSL_CA_Path:
Source_SSL_Cert:
Source_SSL_Cipher:
Source_SSL_Key:
Seconds_Behind_Source: 0
Source_SSL_Verify_Server_Cert: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Source_Server_Id: 1
Source_UUID: af1ff730-5e42-11ec-8ec1-0800275be603
Source_Info_File: mysql.slave_master_info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Replica_SQL_Running_State: Replica has read all relay log; waiting for more updates
Source_Retry_Count: 86400
Source_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Source_SSL_Crl:
Source_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Source_TLS_Version:
Source_public_key_path:
Get_Source_public_key: 0
Network_Namespace:
1 row in set (0.00 sec)
成功!