四.构建双主单从复制架构
1.双主复制的数据库主配置文件配置
node3节点的配置:
[root@node3 ~]# vim /etc/my.cnf
[mysqld] #在此配置段中增加如下内容;
log-bin=mysql-bin
binlog_format=mixed
server-id = 1
relay-log=relay-bin
auto_increment_offset=1
auto_increment_increment=2
log_slave_updates = 1
node4节点的配置:
[root@node4 ~]# vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
binlog_format=mixed
server-id = 10
relay-log = relay-bin
auto_increment_offset=2
auto_increment_offset=2
log_slave_updates = 1
从节点node6:
node6节点的配置:
[root@node6 ~]# vim /etc/my.cnf
[mysqld]
log-bin=mysql-bin
binlog_format=mixed
server-id = 20
relay-log = relay-bin
log_slave_updates = 1
read_only = on
修改过刚才的配置文件以后,在各节点重新启动mysqld服务器;
# service mysqld restart
Shutting down MySQL.. [ OK ]
Starting MySQL. [ OK ]
2.创建复制使用的mysql用户
为了方便,我们就授权一个repluser账户在172.16.0.0这个网段内都能实现复制;
在node3和node4节点上执行如下指令:
12 MariaDB [(none)]> grant replication slave,replication client on *.* to repluser@'172.16.%.%' identified by 'replpass';
MariaDB [(none)]> flush privileges;
3.同步数据
由于我们都是新装的库,数据是同步的,但是我们也简单操作以下步骤,开始配置同步数据到复制结尾,都不要让任何的mysql服务器写入数据,避免导致数据不同步。
查看两个节点的时间点;
node3节点的时间点;
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000006 | 653 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
node4节点的时间点:
MariaDB [(none)]> show master status;
+------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000006 | 653 | | |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)
两个节点的时间点都是一致的,数据是一致的,那就方便设置了;
4.双主复制模型开启设置
node3节点启动:
#开启复制;
MariaDB [(none)]> change master to master_host='172.16.31.21',master_user='repluser',master_password='replpass',master_log_file='mysql-bin.000006',master_log_pos=653;
#开启复制进程;
MariaDB [(none)]> start slave;
node4节点启动:
MariaDB [(none)]> change master to master_host='172.16.31.20',master_user='repluser',master_password='replpass',master_log_file='mysql-bin.000006',master_log_pos=653;
MariaDB [(none)]> start slave;
5.查看节点的复制状态信息;
node3的节点的主从复制状态:
1234567891011121314151617 MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.31.21
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 653
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 535
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
信息略;
Seconds_Behind_Master: 0
1 row in set (0.00 sec)
node4节点的主从复制状态信息;
MariaDB [(none)]> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.31.20
Master_User: repluser
Master_Port: 3306
Connect_Retry: 5
Master_Log_File: mysql-bin.000006
Read_Master_Log_Pos: 653
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 535
Relay_Master_Log_File: mysql-bin.000006
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
信息略;
Seconds_Behind_Master: 0
1 row in set (0.00 sec)
6.简单的双主复制测试;
我们在node3节点插入测试数据库;
在hellodb中的teachers表中插入数据:
[root@node3 ~]# mysql -uroot -pOracle < hellodb.sql
登录数据库:
[root@node3 ~]# mysql -uroot -poracle
#切换到hellodb数据库;
MariaDB [(none)]> use hellodb;
Database changed
#先查看teachers表;
MariaDB [hellodb]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
+-----+---------------+-----+--------+
4 rows in set (0.00 sec)
#插入两条新数据;
MariaDB [hellodb]> insert into teachers(name,age,gender) values('Huang Yaoshi',56,'M'),('Feng Qingyang',100,'M');
Query OK, 2 rows affected (0.04 sec)
Records: 2 Duplicates: 0 Warnings: 0
#再次查看teachers表,观察ID的变化;
MariaDB [hellodb]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
| 5 | Huang Yaoshi | 56 | M |
| 7 | Feng Qingyang | 100 | M |
+-----+---------------+-----+--------+
6 rows in set (0.00 sec)
node3节点的ID自增是以奇数为顺序的,说明我们的双主复制架构完成了。
7.配置从服务器节点node6:
拷贝数据
(假如是你完全新安装mysql主从服务器,这个一步就不需要。因为新安装的master和slave有相同的数据)
关停Master服务器,将Master中的数据拷贝到从服务器节点中,使得Master和slave中的数据同步,并且确保在全部设置操作结束前,禁止在Master和slave服务器中进行写操作,使得两数据库中的数据一定要相同!
停止node3节点和node6节点的数据库:
# service mysqld stop
删除node6节点的数据库文件:
[root@node6 ~]# mkdir /backup
[root@node6 ~]# mv /mydata/data/* /backup/
[root@node6 ~]# ls /mydata/data/
将node3节点的数据库数据全部复制到节点node6:
[root@node3 mydata]# scp -p -r data/* node6:/mydata/data/
记得更改数据库数据目录里的所有权限为mysql:
[root@node6 ~]# chown -R mysql:mysql /mydata/data/
将其中原有的中继日志删除:
[root@node6 ~]# rm -rf /mydata/data/relay*
配置完成后启动服务器:
# service mysqld start
启动从节点的中继日志:
MariaDB [(none)]> change master to master_host='172.16.31.20',master_user='repluser',master_password='replpass',master_log_file='mysql-bin.000009',master_log_pos=326;
启动中继日志进程;
MariaDB [(none)]> start slave;
查看从节点的状态:
MariaDB [(none)]> show slave status \G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.16.31.20
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000009
Read_Master_Log_Pos: 326
Relay_Log_File: relay-bin.000002
Relay_Log_Pos: 535
Relay_Master_Log_File: mysql-bin.000009
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
信息略。。。
查看从节点的数据跟主节点是否一致:
MariaDB [(none)]> use hellodb;
Database changed
MariaDB [hellodb]> select * from teachers;
+-----+---------------+-----+--------+
| TID | Name | Age | Gender |
+-----+---------------+-----+--------+
| 1 | Song Jiang | 45 | M |
| 2 | Zhang Sanfeng | 94 | M |
| 3 | Miejue Shitai | 77 | F |
| 4 | Lin Chaoying | 93 | F |
| 5 | Huang Yaoshi | 56 | M |
| 7 | Feng Qingyang | 100 | M |
+-----+---------------+-----+--------+
6 rows in set (0.00 sec)
OK,从节点配置完毕;