MySQL主主数据同步(2)

注意:要在节点2上创建同步帐户后再启动,否则会报连不上master错误

# 启动从节点,开始工作接收主节点发送事件(数据库数据变更的所有事件) mysql> start slave; # 此时再查看slave节点的状态 mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.206 Master_User: repl Master_Port: 3306 Connect_Retry: 30 Master_Log_File: edu-mysql-bin.000001 Read_Master_Log_Pos: 439 Relay_Log_File: edu-mysql-relay-bin.000002 Relay_Log_Pos: 287 Relay_Master_Log_File: edu-mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: # ...省略其它配置 3. 节点2配置(192.168.1.206) 3.1 添加数据同步配置 shell> vim /etc/my.cnf

在[mysqld]中增加以下配置项:

server_id=206 binlog-ignore-db=mysql log-bin=edu-mysql-bin binlog_cache_size=1M binlog_format=mixed expire_logs_days=7 slave_skip_errors=1062 relay_log=edu-mysql-relay-bin log_slave_updates=1 #ID自增从2开始,双数 auto_increment_increment=2 auto_increment_offset=2 3.2 Master配置 # 先重启一下服务 shell> service mysqld restart # 登录到mysql shell> mysql -uroot -p # 创建数据库同步用户,并授予相应的权限(只允许repl用户从192.168.1.205上登录) mysql> grant replication slave, replication client on *.* to 'repl'@'192.168.1.205' identified by 'root123456'; # 刷新授权表信息 mysql> flush privileges; # 查看binlog文件的position(偏移)和File(日志文件)的值,从机上需要用到 mysql> show master status; +----------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +----------------------+----------+--------------+------------------+-------------------+ | edu-mysql-bin.000001 | 439 | | mysql | | +----------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)

这时可以启动节点1(205)的slave服务

3.3 Slave配置 # master_log_file和master_log_pos:205节点上执行show master status;对应File和position的值 mysql> change master to master_host='192.168.1.205',master_user='repl', master_password='root123456', master_port=3306, master_log_file='edu-mysql-bin.000001', master_log_pos=120, master_connect_retry=30; Query OK, 0 rows affected, 2 warnings (0.02 sec) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.1.205 Master_User: repl Master_Port: 3306 Connect_Retry: 30 Master_Log_File: edu-mysql-bin.000001 Read_Master_Log_Pos: 120 Relay_Log_File: edu-mysql-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: edu-mysql-bin.000001 Slave_IO_Running: No Slave_SQL_Running: No Replicate_Do_DB: #...省略其它配置 3.4、启动Slave shell> start slave; Query OK, 0 rows affected (0.01 sec) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.205 Master_User: repl Master_Port: 3306 Connect_Retry: 30 Master_Log_File: edu-mysql-bin.000001 Read_Master_Log_Pos: 439 Relay_Log_File: edu-mysql-relay-bin.000002 Relay_Log_Pos: 287 Relay_Master_Log_File: edu-mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: ...省略其它配置 4、验证 # 登录205创建一个数据库 shell> mysql -u root -p mysql> create database if not exists mydb default character set utf8 collate utf8_general_ci; mysql> create table user (id int, username varchar(30), password varchar(30)); mysql> insert into user values (1, 'yangxin', '123456'); # 下面是在206节点上的操作 #1、登录206查询所有库,是否包含mydb数据库 #2、切换到mydb库,是否包含user表,并有一条数据 #3、在206的mydb.user表插入一条数据,查看205是否同步过去 mysql> insert into user values (2,'yangxin2','123456')

详细过程如下图所示:

数据同步操作过程

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/1d3df04511b25d6528ba8de2eb7acffe.html