MYSQL主主 mysql中有自增长字段,在做数据库的主主同步时需要设置自增长的两个相关配置:auto_increment_offset和auto_increment_increment。 auto_increment_offset表示自增长字段从那个数开始,他的取值范围是1 .. 65535 auto_increment_increment表示自增长字段每次递增的量,其默认值是1,取值范围是1 .. 65535 在主主同步配置时,需要将两台服务器的auto_increment_increment增长量都配置为2,而要把auto_increment_offset分别配置为1和2. 这样才可以避免两台服务器同时做更新时自增长字段的值之间发生冲突。 init:关防火墙 systemctl stop firewalld 主1(先变更主库配置文件,重启生效,再做从库的配置文件,重启生效) vi /etc/my.cnf #[mysqld]这个字段里 server-id = 1 log-bin = mysql-bin sync_binlog = 1 binlog_format = mixed auto-increment-increment = 2 auto-increment-offset = 1 slave-skip-errors = all 1、创建用户 grant replication slave,replication client on *.* to repl@'%' identified by '1qaz@WSX'; 2、修改root用户密码 alter user 'root'@'localhost' identified by '1qaz@WSX'; 3、锁表 MariaDB [(none)]> flush tables with read lock; Query OK, 0 rows affected (0.00 sec) 4、查看节点 MariaDB [(none)]> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000003 | 411 | | | +------------------+----------+--------------+------------------+ 5、设置同步 change master to master_host='10.211.55.172',master_user='repl',master_password='1qaz@WSX',master_log_file='mysql-bin.000004',master_log_pos=1064; MariaDB [(none)]> start slave; 6、 MariaDB [(none)]> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.211.55.172 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000004 Read_Master_Log_Pos: 411 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 529 Relay_Master_Log_File: mysql-bin.000004 Slave_IO_Running: Yes Slave_SQL_Running: Yes 7、 MariaDB [(none)]> unlock tables; Query OK, 0 rows affected (0.00 sec) ============================================================== 主2 vi /etc/my.cnf #[mysqld]这个字段里 server-id = 2 log-bin = mysql-bin sync_binlog = 1 binlog_format = mixed auto-increment-increment = 2 auto-increment-offset = 2 slave-skip-errors = all 1、创建用户 grant replication slave,replication client on *.* to repl@'%' identified by '1qaz@WSX'; 2、修改root用户密码 alter user 'root'@'localhost' identified by '1qaz@WSX'; 3、锁表 flush tables with read lock; MariaDB [(none)]> flush tables with read lock; Query OK, 0 rows affected (0.00 sec) 4、查看节点 MariaDB [(none)]> show master status; +------------------+----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+----------+--------------+------------------+ | mysql-bin.000004 | 411 | | | +------------------+----------+--------------+------------------+ 1 row in set (0.00 sec) 5、设置同步 change master to master_host='10.211.55.171',master_user='repl',master_password='1qaz@WSX',master_log_file='mysql-bin.000003',master_log_pos=411; MariaDB [(none)]> start slave 6、查看同步 MariaDB [(none)]> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.211.55.171 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 411 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 529 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes 7、 MariaDB [(none)]> unlock tables; Query OK, 0 rows affected (0.00 sec) 二、检查同步 主1操作: MariaDB [(none)]> create database 1804b; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | 1804b | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) MariaDB [(none)]> use 1804b; Database changed MariaDB [1804b]> create table student( id int(10) PRIMARY KEY AUTO_INCREMENT, name varchar(50) NOT NULL,age int(2)); Query OK, 0 rows affected (0.00 sec) MariaDB [1804b]> insert into student values('1','zhangsan','20'); Query OK, 1 row affected (0.00 sec) MariaDB [1804b]> insert into student values('','lisi','20'); Query OK, 1 row affected, 1 warning (0.00 sec) 主2操作: MariaDB [(none)]> MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | 1804b | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) MariaDB [(none)]> use 1804b; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MariaDB [1804b]> show tables; +-----------------+ | Tables_in_1804b | +-----------------+ | student | +-----------------+ 1 row in set (0.00 sec) MariaDB [1804b]> select * from student -> ; +----+----------+------+ | id | name | age | +----+----------+------+ | 1 | zhangsan | 20 | | 3 | lisi | 20 | +----+----------+------+ 2 rows in set (0.00 sec) MariaDB [1804b]> 配置keepalived keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] #alias root="[email protected]" smtp_server 127.0.0.1 #smtp smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr # vrrp_strict #严格的vrrp协议,假如没有注释,每次都得IPTABLES 都得放行一次,默认就是对VIP进行drop数据包 vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script chk_mysql_port { #检测mysql服务是否在运行。有很多方式,比如进程,用脚本检测等等 script "/opt/chk_mysql.sh" #这里通过脚本监测 interval 2 #脚本执行间隔,每2s检测一次 weight -5 #脚本结果导致的优先级变更,检测失败(脚本返回非0)则优先级 -5 fall 2 #检测连续2次失败才算确定是真失败。会用weight减少优先级(1-255之间) rise 1 #检测1次成功就算成功。但不修改优先级 } vrrp_instance VI_1 { state MASTER interface eth0 #指定虚拟ip的网卡接口 virtual_router_id 51 priority 100 #主从权重区分,数值越大,权重越高,VIP就在哪一边 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.211.55.180 } track_script { chk_mysql_port } } [root@master1 ~]# vim /opt/chk_mysql.sh #!/bin/bash counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l) if [ "${counter}" -eq 0 ]; then /etc/init.d/keepalived stop fi
MYSQL主主+ keepalived
猜你喜欢
转载自blog.51cto.com/slapping/2601909
今日推荐
周排行