https://blog.csdn.net/lswzw/article/details/89151740
vi /etc/my.cnf
添加:
主:
server-id=1 #[必须]服务器唯一ID,默认是1
log-bin=mysql-bin #[必须]启用二进制日志
从:
server-id=2
log-bin=mysql-bin
server-id 一定要不一样.
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
File列和Position列的值一会儿我们要用到。
mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO 'backup'@'192.168.1.%' IDENTIFIED BY 'Backup@123';
#添加用户:backup 密码: Backup@123 只允许:192.168.1.0段ip登录
mysql> flush privileges;
#刷新权限
注意:由于此时的mysql已经启动过,会自动保留一个uuid的服务编号,这个编号文件(auto.cnf)如果不删除,无法实现主从。
[root@mysql02 ~]# find / -name 'auto.cnf'
/var/lib/mysql/auto.cnf
[root@mysql02 ~]# cat /var/lib/mysql/auto.cnf
[auto]
server-uuid=10271616-5a93-11e9-ac44-00505698d778
[root@mysql02 ~]# rm -f /var/lib/mysql/auto.cnf
进入MySQL
CHANGE MASTER to MASTER_HOST="192.168.1.44",
MASTER_PORT=3306,
MASTER_user="backup",
MASTER_PASSWORD="Backup@123",
MASTER_LOG_FILE="mysql-bin.000002",
MASTER_LOG_POS=154;
start slave;
参数描述:
master_host=主服务器IP
master_user=在主服务器上创建的备份用户名
master_password=备份用户密码
master_log_file=查询master(主服务器)的状态得到的File列的值
master_log_pos=Position列的值
start slave:启动从服务器复制功能
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.44
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1053
Relay_Log_File: mysql02-relay-bin.000002
Relay_Log_Pos: 1219
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
两都是Yes,表示配置主从成功
mysql> create database db_test_slave;
mysql> use db_test_slave;
mysql> create table tb_test(id int(3), name varchar(50));
mysql> insert into tb_test values(1,'hello slave');
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db_test_slave |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> show databases; use db_test_slave; select * from tb_test;
+--------------------+
| Database |
+--------------------+
| information_schema |
| db_test_slave |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
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
+------+-------------+
| id | name |
+------+-------------+
| 1 | hello slave |
+------+-------------+
1 row in set (0.00 sec)
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 | 154 | | | |
+------------------+----------+--------------+------------------+-------------------+
mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO 'backup'@'192.168.1.%' IDENTIFIED BY 'Backup@123';
#添加用户:backup 密码: Backup@123 只允许:192.168.1.0段ip登录
mysql> flush privileges;
#刷新权限
CHANGE MASTER to MASTER_HOST="192.168.1.45",
MASTER_PORT=3306,
MASTER_user="backup",
MASTER_PASSWORD="Backup@123",
MASTER_LOG_FILE="mysql-bin.000002",
MASTER_LOG_POS=154;
start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.1.45
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 1053
Relay_Log_File: mysql02-relay-bin.000002
Relay_Log_Pos: 1219
Relay_Master_Log_File: mysql-bin.000002
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
两都是Yes,表示配置主从成功