Apr 26

[原]搭建Master-Master Mysql Replication 集群 晴

linuxing , 18:24 , 数据库 » Mysql , 评论(0) , 引用(0) , 阅读(66901) , Via 本站原创 | |
    某项目使用Mysql,两台服务器(不带盘柜),要满足高可用需求。一种方式是采用镜像磁盘,另一种方式可考虑构建主备(Master - Slave)或主主(Master - Master)形式的Mysql 集群。这里描述的是第二种方式。

一、前提条件
Mysql 可构建多种集群:负载均衡、主备、主主形式等。这里暂不考虑负载均衡。
而主备环境中,故障发生后,重新构建集群需进行人工干预;主主环境能较好的避免该问题。从原理上看,Mysql 主备、主主集群都是基于日志的复制机制,所以是一样的。不同地方只在于,该机器在集群中同时扮演主和备两个身份。因此,搭建主主集群并不复杂。
引用
系统环境:红旗 DC Server 5.0
数据库:Mysql 4.1.22-log
A 机:192.168.228.137
B 机:192.168.228.138

这里采用的是操作系统自带的mysql 版本,确实比较老。但配置方式与高版本基本是相同的。不够,4.1版本有个较大的缺陷,不支持auto_increment_incrementauto_increment_offset环境变量。这在主主集群中有比较大的影响。关于此问题,请见后面《常见问题》部分的说明。

二、构建主备集群
先把A机看作主机,B机看作备机。
1、配置A机
编辑/etc/my.cnf 文件,内容如下:
引用
# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
basedir=/var/lib
user=mysql
old_passwords=1

log-bin=/var/lib/mysql/mysql-bin # 定义日志存放路径
max_binlog_size=2000M # 定义日志最大保存大小
binlog-do-db=think # 需同步的数据库
binlog-ignore-db=mysql # 不同步的数据库
binlog-ignore-db=test

server-id=1 #标识必须唯一


[mysqld_safe]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

由于同步依赖的是日志更新,所以,建议把日志单独存放。其默认路径为/var/lib/mysql,以mysql-bin开头的文件。默认最大值1G。更多参数值,可参考:这里
启动数据库:
引用
# service mysqld start
初始化 MySQL 数据库:                                      [  确定  ]
启动 MySQL:                                               [  确定  ]

设置同步数据库的用户帐号,进入mysql 客户端,运行授权:
引用
# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2 to server version: 4.1.22-log

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |      136 | think        | mysql,test       |
+------------------+----------+--------------+------------------+
1 row in set (0.00 sec)

mysql> CREATE DATABASE think;
Query OK, 1 row affected (0.00 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@192.168.228.138 IDENTIFIED BY 'slave';
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH TABLES WITH READ LOCK;
Query OK, 0 rows affected (0.00 sec)

授权后,把表更新暂时锁定。

2、配置B机
配置文件/etc/my.cnf 内容如下:
引用
# cat /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
old_passwords=1

server-id=2 # 必须唯一

master-host=192.168.228.137 # 主机IP
master-user=replication # 已授权用户
master-password=slave # 密码
master-port=3306 # 主机端口


[mysqld_safe]
err-log=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

启动B机数据库:
引用
# service mysqld start
初始化 MySQL 数据库:                                      [  确定  ]
启动 MySQL:                                               [  确定  ]

查看数据库信息:
引用
# mysql
mysql> SHOW DATABASES;
+----------+
| Database |
+----------+
| mysql    |
| test     |
| think    |
+----------+
3 rows in set (0.00 sec)
mysql> SHOW SLAVE STATUS\G;
*************************** 1. row ***************************
             Slave_IO_State: Waiting for master to send event
                Master_Host: 192.168.228.137
                Master_User: replication
                Master_Port: 3306
              Connect_Retry: 60
            Master_Log_File: mysql-bin.000003
        Read_Master_Log_Pos: 136
             Relay_Log_File: mysqlB-relay-bin.000003
              Relay_Log_Pos: 415
      Relay_Master_Log_File: mysql-bin.000003
           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: 136
            Relay_Log_Space: 415
            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
1 row in set (0.00 sec)

ERROR:
No query specified

可见,A机的think 数据库已自动同步到B机上。查看信息时,最关键是Slave_IO_Running: Yes 和 Slave_SQL_Running: Yes两个参数。含义如下:
引用
Slave_IO_Running:此进程负责slave 从master 服务器上读取binlog 日志,并写入slave 服务器上的中继日志中
Slave_SQL_Running:此进程负责读取并且执行中继日志中的binlog 日志只要其中有一个进程的状态是no,则表示复制进程停止,错误原因可以从Last_Errno后面看到。

3、同步A和B
把A机上的临时表锁打开:
引用
mysql> UNLOCK TABLES;
Query OK, 0 rows affected (0.00 sec)

4、测试
A机上:
引用
mysql> USE think;
Database changed
mysql> CREATE TABLE t1 (
    -> id tinyint(5) NOT NULL AUTO_INCREMENT,
    -> PRIMARY KEY (id)
    -> );
mysql> INSERT INTO t1 VALUE (''),(''),('');
Query OK, 3 rows affected, 3 warnings (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 3

mysql> SELECT * FROM t1;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
+----+
3 rows in set (0.00 sec)

B机上查看:
引用
mysql> SHOW DATABASES;
+----------+
| Database |
+----------+
| mysql    |
| test     |
| think    |
+----------+
3 rows in set (0.00 sec)

mysql> USE think;
Database changed
mysql> SELECT * FROM t1;
+----+
| id |
+----+
|  1 |
|  2 |
|  3 |
+----+
3 rows in set (0.00 sec)

可见,对A机上think数据库的操作,已完全同步到B机上。
基于相同的原理,在完成上述功能的同时,我们把B机看作主机,A机看作备机,即可实现主主形式(Master - Master)集群。
内文分页: [1] [2]
Tags: , ,
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]