要求:
1、版本一致
2、初始化表,并启动mysql服务
操作:
1、修改主服务器配置
vim /etc.my.cnf
[mysqld]
log-bin=mysql-bin 启用二进制日志
server-id=1 服务器唯一ID,默认是1
2、修改从服务器配置
vim /etc/my.cnf
[mysqld]
server-id=2
3、重启mysql服务
systemctl restart mysql
4、主服务器建立账号并授权
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.81.%' IDENTIFIED BY 'slave';
FLUSH PRIVILEGES;
show master status;
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 | 409 |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化
5、配置从服务器
change master to master_host='192.168.81.145',master_user='slave',master_password='slave',master_log_file='mysql-bin.000001',master_log_pos=409;
start slave;
6、检查从服务器状态
show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.81.141
Master_User: slave
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 847
Relay_Log_File: mysqld-relay-bin.000002
Relay_Log_Pos: 721
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
.....
Slave_IO_Running: Yes和Slave_SQL_Running: Yes即为正常。