大家好,我是 沐风晓月 本文收录于《数据库入门到精通系列》专栏, 更多内容可以关注我的csdn博客。
本文主要讲解MySQL主主架构实战,在开始之前需要根据下面的提示来配置环境:
本次实验需要提前配置好两台安装好MySQL8的服务器
架构 | 版本 | IP | hostname |
---|---|---|---|
master1 | CentOS7.6 | 192.168.1.41 | master41 |
master2 | CentOS7.6 | 192.168.1.42 | master42 |
数据库密码: Root!2#admin
要实现主主复制,就是在主从复制的基础上配置相互为主从。 这里很容易需要对两台服务器反复配置,很容易搞乱。
vim /etc/my.cnf
[mysqld]server-id = 1
log-bin=mysql-bin
auto_increment_increment=2
auto_increment_offset=1
replicate-do-db=test_db
对上文代码进行注释:
server-id = 1 #任意自然n,只需要保证两台mysql主机不重复就可以
log-bin=mysql-bin #开启二进制日志
auto_increment_increment=2 #步进值auto_imcrement 。一般有n台主mysql就填n
auto_increment_offset=1 #起始值,一般填写第n台主机mysql.此时为第一台主 mysql
#binlog-ignore=mysql #忽略mysql库,可以不填写
#binlog-ignore=infomation_schema #忽略information_schema库,一般不填写
replicate-do-db=test_db #指定同步的数据库,不填写则默认所有的数据库
配置完成后,重启数据库
在 配置文件[mysqld]模块下添加以下内容:
vim /etc/my.cnf
server-id = 2
log-bin=mysql-bin
auto_increment_increment=2
auto_increment_offset=2
replicate-do-db=test_db
对MySQL1配置主从,在这里就是对192.168.1.41这台服务器设置MySQL主从,master1做为主库,master2作为从库。
root@mufeng41 ~]# mysql -uroot -p'Root!2#admin'
create user 'mufeng41'@'%' identified with mysql_native_password by 'mysqlMufeng@passw0rd';
#创建用户 mysql8.0中密码需要填写mysql_native_password
grant replication slave on *.* to 'mufeng41'@'%';
#分配权限
flush privileges;
#刷新权限
mysql> show grants for 'mufeng41'@'%';
+--------------------------------------------------+
| Grants for mufeng41@% |
+--------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO `mufeng41`@`%` |
+--------------------------------------------------+
1 row in set (0.00 sec)
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000001 | 1125 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql>
2. 设置MySQL2为从库
[root@mufeng42 ~]# mysql -uroot -p'Root!2#admin'
mysql> change master to master_host='192.168.1.41',
-> master_user='mufeng41',
-> master_password='mysqlMufeng@passw0rd',
-> master_log_file='mysql-bin.000001',
-> master_log_pos=1125;
Query OK, 0 rows affected, 8 warnings (0.01 sec)
为方便下次使用这个SQL语句方便,这里改成一条语句,不分段的(切记不需要执行两遍):
change master to master_host='192.168.1.41',master_user='mufeng41',master_password='mysqlMufeng@passw0rd',master_log_file='mysql-bin.000001',master_log_pos=1125;
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
3.测试主从是否成功
先在主数据库插入数据,然后从从库进行查询:
mysql> create database test_db;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test_db |
+--------------------+
5 rows in set (0.01 sec)
mysql>
至此已经配置成功master1做为主库,MySQL2做为从库的情况,如果你在配置这一步的时候,遇到问题,比如主从不同步的情况,可以参考文章:《成功解决主从同步异常之Slave_IO_Running显示为No的问题》
此时 设置的主库为master2,从库为master1
1. 配置master2为主master服务器
create user 'mufeng42'@'%' identified with mysql_native_password by 'mysqlMufeng@passw0rd';
#创建用户 mysql.80中密码需要填写mysql_native_password
grant replication slave on *.* to 'mufeng42'@'%';
#分配权限
flush privileges;
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| mysql-bind.000001 | 1051 | | | |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
2. 配置master1为从服务器
mysql> change master to master_host='192.168.1.42', master_user='mufeng42', master_password='mysqlMufeng@passw0rd', master_log_file='mysql-bin.000001', master_log_pos=1051;
Query OK, 0 rows affected, 8 warnings (0.01 sec)
mysql> start slave;
mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
有时候显示不同步可以直接设置reset slave
mysql> reset slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show slave status \G
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test_db |
+--------------------+
5 rows in set (0.00 sec)
mysql> use test_db
Database changed
mysql> create table test_db(id int,name varchar(20));
Query OK, 0 rows affected (0.01 sec)
mysql> use test_db;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| test_db |
+-------------------+
1 row in set (0.01 sec)
可以看到,数据已经同步过来了,至此MySQL主主架构就配置完成了。
以上就是主主架构配置的全部内容了,如果对你有用,欢迎点赞收藏哦。
好啦,这就是今天要分享给大家的全部内容了,我们下期再见!
本文由沐风晓月原创,首发于CSDN博客, 博客主页:mufeng.blog.csdn.net
学习如逆水行舟,不进则退,一起努力加油哦!
喜欢的话记得点赞收藏哈