14-4/5 MySQL 主主同步

MySQL 主主同步

准备工做

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

环境:CentOS7.3linux

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

安装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

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

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

B:bash

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 为(主键)起始值,两台设备主键相同会致使主主断裂,须要错开ide

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

举个例子:四台设备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开启
相关文章
相关标签/搜索