门前有条河

 

Mysql主从复制

折腾了几天,终于弄清了mysql主从复制,原来如此简单

 

1.主从保持版本一致,

2.开启主服务器的bin-log

[root@cetos2 ~]# whereis my.cn
my: /etc/my.cnf
[root@cetos2 ~]# vim /etc/my.cnf

 

 

server-id=129  //服务器id 这个要与从服务器区分
log_bin=mysql-bin
log_bin_index=mysql-bin.index
expire_logs_days=10
max_binlog_size=100M

重启服务

[root@cetos2 ~]# systemctl restart mysql;
[root@cetos2 ~]#

--查看主服务器日志是否已开启  ON代表已开启

mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON |
+---------------+-------+
1 row in set (0.00 sec)

 

3.修改从服务器slave

#vi /etc/my.cnf
    [mysqld]
    log-bin=mysql-bin   //[不是必须]启用二进制日志
    server-id=226      //[必须]服务器唯一ID,默认是1,一般取IP最后一段

4.在主服务器上建立帐户并授权slave:

mysql>GRANT REPLICATION SLAVE ON *.* 'repl'@'%' identified by 'm123456';

//通常复制用单独的账号 repl

5. 登录主服务器的mysql,查询master的状态


mysql> show master status\g;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 | 120 | | | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

ERROR:
No query specified

注:执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化

 

6.配置从服务器Slave:


mysql> change master to master_host='192.168.146.128',master_user='repl',
-> master_password='m123456',
-> master_log_file='mysql-bin.000004',master_log_pos=120;

  • master_host:Master 服务器IP
  • master_user:Master 服务器授权用户,也就是 Master 前面创建的那个用户
  • master_password:Master 服务器授权用户对应的密码
  • master_log_file:Master binlog 文件名,对应查询Master服务器状态时,File字段
  • master_log_pos:Master binlog 文件中的 Postion 值,对应查询Master服务器状态时,Position字段
  • 更多的选项可以看:http://dev.mysql.com/doc/refman/5.7/en/change-master-to.html  

Mysql>start slave; //启动从服务器复制功能

如果上面有错误可以重新配置

在从库上reset slave all  ,重新配置,用repl用户

mysql> reset slave all

mysql>stop slave //停止

mysql>start slave //启动

7.检查从服务器复制功能状态:

mysql> show slave status\G

mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.146.128
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 517
Relay_Log_File: mysqld-relay-bin.000010
Relay_Log_Pos: 283
Relay_Master_Log_File: mysql-bin.000006
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: 517
Relay_Log_Space: 457
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: 128
Master_UUID: 96ed73d8-942e-11ea-ae75-000c29f06575
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
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
1 row in set (0.00 sec)

ERROR:
No query specified

 

*************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.2.222 //主服务器地址 Master_User: mysync //授权帐户名,尽量避免使用root Master_Port: 3306 //数据库端口,部分版本没有此行 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 600 //#同步读取二进制日志的位置,大于等于Exec_Master_Log_Pos Relay_Log_File: ddte-relay-bin.000003 Relay_Log_Pos: 251 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes //此状态必须YES Slave_SQL_Running: Yes //此状态必须YES ...... 注:Slave_IO及Slave_SQL进程必须正常运行,即YES状态,否则都是错误的状态(如:其中一个NO均属错误)。 以上操作过程,主从服务器配置完成。

8.主从服务器测试

主服务器Mysql,建立数据库,并在这个库中建表插入一条数据:
可以看到从服务器已能同步

转自 https://www.cnblogs.com/NiceCui/p/8213723.html

 

posted on 2020-06-12 16:26  王述兵  阅读(193)  评论(0)    收藏  举报

导航