14-4/5 MySQL 主主同步
博客专区 > 阿想 的博客 > 博客详情
14-4/5 MySQL 主主同步
阿想 发表于3个月前
14-4/5 MySQL 主主同步
  • 发表于 3个月前
  • 阅读 9
  • 收藏 1
  • 点赞 0
  • 评论 0

MySQL 主主同步

准备工作

设备: 服务器A 10.1.1.7 服务器B 10.1.1.8

环境:CentOS7.3

目标:两台服务器主主同步,实现高可用,

安装MySQL 5.1绿色版: wget~tar~mv~useradd~mkdir~scripts/~cp support x2~vim~chkconfig~start

配置主主同步

关防火墙

systemctl stop firewalld
systemctl disable firewalld

调整my.cnf配置

A:

vim /etc/my.cnf
~
[mysqld]
server-id       = 1
auto_increment_offset = 1
auto_increment_increment = 2

log-bin=mysql-bin
log-slave-updates

B:

vim /etc/my.cnf
~
[mysqld]
server-id       = 2
auto_increment_offset = 2
auto_increment_increment = 2

log-bin=mysql-bin
log-slave-updates

注: server-id 不能一样,可以考虑改为IP末位

auto_increment_offset 为(主键)起始值,两台设备主键相同会导致主主断裂,需要错开

auto_increment_increment 为(主键)增长值,因为起始值已经错开,增加的值为错开值即可保证不会冲突。

举个例子:四台设备a,b,c,d的主键起始值分别为1,2,3,4。增长值为4。a主键则会以1,5,9,13...增长,b主键会以2,6,10,14...增长,c主键以3,7,11,15增长,d我懒得写了。反正结果是不会出现主键冲突。两台设备则呈现主键奇偶增长。

调整数据库

将mysql命令加入PATH

echo "PATH=$PATH:/usr/local/mysql/bin/" >> .bash_profile
source !$

A上操作:A给B开户,查看A主信息

mysql -uroot 
mysql> grant replication slave on *.* to 'repl'@'10.1.1.8' identified by 'axianglinux';
mysql> flush privileges;
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000007 |      106 |              |                  |
+------------------+----------+--------------+------------------+

B上操作:B从A

mysql> change master to master_host='10.1.1.7',master_port=3306,master_user='repl',master_password='axianglinux',master_log_file='mysql-bin.000007',master_log_pos=106;
mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.1.7
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 106
               Relay_Log_File: axiang-02-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000007
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
~

反着再来一遍 B上操作:B给A开户,查看B主信息

grant replication slave on *.* to 'repl'@'10.1.1.7' identified by 'axianglinux'
flush privileges;
show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000009 |      462 |              |                  |
+------------------+----------+--------------+------------------+

A上操作:A从B

change master to master_host='10.1.1.8',master_port=3306,master_user='repl',master_password='axianglinux',master_log_file='mysql-bin.000009',master_log_pos=462;
start slave;
show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.1.8
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000009
          Read_Master_Log_Pos: 622
               Relay_Log_File: axiang-01-relay-bin.000002
                Relay_Log_Pos: 331
        Relay_Master_Log_File: mysql-bin.000009
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
~

测试

A:

mysql> grant all on *.* to 'usera'@'127.0.0.1';

mysql> create database dbusera;

B:

mysql> select user,host,password from mysql.user;
+-------+-----------+-------------------------------------------+
| user  | host      | password                                  |
+-------+-----------+-------------------------------------------+
| root  | localhost |                                           |
| root  | axiang-02 |                                           |
| root  | 127.0.0.1 |                                           |
|       | localhost |                                           |
|       | axiang-02 |                                           |
| repl  | 10.1.1.7  | *8E1A3402D66F8DDD8D9D19596B706C6D238C0F34 |
| usera | 127.0.0.1 |                                           |
+-------+-----------+-------------------------------------------+
7 rows in set (0.01 sec)

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| dbusera            |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.01 sec)

mysql> create database dbuserb
    -> ;
Query OK, 1 row affected (0.00 sec)

A:

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| dbusera            |
| dbuserb            |
| mysql              |
| test               |
| wordpress          |
+--------------------+

  • 因为A上有wordpress库,B没有,对wordPress的操作会导致B从A断裂

恢复

A:

mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000008 |      945 |              |                  |
+------------------+----------+--------------+------------------+

B:

mysql> stop slave
    -> ;
Query OK, 0 rows affected (0.02 sec)

mysql> change master to master_host='10.1.1.7',master_port=3306,master_user='repl',master_password='axianglinux',master_log_file='mysql-bin.000008',master_log_pos=945;
Query OK, 0 rows affected (0.01 sec)

mysql> start slave;
Query OK, 0 rows affected (0.02 sec)

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.1.7
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000008
          Read_Master_Log_Pos: 945
               Relay_Log_File: axiang-02-relay-bin.000002
                Relay_Log_Pos: 251
        Relay_Master_Log_File: mysql-bin.000008
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
~
  • 以上实验追求简单,实际生产环境有写入的情况需先备份数据
  • mysqldump备份需要锁表,innobackup貌似不用
  • 从的logbin内的Pos点可以断点续传。后面主从逻辑不出错则可以从旧Pos开启
共有 人打赏支持
粉丝 0
博文 72
码字总数 69742
×
阿想
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: