linux配置mysql5.6 主从同步

Centos6.5 MySQL主从同步 MySQL版本5.6.33

主服务器:centos6.5 IP:172.16.38.225

从服务器:centos6.5 IP:172.16.38.226

此前已安装好两个数据库.而我安装的路径为/data/mysql-5.6.33

一、主服务器相关配置

  1、创建同步账户并指定服务器地址

 

1
2
3
4
[root@localhost ~]mysql -uroot -p
mysql>use mysql
mysql>grant replication slave on *.* to 'slaveuser'@'172.16.38.226 identified by '123456';
mysql>flush privileges #刷新权限

  授权用户slaveuser只能从172.16.38.226这个地址访问主服务器172.16.38.225的数据库,并且只具有数据库备份的权限

   创建用户(IP为可访问该master的IP,任意IP就写'%')

  2、修改my.cnf配置文件 vi /data/mysql-5.6.33/my.cnf

1
2
3
4
5
6
[mysqld]下添加以下参数,若文件中已经存在,则不用添加
server-id=2 
log-bin=mysql-bin  #启动MySQL二进制日志系统,
binlog-do-db=huagang  #需要同步的数据库
binlog-ignore-db=mysql   #不同步mysql系统数据库,若还有其它不想同步的,继续添加
[root@localhost ~]service mysql restart #重启服务

  3、查看主服务器master状态(注意File与Position项,从服务器需要这两项参数)

1
2
3
4
5
6
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000012 |      120 | huagang| mysql            |
+------------------+----------+--------------+------------------+

 

 

 二、从服务器相关配置

  1、修改my.cnf配置文件 vi /data/mysql-5.6.33/my.cnf

1
2
3
4
5
6
[mysqld]下添加以下参数,若文件中已经存在,则不用添加
server-id=3   #设置从服务器id,必须于主服务器不同
log-bin=mysql-bin  #启动MySQ二进制日志系统
replicate-do-db=huagang   #需要同步的数据库名
replicate-ignore-db=mysql   #不同步mysql系统数据库
[root@localhost~ ]service mysql restart #重启服务

  2、导入数据库

  导入过程这里不做阐述

    3、配置主从同步  

    注:master_log_file,master_log_pos,master_host相关参数值必须跟主库一致;

1
2
3
4
5
6
7
8
9
10
11
[root@localhost~ ]mysql -uroot -p
mysql>use mysql
mysql>stop slave;
mysql>change master to
      master_host='172.16.38.225',
      master_user='slaveuser',
      master_password='123456',
      master_log_file='mysql-bin.000012',
      master_log_pos=120;  #log_file与log_pos是主服务器master状态下的File与Position
mysql>start slave;
mysql>show slave status\G;

*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.38.225
Master_User: slaveuser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000007
Read_Master_Log_Pos: 5094
Relay_Log_File: cs_ztb-server6-relay-bin.000002
Relay_Log_Pos: 1142
Relay_Master_Log_File: mysql-bin.000007
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: 5094
Relay_Log_Space: 1324
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: 162f6160-817a-11eb-b9d1-000c29105f21
Master_Info_File: /data/mysql-5.6.33/data/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

 

注意查看Slave_IO_Running: Yes  Slave_SQL_Running: Yes 这两项必须为Yes 以及Log_File、Log_Pos要于master状态下的File,Position相同

如果都是正确的,则说明配置成功!

 

测试下,在225那台数据库添加一些数据表及数据,看226那台数据库有没有此数据,如果有此数据,说明主从同步是正常的

 

posted @ 2021-03-11 15:46  幸福眼泪  阅读(166)  评论(0编辑  收藏  举报