mysql主从方案,有一主多从,读写分离等功能,但是单主存在单点故障,从库切换成主库需要作改动等缺点。
因此,如果是双主或者多主,就会增加mysql入口,增加高可用。
不过多主需要考虑自增长ID问题,这个需要特别设置配置文件,比如双主,可以使用奇偶,总之,主之间设置自增长ID相互不冲突就能完美解决自增长ID冲突问题。
MySQL双主(主主)架构方案思路是:
这样做可以在一定程度上保证主库的高可用,在一台主库down掉之后,可以在极短的时间内切换到另一台主库上(尽可能减少主库宕机对业务造成的影响),减少了主从同步给线上主库带来的压力;
但是也有几个不足的地方:
搭建环境
安装mysql软件
ip地址
masterA :192.168.169.140
masterB :192.168.169.139
首先我们配置mysql双主模型,让其数据同步
vim /etc.my.cnf
server-id=1
log-bin=mysql-bin
binlog_format=mixed
relay-log=relay-bin
relay-log-index=slave-relay-bin.index
auto-increment-increment=2
auto-increment-offset=1
log-slave-updates
粉框中是两个mysql不同的地方
mysql2的粉框处是2
然后重启mysql
service mysqld restart
auto-increment 两行的配置,使 masterA字段产生的数值是 奇数1,3,5,7 下面的masterB 产生的是 2,4,6,8 等,这样会避开双主 id 重复的问题
创建用户
grant replication slave on *.* to 'repl'@'172.18.74.71' identified by '123456'; ';
flush privileges;
web服务器授权
mysql> create database db_jd;
Query OK, 1 row affected (0.00 sec)
mysql> create user web@localhost identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on *.* to web@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> create user [email protected] identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> create user [email protected] identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'web'@'%' IDENTIFIED BY '123456' WITH GRANT OPTTION;
GRANT ALL PRIVILEGES ON *.* TO 'web'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.01 sec)
查看主库的状态
配置同步信息
masterA上:
change master to master_host='192.168.169.139',master_port=3306,master_user='repl',master_password='root',master_log_file='mysql-bin.000002',master_log_pos=632;
start slave;
show slave status\G
masterB上:
change master to master_host='192.168.169.140',master_port=3306,master_user='repl',master_password='root',master_log_file='mysql-bin.000002',master_log_pos=632;
start slave;
show slave status\G
测试主主同步
在masterA上创建一个数据库测试同步效果
mysql> create database myTest;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| myTest |
| mysql |
| performance_schema |
±-------------------+
4 rows in set (0.00 sec)
到masterB查看是否已经同步创建数据库
mysql> show databases;
±-------------------+
| Database |
±-------------------+
| information_schema |
| myTest |
| mysql |
| performance_schema |
±-------------------+
4 rows in set (0.00 sec)
成功!