mysql双主安装部署
Posted python真好用
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mysql双主安装部署相关的知识,希望对你有一定的参考价值。
mysql双主安装部署
准备工作:
1.master1与master2的mysql已安装完成(版本一致,版本不一致没试过小版本应该问题不大)
2.master1跟master2的端口网络已互通
3.数据库新建同步用户,create user slave identified by ‘xxx’;
开始:
1.master1节点修改my.cnf
关键配置:
server_id=2 --server id 每个节点都不能相同
auto-increment-increment = 2 --自增长字段每次递增2,n台机器就是n,这里是2
auto-increment-offset = 1 --自增长字段从1开始,1,2,…,n,第n台就是n
这里my.cnf的配置是
[mysqld]
#admin
datadir = /dsj/data
tmpdir = /dsj/tmp
port = 13307
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER
lower_case_table_names=1
character-set-server=utf8mb4
collation-server=utf8mb4_bin
skip_name_resolve=1
secure_file_priv=/var/lib/mysql-files
log-error=/var/log/mysqld.log
local_infile=0
slow_query_log=1
long_query_time=0.5
expire_logs_days = 20
event_scheduler=1
#replication
server_id=1
log_bin=/dsj/binlog/mysql-bin
relay_log=relay-bin
relay_log_recovery=1
innodb_flush_log_at_trx_commit=1
sync_binlog=1
gtid_mode=on
log_slave_updates=1
enforce_gtid_consistency=1
skip_slave_start=1
binlog_format=ROW
sync_master_info=1
slave_parallel_workers=4
binlog_checksum=CRC32
master_verify_checksum=1
slave_sql_verify_checksum=1
binlog_rows_query_log_events=1
slave_net_timeout=30
auto-increment-increment = 2 #自增长字段每次递增2
auto-increment-offset = 1 #自增长字段从1开始
slave-skip-errors = all #跳过所有的错误错误,继续执行复制操作
validate_password_policy=0 #指定密码策略
validate_password = off #禁用密码策略
#performance
innodb_buffer_pool_size=16000M
read_buffer_size=4M
sort_buffer_size=4M
join_buffer_size=4M
innodb_log_file_size=512M
back_log=500
max_connections=2000
open_files_limit=100000
innodb_open_files=100000
table_open_cache=20000
table_definition_cache=20000
max_allowed_packet=40M
[client]
default-character-set=utf8
2.加载配置,重启mysql
systemctl daemon-reload
systemctl restart mysqld
3.master1的数据同步授权(同步到master2那台机器上)
以下为master1下操作
grant replication slave,replication client,select on *.* to slave@'192.168.1.95' identified by 'Congku@114';
flush privileges;
#查看服务器状态
mysql> show master status;
+------------------+----------+--------------+------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+------------------------------------------+
| mysql-bin.000006 | 929 | | | d3672032-c801-11eb-9a70-005056aeb414:1-4 |
+------------------+----------+--------------+------------------+------------------------------------------+
#最好将库锁住,仅仅允许读,以保证数据一致性;待主主同步环境部署后再解锁;锁住后,就不能往表里写数据,但是重启mysql服务后就会自动解锁!
注意该参数设置后,如果自己同步对方数据,同步前一定要记得先解锁!
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
4.master2节点修改my.cnf
关键配置:
server_id=2 --server id 每个节点都不能相同
auto-increment-increment = 2 --自增长字段每次递增2,n台机器就是n,这里是2
auto-increment-offset = 1 --自增长字段从1开始,1,2,…,n,第n台就是n
其余配置同master1
5.master2的数据同步授权(同步到master1那台机器上)
以下为master2下操作
grant replication slave,replication client,select on *.* to slave@'192.168.1.58' identified by 'Congku@114';
flush privileges;
show master status;
#最好将库锁住,仅仅允许读,以保证数据一致性;待主主同步环境部署后再解锁;锁住后,就不能往表里写数据,但是重启mysql服务后就会自动解锁!
注意该参数设置后,如果自己同步对方数据,同步前一定要记得先解锁!
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
6.先在master2数据库上做同步master1的设置
以下为master2下操作
unlock tables;
stop slave;
#参数均为master1的参数
change master to master_host='192.168.1.58',master_user='slave',master_password='Congku@114',master_port=113307,master_log_file='mysql-bin.000006',master_log_pos=929;
start slave;
#查看slave状态有报错解决报错
show slave status\\G;
#完成了master2->master1的同步环境
7.在master1数据库上做同步master2的设置
以下为master1下操作
unlock tables;
stop slave;
change master to master_host='192.168.1.95',master_user='slave',master_password='Congku@114',master_port=113307,master_log_file='mysql-bin.000006',master_log_pos=929;
start slave;
#查看slave状态有报错解决报错
show slave status\\G;
#完成了master1->master2的同步环境
测试一下:
在master1新建数据库
回到master2会发现数据库也已经建好
master2新建表master1也已经建好
master1插入数据,master2可以查看
master2插入数据,master1可以查看
master1删除数据,master2可以查看已删除
master2删除数据,master1可以查看已删除
以上是关于mysql双主安装部署的主要内容,如果未能解决你的问题,请参考以下文章