Linux下的MYSQL主主复制的用法
这篇文章主要讲解了“Linux下的MYSQL主主复制的用法”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Linux下的MYSQL主主复制的用法”吧!
为什么,会有mysql的主主复制。因为在一些高可用的环境中,mysql的主从不能满足现实中的一些实际需求。比如,一些流量大的网站数据库访问有了瓶颈,需要负载均衡的时候就用两个或者多个的mysql服务器,而这些mysql服务器的数据库数据必须要保持一致,那么就会用到主主复制。
mysql主从架构中其实就一个主在工作,而从就相当于一个备份机器,从通过日志监测的方式来备份主库上的数据而保证主库的数据安全。在这种架构中如果从上的数据做了改变,主数据是不会用任何变化的。因为mysql主从架构主要是mysql从监控mysql主的日志变化来实现同步,相反的在这个架构中主并没有监控从的日志变化。所以,mysql从数据反生变化,主也就没有什么变化了。
通过上述描述,可以看到如果想实现主主复制,无非就是在mysql主从架构上让mysql主实现监测从的日志变化,从而实现两台机器相互同步。(主从的架构前面有博文http://duyunlong.blog.51cto.com/1054716/1102237)
实验环境:两台服务器:
主机名:HA1,HA2(呵呵,这个主机名是英文缩写High availability,高可用的意思)
ip:192.168.1.231
192.168.1.232
主机系统:centos6.4
mysql版本5.5.22
首先,看下HA1(192.168.1.231)的mysql配置文件
vim/etc/my.cnf#ExampleMySQLconfigfileforverylargesystems.##Thisisforalargesystemwithmemoryof1G-2Gwherethesystemrunsmainly#MySQL.##MySQLprogramslookforoptionfilesinasetof#locationswhichdependonthedeploymentplatform.#Youcancopythisoptionfiletooneofthose#locations.Forinformationabouttheselocations,see:#http://dev.mysql.com/doc/mysql/en/option-files.html##Inthisfile,youcanusealllongoptionsthataprogramsupports.#Ifyouwanttoknowwhichoptionsaprogramsupports,runtheprogram#withthe"--help"option.#ThefollowingoptionswillbepassedtoallMySQLclients[client]#password=your_passwordport=3306socket=/usr/local/mysql/tmp/mysql.sock#Herefollowsentriesforsomespecificprograms#TheMySQLserver[mysqld]port=3306socket=/usr/local/mysql/tmp/mysql.sockskip-external-lockingkey_buffer_size=384Mmax_allowed_packet=1Mtable_open_cache=512sort_buffer_size=2Mread_buffer_size=2Mread_rnd_buffer_size=8Mmyisam_sort_buffer_size=64Mthread_cache_size=8query_cache_size=32M#TrynumberofCPU's*2forthread_concurrencythread_concurrency=8#Don'tlistenonaTCP/IPportatall.Thiscanbeasecurityenhancement,#ifallprocessesthatneedtoconnecttomysqldrunonthesamehost.#AllinteractionwithmysqldmustbemadeviaUnixsocketsornamedpipes.#NotethatusingthisoptionwithoutenablingnamedpipesonWindows#(viathe"enable-named-pipe"option)willrendermysqlduseless!##skip-networking#ReplicationMasterServer(default)#binaryloggingisrequiredforreplicationlog-bin=mysql-binlog-slave-updates#requireduniqueidbetween1and2^32-1#defaultsto1ifmaster-hostisnotset#butwillnotfunctionasamasterifomittedserver-id=1
在这个配置文件中,需要特别注意的三处地方:
log-bin=mysql-bin:这个选项基本默认都是开着的,如果没有打开,可以手动打开。
log-slave-updates:这个选项特别的重要它是为了让slave也能充当master,同时也为了更好的服务于 m-m + s 的环境,保证slave挂在任何一台master上都会接收到另一个master的写入信息。当然不局限于这个架构,级联复制的架构同样也需要log-slave-updates的支持。
server-id = 1:这个ID为服务器ID如果配置一样会出现冲突,而不能复制
接着再看下HA2(192.168.1.232)的mysql配置文件
vim/etc/my.cnf#ExampleMySQLconfigfileforverylargesystems.##Thisisforalargesystemwithmemoryof1G-2Gwherethesystemrunsmainly#MySQL.##MySQLprogramslookforoptionfilesinasetof#locationswhichdependonthedeploymentplatform.#Youcancopythisoptionfiletooneofthose#locations.Forinformationabouttheselocations,see:#http://dev.mysql.com/doc/mysql/en/option-files.html##Inthisfile,youcanusealllongoptionsthataprogramsupports.#Ifyouwanttoknowwhichoptionsaprogramsupports,runtheprogram#withthe"--help"option.#ThefollowingoptionswillbepassedtoallMySQLclients[client]#password=your_passwordport=3306socket=/usr/local/mysql/tmp/mysql.sock#Herefollowsentriesforsomespecificprograms#TheMySQLserver[mysqld]port=3306socket=/usr/local/mysql/tmp/mysql.sockskip-external-lockingkey_buffer_size=384Mmax_allowed_packet=1Mtable_open_cache=512sort_buffer_size=2Mread_buffer_size=2Mread_rnd_buffer_size=8Mmyisam_sort_buffer_size=64Mthread_cache_size=8query_cache_size=32M#TrynumberofCPU's*2forthread_concurrencythread_concurrency=8#Don'tlistenonaTCP/IPportatall.Thiscanbeasecurityenhancement,#ifallprocessesthatneedtoconnecttomysqldrunonthesamehost.#AllinteractionwithmysqldmustbemadeviaUnixsocketsornamedpipes.#NotethatusingthisoptionwithoutenablingnamedpipesonWindows#(viathe"enable-named-pipe"option)willrendermysqlduseless!##skip-networking#ReplicationMasterServer(default)#binaryloggingisrequiredforreplicationlog-bin=mysql-binlog-slave-updates#requireduniqueidbetween1and2^32-1#defaultsto1ifmaster-hostisnotset#butwillnotfunctionasamasterifomittedserver-id=10#ReplicationSlave(commentoutmastersectiontousethis)
在HA2的mysql配置文件中,除了server-id不一样,其他几乎一模一样。配置文件写好后,我们把两台服务器上的mysql服务器启动起来。
首先,登录HA2(192.168.1.232)的mysql中,查看master状态
mysql>showmasterstatus;+------------------+----------+--------------+------------------+|File|Position|Binlog_Do_DB|Binlog_Ignore_DB|+------------------+----------+--------------+------------------+|mysql-bin.000016|615|||+------------------+----------+--------------+------------------+1rowinset(0.00sec)mysql>
然后,登录HA1(192.168.1.231)的msyql中,把HA2配置成自己的主,在做这个之前先在两台机器的mysql中建立一个可以复制用的帐号:
mysql>grantallon*.*toduyunlong@'192.168.1.%'identifiedby'123456';QueryOK,0rowsaffected(0.01sec)mysql>changemastertomaster_host='192.168.1.232',master_user='duyunlong',master_password='123456',master_log_file='mysql-bin.000016',master_log_pos=615;
同上,查看HA1(192.168.1.231)master,然后登录HA2(192.168.1.232),把HA1(192.168.1.231),配置成自己的主,然后分别在两台机器的mysql中,启动slave
启动后HA1状态
mysql>showslavestatus\G;***************************1.row***************************Slave_IO_State:WaitingformastertosendeventMaster_Host:192.168.1.232Master_User:duyunlongMaster_Port:3306Connect_Retry:60Master_Log_File:mysql-bin.000016Read_Master_Log_Pos:615Relay_Log_File:HA1-relay-bin.000002Relay_Log_Pos:346Relay_Master_Log_File:mysql-bin.000016Slave_IO_Running:YesSlave_SQL_Running:YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno:0Last_Error:Skip_Counter:0Exec_Master_Log_Pos:615Relay_Log_Space:500Until_Condition:NoneUntil_Log_File:Until_Log_Pos:0Master_SSL_Allowed:NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master:0Master_SSL_Verify_Server_Cert:NoLast_IO_Errno:0Last_IO_Error:Last_SQL_Errno:0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id:101rowinset(0.00sec)ERROR:Noqueryspecified
可以看到Slave_IO_Running:Yes
Slave_SQL_Running:Yes
然后在看HA2的状态:
mysql>showslavestatus\G;***************************1.row***************************Slave_IO_State:WaitingformastertosendeventMaster_Host:192.168.1.231Master_User:duyunlongMaster_Port:3306Connect_Retry:60Master_Log_File:mysql-bin.000018Read_Master_Log_Pos:552Relay_Log_File:HA2-relay-bin.000002Relay_Log_Pos:441Relay_Master_Log_File:mysql-bin.000018Slave_IO_Running:YesSlave_SQL_Running:YesReplicate_Do_DB:Replicate_Ignore_DB:Replicate_Do_Table:Replicate_Ignore_Table:Replicate_Wild_Do_Table:Replicate_Wild_Ignore_Table:Last_Errno:0Last_Error:Skip_Counter:0Exec_Master_Log_Pos:552Relay_Log_Space:595Until_Condition:NoneUntil_Log_File:Until_Log_Pos:0Master_SSL_Allowed:NoMaster_SSL_CA_File:Master_SSL_CA_Path:Master_SSL_Cert:Master_SSL_Cipher:Master_SSL_Key:Seconds_Behind_Master:0Master_SSL_Verify_Server_Cert:NoLast_IO_Errno:0Last_IO_Error:Last_SQL_Errno:0Last_SQL_Error:Replicate_Ignore_Server_Ids:Master_Server_Id:11rowinset(0.00sec)ERROR:Noqueryspecified
可以看到Slave_IO_Running:Yes
Slave_SQL_Running:Yes
接下来,我们要测试,是不是已经可以主主复制了呢,首先登录HA1(192.168.1.231)的mysql中,建立一数据库,当然在测试前我们先看下,两台服务器中的mysql中有哪些数据
首先看下HA1(192.168.1.231)
[root@HA1~]#mysqlmysql-uduyunlong-p123456-h292.168.1.231-e'showdatabases;'+--------------------+|Database|+--------------------+|information_schema||mysql||performance_schema||test||wanghaipeng|+--------------------+[root@HA1~]#
再看下HA2(192.168.1.232)
[root@HA2~]#mysqlmysql-uduyunlong-p123456-h292.168.1.232-e'showdatabases;'+--------------------+|Database|+--------------------+|information_schema||mysql||performance_schema||test||wanghaipeng|+--------------------+[root@HA2~]#
可以看到,现在两台服务器上的mysql中数据是一样的,接下来在HA中建立一数据库“a”,再看结果
[root@HA1~]#mysqlmysql-uduyunlong-p123456-h292.168.1.231-e'createdatabasea;'[root@HA1~]#mysqlmysql-uduyunlong-p123456-h292.168.1.231-e'showdatabases;'+--------------------+|Database|+--------------------+|information_schema||a||mysql||performance_schema||test||wanghaipeng|+--------------------+[root@HA1~]#
然后看下HA2(192.168.1.232)是不是会把刚建立的数据库“a”复制过来
[root@HA2~]#mysqlmysql-uduyunlong-p123456-h292.168.1.232-e'showdatabases;'+--------------------+|Database|+--------------------+|information_schema||a||mysql||performance_schema||test||wanghaipeng|+--------------------+
可以看到,数据库“a”已经成功复制过来了,反过来我们在HA2(192.168.1.232)上建立一数据库“b”看是否HA1也可以复制过去
[root@HA2~]#mysqlmysql-uduyunlong-p123456-h292.168.1.232-e'createdatabaseb;'[root@HA2~]#mysqlmysql-uduyunlong-p123456-h292.168.1.232-e'showdatabases;'+--------------------+|Database|+--------------------+|information_schema||a||b||mysql||performance_schema||test||wanghaipeng|+--------------------+[root@HA2~]#
然后登录HA1(192.168.1.231),查看是否复制成功
[root@HA1~]#mysqlmysql-uduyunlong-p123456-h292.168.1.231-e'showdatabases;'+--------------------+|Database|+--------------------+|information_schema||a||b||mysql||performance_schema||test||wanghaipeng|+--------------------+[root@HA1~]#
在HA1(192.168.1.231)可以看到数据库“b”已经复制过来了。
那么到此,主主复制架构已经成功!
感谢各位的阅读,以上就是“Linux下的MYSQL主主复制的用法”的内容了,经过本文的学习后,相信大家对Linux下的MYSQL主主复制的用法这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。