南京网站建设公司 雷仁网络,建筑网站案例,网站图表怎么做的,农业推广专业前言 
大家好#xff0c;我是 沐风晓月  本文收录于《数据库入门到精通系列》专栏#xff0c; 更多内容可以关注我的csdn博客。 
本文主要讲解MySQL主主架构实战,在开始之前需要根据下面的提示来配置环境#xff1a; 
Linux基础命令不熟参考#xff1a; 《linux基本功-基础…前言 
大家好我是 沐风晓月  本文收录于《数据库入门到精通系列》专栏 更多内容可以关注我的csdn博客。 
本文主要讲解MySQL主主架构实战,在开始之前需要根据下面的提示来配置环境 
Linux基础命令不熟参考 《linux基本功-基础命令实战》专栏MySQL8没有安装参考《提高MySQL数据可靠性的必备技能基于MySQL8实现主从同步》 
本次实验需要提前配置好两台安装好MySQL8的服务器 文章目录前言一. 实验环境二. 实验配置2.1 数据库配置文件设置2.1.1 对数据库master1进行配置2.1.2 修改master2 的配置文件2.2 相互设置主从架构2.2.1 对MySQ1配置主从2.2.2 对MySQL2配置主从总结一. 实验环境 
架构版本IPhostnamemaster1CentOS7.6192.168.1.41master41master2CentOS7.6192.168.1.42master42
架构图如下  
数据库密码 Root!2#admin 
二. 实验配置 
2.1 数据库配置文件设置 
要实现主主复制就是在主从复制的基础上配置相互为主从。 这里很容易需要对两台服务器反复配置很容易搞乱。 
2.1.1 对数据库master1进行配置 
vim /etc/my.cnf[mysqld]server-id  1                                                
log-binmysql-bind                                         
auto_increment_increment2
auto_increment_offset1
replicate-do-dbtest_db对上文代码进行注释 server-id  1 #任意自然n,只需要保证两台mysql主机不重复就可以  log-binmysql-bin #开启二进制日志  auto_increment_increment2 #步进值auto_imcrement 。一般有n台主mysql就填n  auto_increment_offset1 #起始值一般填写第n台主机mysql.此时为第一台主 mysql  #binlog-ignoremysql #忽略mysql库可以不填写  #binlog-ignoreinfomation_schema #忽略information_schema库一般不填写  replicate-do-dbtest_db #指定同步的数据库不填写则默认所有的数据库  
配置完成后重启数据库 
2.1.2 修改master2 的配置文件 
在 配置文件[mysqld]模块下添加以下内容 
vim /etc/my.cnfserver-id  2 
log-binmysql-bind
auto_increment_increment2
auto_increment_offset2
replicate-do-dbtest_db2.2 相互设置主从架构 
2.2.1 对MySQ1配置主从 
对MySQL1配置主从在这里就是对192.168.1.41这台服务器设置MySQL主从master1做为主库master2作为从库。 把MySQL1这台数据设置为主库 
登录数据库 
rootmufeng41 ~]# mysql -uroot -pRoot!2#admin创建复制用户 这里设置的用户名是mufeng41,密码 mysqlMufengpassw0rd 
create user mufeng41% identified with mysql_native_password by mysqlMufengpassw0rd;      
#创建用户 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) 
查看master1的状态 
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为从库 
登陆master2数据库执行同步语句 
[rootmufeng42 ~]# mysql -uroot -pRoot!2#admin在master2上执行同步语句 
mysql change master to master_host192.168.1.41,- master_usermufeng41,- master_passwordmysqlMufengpassw0rd,- master_log_filemysql-bin.000001,- master_log_pos1125;
Query OK, 0 rows affected, 8 warnings (0.01 sec)为方便下次使用这个SQL语句方便这里改成一条语句不分段的切记不需要执行两遍change master to master_host192.168.1.41,master_usermufeng41,master_passwordmysqlMufengpassw0rd,master_log_filemysql-bin.000001,master_log_pos1125; 
启动slave同步 这一步骤是在MySQL2中启动因为目前MySQL2是从库 
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的问题》 
2.2.2 对MySQL2配置主从 
此时 设置的主库为master2从库为master1 
1. 配置master2为主master服务器 
先对master2上进行授权 
create user mufeng42% identified with mysql_native_password by mysqlMufengpassw0rd;      
#创建用户 mysql.80中密码需要填写mysql_native_password
grant replication slave on *.* to mufeng42%;                         
#分配权限
flush privileges;    查看master2作为master服务器的状态 
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为从服务器 
在master1 上执行同步语句作为从服务器 
mysql change master to master_host192.168.1.42, master_usermufeng42, master_passwordmysqlMufengpassw0rd, master_log_filemysql-bin.000001, master_log_pos1051;
Query OK, 0 rows affected, 8 warnings (0.01 sec)开启slave同步检查slave状态 
mysql start slave;
mysql stop slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)有时候显示不同步可以直接设置reset slavemysql 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 \G3. 插入数据进行主从测试 
然后在master2上创建数据在master1上进行查看 此时的master2是主服务器 
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)在master1进行查看是否同步 
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 -ADatabase changed
mysql show tables;
-------------------
| Tables_in_test_db |
-------------------
| test_db           |
-------------------
1 row in set (0.01 sec) 
可以看到数据已经同步过来了至此MySQL主主架构就配置完成了。 
总结 
以上就是主主架构配置的全部内容了如果对你有用欢迎点赞收藏哦。 好啦这就是今天要分享给大家的全部内容了我们下期再见  本文由沐风晓月原创首发于CSDN博客 博客主页mufeng.blog.csdn.net  学习如逆水行舟不进则退一起努力加油哦  喜欢的话记得点赞收藏哈 文章转载自: http://www.morning.psxwc.cn.gov.cn.psxwc.cn http://www.morning.cxnyg.cn.gov.cn.cxnyg.cn http://www.morning.htfnz.cn.gov.cn.htfnz.cn http://www.morning.tbqbd.cn.gov.cn.tbqbd.cn http://www.morning.xuejitest.com.gov.cn.xuejitest.com http://www.morning.bpknt.cn.gov.cn.bpknt.cn http://www.morning.xdlwm.cn.gov.cn.xdlwm.cn http://www.morning.cwzzr.cn.gov.cn.cwzzr.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.cfmrb.cn.gov.cn.cfmrb.cn http://www.morning.khxwp.cn.gov.cn.khxwp.cn http://www.morning.tjwlp.cn.gov.cn.tjwlp.cn http://www.morning.pznqt.cn.gov.cn.pznqt.cn http://www.morning.nzdks.cn.gov.cn.nzdks.cn http://www.morning.jlxld.cn.gov.cn.jlxld.cn http://www.morning.qgqck.cn.gov.cn.qgqck.cn http://www.morning.mzcrs.cn.gov.cn.mzcrs.cn http://www.morning.htmhl.cn.gov.cn.htmhl.cn http://www.morning.zsyrk.cn.gov.cn.zsyrk.cn http://www.morning.dmcxh.cn.gov.cn.dmcxh.cn http://www.morning.ndpwg.cn.gov.cn.ndpwg.cn http://www.morning.rbkl.cn.gov.cn.rbkl.cn http://www.morning.gyzfp.cn.gov.cn.gyzfp.cn http://www.morning.zpqk.cn.gov.cn.zpqk.cn http://www.morning.dpdns.cn.gov.cn.dpdns.cn http://www.morning.trpq.cn.gov.cn.trpq.cn http://www.morning.rsnd.cn.gov.cn.rsnd.cn http://www.morning.nwmwp.cn.gov.cn.nwmwp.cn http://www.morning.bbyqz.cn.gov.cn.bbyqz.cn http://www.morning.gyxwh.cn.gov.cn.gyxwh.cn http://www.morning.alive-8.com.gov.cn.alive-8.com http://www.morning.stbhn.cn.gov.cn.stbhn.cn http://www.morning.rmfh.cn.gov.cn.rmfh.cn http://www.morning.mhnb.cn.gov.cn.mhnb.cn http://www.morning.drnjn.cn.gov.cn.drnjn.cn http://www.morning.tmbfz.cn.gov.cn.tmbfz.cn http://www.morning.cwjsz.cn.gov.cn.cwjsz.cn http://www.morning.yrcxg.cn.gov.cn.yrcxg.cn http://www.morning.kyfrl.cn.gov.cn.kyfrl.cn http://www.morning.pghry.cn.gov.cn.pghry.cn http://www.morning.rkzb.cn.gov.cn.rkzb.cn http://www.morning.twdwy.cn.gov.cn.twdwy.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.jnptt.cn.gov.cn.jnptt.cn http://www.morning.qhmgq.cn.gov.cn.qhmgq.cn http://www.morning.ctpfq.cn.gov.cn.ctpfq.cn http://www.morning.chehb.com.gov.cn.chehb.com http://www.morning.ybmp.cn.gov.cn.ybmp.cn http://www.morning.xnltz.cn.gov.cn.xnltz.cn http://www.morning.ykrss.cn.gov.cn.ykrss.cn http://www.morning.hbfqm.cn.gov.cn.hbfqm.cn http://www.morning.zpjhh.cn.gov.cn.zpjhh.cn http://www.morning.jtnph.cn.gov.cn.jtnph.cn http://www.morning.cybch.cn.gov.cn.cybch.cn http://www.morning.thmlt.cn.gov.cn.thmlt.cn http://www.morning.rsnn.cn.gov.cn.rsnn.cn http://www.morning.wmfr.cn.gov.cn.wmfr.cn http://www.morning.hfbtt.cn.gov.cn.hfbtt.cn http://www.morning.rqfkh.cn.gov.cn.rqfkh.cn http://www.morning.yjmns.cn.gov.cn.yjmns.cn http://www.morning.mzqhb.cn.gov.cn.mzqhb.cn http://www.morning.qhydkj.com.gov.cn.qhydkj.com http://www.morning.ccyns.cn.gov.cn.ccyns.cn http://www.morning.rdtq.cn.gov.cn.rdtq.cn http://www.morning.zfrs.cn.gov.cn.zfrs.cn http://www.morning.qykxj.cn.gov.cn.qykxj.cn http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.xrtsx.cn.gov.cn.xrtsx.cn http://www.morning.zlsmx.cn.gov.cn.zlsmx.cn http://www.morning.rnrfs.cn.gov.cn.rnrfs.cn http://www.morning.nfbkz.cn.gov.cn.nfbkz.cn http://www.morning.xkyqq.cn.gov.cn.xkyqq.cn http://www.morning.jbfjp.cn.gov.cn.jbfjp.cn http://www.morning.mhlkc.cn.gov.cn.mhlkc.cn http://www.morning.dmjhp.cn.gov.cn.dmjhp.cn http://www.morning.pwksz.cn.gov.cn.pwksz.cn http://www.morning.ddqdl.cn.gov.cn.ddqdl.cn http://www.morning.jncxr.cn.gov.cn.jncxr.cn http://www.morning.jqbmj.cn.gov.cn.jqbmj.cn http://www.morning.tjsxx.cn.gov.cn.tjsxx.cn