为什么,会有mysql的主主复制。因为在一些高可用的环境中,mysql的主从不能满足现实中的一些实际需求。比如,一些流量大的网站数据库访问有了瓶颈,需要负载均衡的时候就用两个或者多个的mysql服务器,而这些mysql服务器的数据库数据必须要保持一致,那么就会用到主主复制。
mysql主从架构中其实就一个主在工作,而从就相当于一个备份机器,从通过日志监测的方式来备份主库上的数据而保证主库的数据安全。在这种架构中如果从上的数据做了改变,主数据是不会用任何变化的。因为mysql主从架构主要是mysql从监控mysql主的日志变化来实现同步,相反的在这个架构中主并没有监控从的日志变化。所以,mysql从数据反生变化,主也就没有什么变化了。
通过上述描述,可以看到如果想实现主主复制,无非就是在mysql主从架构上让mysql主实现监测从的日志变化,从而实现两台机器相互同步。(主从的架构前面有博文 http://www.linuxidc.com/Linux/2013-10/91682.htm )
实验环境:两台服务器:
主机名: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 
# Example MySQL config file for very large systems. 

# This is for a large system with memory of 1G-2G where the system runs mainly 
# MySQL. 

# MySQL programs look for option files in a set of 
# locations which depend on the deployment platform. 
# You can copy this option file to one of those 
# locations. For information about these locations, see: 
# http://dev.mysql.com/doc/mysql/en/option-files.html 

# In this file, you can use all long options that a program supports. 
# If you want to know which options a program supports, run the program 
# with the "--help" option. 
# The following options will be passed to all MySQL clients 
[client] 
#password      = your_password 
port            = 3306
socket          = /usr/local/mysql/tmp/mysql.sock 
# Here follows entries for some specific programs 
# The MySQL server 
[mysqld] 
port            = 3306
socket          = /usr/local/mysql/tmp/mysql.sock 
skip-external-locking 
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
# Try number of CPU's*2 for thread_concurrency 
thread_concurrency = 8
# Don't listen on a TCP/IP port at all. This can be a security enhancement, 
# if all processes that need to connect to mysqld run on the same host. 
# All interaction with mysqld must be made via Unix sockets or named pipes. 
# Note that using this option without enabling named pipes on Windows 
# (via the "enable-named-pipe" option) will render mysqld useless! 

#skip-networking 
# Replication Master Server (default) 
# binary logging is required for replication 
log-bin=mysql-bin
log-slave-updates 
# required unique id between 1 and 2^32 - 1 
# defaults to 1 if master-host is not set 
# but will not function as a master if omitted 
server-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 
# Example MySQL config file for very large systems. 

# This is for a large system with memory of 1G-2G where the system runs mainly 
# MySQL. 

# MySQL programs look for option files in a set of 
# locations which depend on the deployment platform. 
# You can copy this option file to one of those 
# locations. For information about these locations, see: 
# http://dev.mysql.com/doc/mysql/en/option-files.html 

# In this file, you can use all long options that a program supports. 
# If you want to know which options a program supports, run the program 
# with the "--help" option. 
# The following options will be passed to all MySQL clients 
[client] 
#password      = your_password 
port            = 3306
socket          = /usr/local/mysql/tmp/mysql.sock 
# Here follows entries for some specific programs 
# The MySQL server 
[mysqld] 
port            = 3306
socket          = /usr/local/mysql/tmp/mysql.sock 
skip-external-locking 
key_buffer_size = 384M
max_allowed_packet = 1M
table_open_cache = 512
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 8M
myisam_sort_buffer_size = 64M
thread_cache_size = 8
query_cache_size = 32M
# Try number of CPU's*2 for thread_concurrency 
thread_concurrency = 8
# Don't listen on a TCP/IP port at all. This can be a security enhancement, 
# if all processes that need to connect to mysqld run on the same host. 
# All interaction with mysqld must be made via Unix sockets or named pipes. 
# Note that using this option without enabling named pipes on Windows 
# (via the "enable-named-pipe" option) will render mysqld useless! 

#skip-networking 
# Replication Master Server (default) 
# binary logging is required for replication 
log-bin=mysql-bin
log-slave-updates 
# required unique id between 1 and 2^32 - 1 
# defaults to 1 if master-host is not set 
# but will not function as a master if omitted 
server-id      = 10
# Replication Slave (comment out master section to use this)

在HA2的mysql配置文件中,除了server-id不一样,其他几乎一模一样。配置文件写好后,我们把两台服务器上的mysql服务器启动起来。

首先,登录HA2(192.168.1.232)的mysql中,查看master状态
mysql> show master status; 
+------------------+----------+--------------+------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | 
+------------------+----------+--------------+------------------+
| mysql-bin.000016 |      615 |              |                  | 
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec) 
mysql>

然后,登录HA1(192.168.1.231)的msyql中,把HA2配置成自己的主,在做这个之前先在两台机器的mysql中建立一个可以复制用的帐号:
mysql>grant all on *.* to duyunlong@'192.168.1.%' identified by '123456'; 
Query OK, 0 rows affected (0.01 sec) 
mysql>change master to master_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> show slave status \G; 
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event 
                  Master_Host: 192.168.1.232
                  Master_User: duyunlong 
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000016
          Read_Master_Log_Pos: 615
              Relay_Log_File: HA1-relay-bin.000002
                Relay_Log_Pos: 346
        Relay_Master_Log_File: mysql-bin.000016
            Slave_IO_Running: Yes 
            Slave_SQL_Running: Yes 
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
          Replicate_Do_Table: 
      Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                  Last_Errno: 0
                  Last_Error: 
                Skip_Counter: 0
          Exec_Master_Log_Pos: 615
              Relay_Log_Space: 500
              Until_Condition: None
              Until_Log_File: 
                Until_Log_Pos: 0
          Master_SSL_Allowed: No 
          Master_SSL_CA_File: 
          Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
              Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No 
                Last_IO_Errno: 0
                Last_IO_Error: 
              Last_SQL_Errno: 0
              Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
            Master_Server_Id: 10
1 row in set (0.00 sec) 
ERROR: 
No query specified

可以看到 Slave_IO_Running: Yes
Slave_SQL_Running: Yes
然后在看HA2的状态:
mysql> show slave status \G; 
*************************** 1. row ***************************
              Slave_IO_State: Waiting for master to send event 
                  Master_Host: 192.168.1.231
                  Master_User: duyunlong 
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000018
          Read_Master_Log_Pos: 552
              Relay_Log_File: HA2-relay-bin.000002
                Relay_Log_Pos: 441
        Relay_Master_Log_File: mysql-bin.000018
            Slave_IO_Running: Yes 
            Slave_SQL_Running: Yes 
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
          Replicate_Do_Table: 
      Replicate_Ignore_Table: 
      Replicate_Wild_Do_Table: 
  Replicate_Wild_Ignore_Table: 
                  Last_Errno: 0
                  Last_Error: 
                Skip_Counter: 0
          Exec_Master_Log_Pos: 552
              Relay_Log_Space: 595
              Until_Condition: None
              Until_Log_File: 
                Until_Log_Pos: 0
          Master_SSL_Allowed: No 
          Master_SSL_CA_File: 
          Master_SSL_CA_Path: 
              Master_SSL_Cert: 
            Master_SSL_Cipher: 
              Master_SSL_Key: 
        Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: No 
                Last_IO_Errno: 0
                Last_IO_Error: 
              Last_SQL_Errno: 0
              Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
            Master_Server_Id: 1
1 row in set (0.00 sec) 
ERROR: 
No query specified

可以看到Slave_IO_Running: Yes
Slave_SQL_Running: Yes
接下来,我们要测试,是不是已经可以主主复制了呢,首先登录HA1(192.168.1.231)的mysql中,建立一数据库,当然在测试前我们先看下,两台服务器中的mysql中有哪些数据
首先看下HA1(192.168.1.231)
[root@HA1 ~]# mysql mysql -uduyunlong -p123456 -h192.168.1.231 -e 'show databases;' 
+--------------------+
| Database          | 
+--------------------+
| information_schema | 
| mysql              | 
| performance_schema | 
| test              | 
| wanghaipeng        | 
+--------------------+
[root@HA1 ~]#

再看下HA2(192.168.1.232)
[root@HA2 ~]# mysql mysql -uduyunlong -p123456 -h192.168.1.232 -e 'show databases;' 
+--------------------+
| Database          | 
+--------------------+
| information_schema | 
| mysql              | 
| performance_schema | 
| test              | 
| wanghaipeng        | 
+--------------------+
[root@HA2 ~]#

可以看到,现在两台服务器上的mysql中数据是一样的,接下来在HA中建立一数据库“a”,再看结果
[root@HA1 ~]# mysql mysql -uduyunlong -p123456 -h192.168.1.231 -e 'create database a;' 
[root@HA1 ~]# mysql mysql -uduyunlong -p123456 -h192.168.1.231 -e 'show databases;' 
+--------------------+
| Database          | 
+--------------------+
| information_schema | 
| a                  | 
| mysql              | 
| performance_schema | 
| test              | 
| wanghaipeng        | 
+--------------------+
[root@HA1 ~]#

然后看下HA2(192.168.1.232)是不是会把刚建立的数据库“a”复制过来
[root@HA2 ~]# mysql mysql -uduyunlong -p123456 -h192.168.1.232 -e 'show databases;' 
+--------------------+
| Database          | 
+--------------------+
| information_schema | 
| a                  | 
| mysql              | 
| performance_schema | 
| test              | 
| wanghaipeng        | 
+--------------------+

可以看到,数据库“a”已经成功复制过来了,反过来我们在HA2(192.168.1.232)上建立一数据库“b”看是否HA1也可以复制过去
[root@HA2 ~]# mysql mysql -uduyunlong -p123456 -h192.168.1.232 -e 'create database b;' 
[root@HA2 ~]# mysql mysql -uduyunlong -p123456 -h192.168.1.232 -e 'show databases;' 
+--------------------+
| Database          | 
+--------------------+
| information_schema | 
| a                  | 
| b                  | 
| mysql              | 
| performance_schema | 
| test              | 
| wanghaipeng        | 
+--------------------+
[root@HA2 ~]#

然后登录HA1(192.168.1.231),查看是否复制成功
[root@HA1 ~]# mysql mysql -uduyunlong -p123456 -h192.168.1.231 -e 'show databases;' 
+--------------------+
| Database          | 
+--------------------+
| information_schema | 
| a                  | 
| b                  | 
| mysql              | 
| performance_schema | 
| test              | 
| wanghaipeng        | 
+--------------------+
[root@HA1 ~]#

在HA1(192.168.1.231)可以看到数据库“b”已经复制过来了。
那么到此,主主复制架构已经陈功!

Linux下的MySQL主主复制的更多相关文章

  1. Linux下的Mysql的主从备份

    MySQL复制概述 MySQL数据库支持同步复制.单向.异步复制,在复制的过程中一个服务器充当主服务,而一个或多个服务器充当从服务器.主服务器将更新写入二进制日志文件,并维护文件的一个索引以跟踪日志循 ...

  2. Linux下的MySQL主从同步

    网上一些关于Linux下的MySQL主从同步教程非常之多,有些很简单的配置却弄的非常复杂,有些根本无法配通,下面是我通过简单的配置完成的主从同步过程,大家可以参考,此文章更适用于新手. 一.测试环境: ...

  3. Linux 下一个 Mysql error 2002 错误解决

    Linux 下一个 Mysql error 2002 错误解决     首先查看 /etc/rc.d/init.d/mysqld status 查看mysql它已开始.     假设启动的的话,先将数 ...

  4. linux下,MySQL默认的数据文档存储目录为/var/lib/mysql。

    0.说明 Linux下更改yum默认安装的mysql路径datadir. linux下,MySQL默认的数据文档存储目录为/var/lib/mysql. 假如要把MySQL目录移到/home/data ...

  5. Linux下安装mysql(2) 及常见问题解决(CentOS)

    上一篇讲了基本的安装,这篇姑且算作进阶吧 链接Linux下安装mysql(1) 1.准备好mysql的rpm安装包 2.解压并进入usr/local/mysql 3.先执行useradd mysql( ...

  6. Linux下查看mysql、apache是否安装,安装,卸载等操作

    Linux下查看mysql.apache是否安装,并卸载. 指令 ps -ef|grep mysql 得出结果 root               ?        :: /bin/sh /usr/ ...

  7. Linux下查看MySQL的安装路径

    Linux下查看mysql.apache是否安装,并卸载. 指令 ps -ef|grep mysql 得出结果 root               ?        :: /bin/sh /usr/ ...

  8. 【云服务器部署】---Linux下安装MySQL

    [云服务器部署]---Linux下安装MySQL 有关如何阿里云ECS建网站,推荐一片文章,我是是通过这篇文章安装tomcat和jdk的 网址:阿里云ECS建网站(建站)超详细全套完整图文教程! 注意 ...

  9. Linux下将MySQL服务添加到服务器的系统服务中

    Linux下将MySQL服务添加到服务器的系统服务中 Linux环境下将MySQL服务添加到服务器的系统服务中 1.了解MySQL程序路径 MySQL数据目录: /home/mysql/dataMyS ...

  10. Linux下安装mysql(1)(CentOS)

    标题是(1)也就是说这次是基础安装,这种方式安装,没有组的创建,权限管理,配置文件更改等,仅仅是最基本的安装,适合第一次在linux上安装mysql的新手 1.准备好安装包(Linux-Generic ...

随机推荐

  1. jsp:include 动作指令 与 include 指令

    include动作指令可以在JSP页面中动态包含一个文件,这与include指令不同,前者可以动态包含一个文件,文件的内容可以是静态的文件也可以是动态的脚本,而且当包含的动态文件被修改的时候JSP引擎 ...

  2. EntityFramework:EF Migrations Command Reference

    Entity Framework Migrations are handled from the package manager console in Visual Studio. The usage ...

  3. faked 一个用于 mock 后端 API 的轻量工具

    一.简介 faked 是一个在前端开发中用于 mock 服务端接口的模块,轻量简单,无需要在本地启动 Server 也无需其它更多的资源,仅在浏览器中完成「请求拉截」,配合完整的「路由系统」轻而易举的 ...

  4. iOS:三种数据库的小总结

    三种数据库总结:sqlite.FMDB.CoreData   1.sqlite数据库(C语言)需要方法和属性:  (1)数据类型: –INTEGER 有符号的整数类型 –REAL 浮点类型 –TEXT ...

  5. C 语言高效编程的几招——A few action of efficient C language programming

    编写高效简洁的C 语言代码,是许多软件工程师追求的目标.本文就工作中的一些体会和经验做相关的阐述,不对的地方请各位指教. 第1 招:以空间换时间 计算机程序中最大的矛盾是空间和时间的矛盾,那么,从这个 ...

  6. java中读取配置文件中的数据

    1.先在项目中创建一个包(如:config),再创建一个配置文件(如:a.properties),添加配置信息如下:比如:name=kakaage=28 2.代码:import java.io.IOE ...

  7. IOS NSNotification Center 通知中心的使用

    通知中心,它是IOS程序内部的一种消息广播机制,通过它,可以实现无引用关系的对象之间的通信.通知中心他是基于观察者模式,它只能进行程序内部通信,不能跨应用程序进程通信.当通知中心接受到消息后会根据设置 ...

  8. 【Android】Activity 生命周期具体解释

    与其它编程模式不同,android中的Activity没有main()函数.我们无法决定Activity的创建和销毁过程,Activiy的创建和销毁(即生命周期)由系统完毕,系统会在Activity的 ...

  9. android 开源组件合集-UI篇(2013-11-07更新)

    其实也算不上合集,只是将我经常用到的部分整理一下,如果您有好东西,也可以留言补充 1.actionbar http://actionbarsherlock.com/ https://github.co ...

  10. 算法笔记_022:字符串的旋转(Java)

    目录 1 问题描述 2 解决方案 2.1 蛮力移位 2.2 三步反转 1 问题描述 给定一个字符串,要求将字符串前面的若干个字符移到字符串的尾部.例如,将字符串“abcdef”的前3个字符‘a’.‘b ...