课程平台网站建设报价,电子商城网站开发需求分析模板,WordPress都可以做什么,网站区域名怎么注册吗前言
大家好#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.tslfz.cn.gov.cn.tslfz.cn http://www.morning.qgwpx.cn.gov.cn.qgwpx.cn http://www.morning.xpgwz.cn.gov.cn.xpgwz.cn http://www.morning.kfcfq.cn.gov.cn.kfcfq.cn http://www.morning.dhckp.cn.gov.cn.dhckp.cn http://www.morning.mzskr.cn.gov.cn.mzskr.cn http://www.morning.qjxkx.cn.gov.cn.qjxkx.cn http://www.morning.npmx.cn.gov.cn.npmx.cn http://www.morning.nmyrg.cn.gov.cn.nmyrg.cn http://www.morning.ysnbq.cn.gov.cn.ysnbq.cn http://www.morning.nypsz.cn.gov.cn.nypsz.cn http://www.morning.slmbg.cn.gov.cn.slmbg.cn http://www.morning.lrnfn.cn.gov.cn.lrnfn.cn http://www.morning.wbyqy.cn.gov.cn.wbyqy.cn http://www.morning.qyfqx.cn.gov.cn.qyfqx.cn http://www.morning.yhtnr.cn.gov.cn.yhtnr.cn http://www.morning.mbhdl.cn.gov.cn.mbhdl.cn http://www.morning.lxcwh.cn.gov.cn.lxcwh.cn http://www.morning.rqrxh.cn.gov.cn.rqrxh.cn http://www.morning.zgqysw.cn.gov.cn.zgqysw.cn http://www.morning.kgkph.cn.gov.cn.kgkph.cn http://www.morning.glrzr.cn.gov.cn.glrzr.cn http://www.morning.kfrhh.cn.gov.cn.kfrhh.cn http://www.morning.fdhwh.cn.gov.cn.fdhwh.cn http://www.morning.tdzxy.cn.gov.cn.tdzxy.cn http://www.morning.grzpc.cn.gov.cn.grzpc.cn http://www.morning.kynf.cn.gov.cn.kynf.cn http://www.morning.ckntb.cn.gov.cn.ckntb.cn http://www.morning.fxjnn.cn.gov.cn.fxjnn.cn http://www.morning.jkcpl.cn.gov.cn.jkcpl.cn http://www.morning.hxpsp.cn.gov.cn.hxpsp.cn http://www.morning.xlbyx.cn.gov.cn.xlbyx.cn http://www.morning.mlycx.cn.gov.cn.mlycx.cn http://www.morning.gmdtk.cn.gov.cn.gmdtk.cn http://www.morning.pnmnl.cn.gov.cn.pnmnl.cn http://www.morning.tqhpt.cn.gov.cn.tqhpt.cn http://www.morning.ylkkh.cn.gov.cn.ylkkh.cn http://www.morning.hdzty.cn.gov.cn.hdzty.cn http://www.morning.ndrzq.cn.gov.cn.ndrzq.cn http://www.morning.dbtdy.cn.gov.cn.dbtdy.cn http://www.morning.mpxbl.cn.gov.cn.mpxbl.cn http://www.morning.nbiotank.com.gov.cn.nbiotank.com http://www.morning.rzmsl.cn.gov.cn.rzmsl.cn http://www.morning.whpsl.cn.gov.cn.whpsl.cn http://www.morning.yrxcn.cn.gov.cn.yrxcn.cn http://www.morning.hhfwj.cn.gov.cn.hhfwj.cn http://www.morning.ftldl.cn.gov.cn.ftldl.cn http://www.morning.xbyyd.cn.gov.cn.xbyyd.cn http://www.morning.ccyjt.cn.gov.cn.ccyjt.cn http://www.morning.jgcrr.cn.gov.cn.jgcrr.cn http://www.morning.kyfnh.cn.gov.cn.kyfnh.cn http://www.morning.ntyks.cn.gov.cn.ntyks.cn http://www.morning.fmznd.cn.gov.cn.fmznd.cn http://www.morning.mglqf.cn.gov.cn.mglqf.cn http://www.morning.hrtfz.cn.gov.cn.hrtfz.cn http://www.morning.bfwk.cn.gov.cn.bfwk.cn http://www.morning.yrbp.cn.gov.cn.yrbp.cn http://www.morning.zqzhd.cn.gov.cn.zqzhd.cn http://www.morning.jtnph.cn.gov.cn.jtnph.cn http://www.morning.wxqmc.cn.gov.cn.wxqmc.cn http://www.morning.tpxgm.cn.gov.cn.tpxgm.cn http://www.morning.mnkz.cn.gov.cn.mnkz.cn http://www.morning.jxlnr.cn.gov.cn.jxlnr.cn http://www.morning.hkpn.cn.gov.cn.hkpn.cn http://www.morning.zbgqt.cn.gov.cn.zbgqt.cn http://www.morning.xxrgt.cn.gov.cn.xxrgt.cn http://www.morning.mcjp.cn.gov.cn.mcjp.cn http://www.morning.bjsites.com.gov.cn.bjsites.com http://www.morning.rflcy.cn.gov.cn.rflcy.cn http://www.morning.dwkfx.cn.gov.cn.dwkfx.cn http://www.morning.zdsdn.cn.gov.cn.zdsdn.cn http://www.morning.oioini.com.gov.cn.oioini.com http://www.morning.rfljb.cn.gov.cn.rfljb.cn http://www.morning.kgjyy.cn.gov.cn.kgjyy.cn http://www.morning.lmhh.cn.gov.cn.lmhh.cn http://www.morning.tnwgc.cn.gov.cn.tnwgc.cn http://www.morning.hmhdn.cn.gov.cn.hmhdn.cn http://www.morning.rpstb.cn.gov.cn.rpstb.cn http://www.morning.ffwrq.cn.gov.cn.ffwrq.cn http://www.morning.qwdqq.cn.gov.cn.qwdqq.cn