IP地址 | 用途 | 備註 |
172.16.10.83 | MySQL | mysqla |
172.16.10.62 | MySQL | mysqlb |
172.16.10.199 | VIP(keepalived) |
mkdir -p /data/mysqldb mkdir -p /data/docker-compose/mysql-compose
cd /data/docker-compose/mysql-compose vim docker-compose.yml version: '3' services: mysql: image: mysql:5.6.35 ports: - "3306:3306" volumes: - ./my.cnf:/etc/mysql/conf.d/my.cnf - /data/mysqldb:/var/lib/mysql - /etc/localtime:/etc/localtime:ro environment: - MYSQL_ROOT_PASSWORD=123456 restart: always
將本身的my.cnf文件放置在/data/docker-compose/mysql-compose目錄下node
my.cnf python
[client] default-character-set = utf8 port = 3306 [mysql] port = 3306 default-character-set = utf8 [mysqld] port = 3306 basedir = /var/lib/mysql datadir = /var/lib/mysql character-set-server = utf8 log-bin = mysql-bin binlog_cache_size = 1M expire_logs_days = 10 max_binlog_size = 128M server_id = 1235 binlog_format=MIXED read-only=0 auto-increment-increment=10 auto-increment-offset=1 skip-external-locking slow-query-log = on long_query_time = 1 slow_query_log_file = /var/lib/mysql/slow.log lower_case_table_names = 1 max_connections=1100 max_user_connections=100 max_connect_errors=1000 innodb_buffer_pool_size = 100M innodb_buffer_pool_instances = 8 innodb_log_file_size = 200M innodb_log_buffer_size = 16M innodb_log_files_in_group = 3 innodb_flush_log_at_trx_commit = 0 innodb_lock_wait_timeout = 10 innodb_sync_spin_loops = 40 innodb_max_dirty_pages_pct = 90 innodb_support_xa = 0 innodb_thread_concurrency = 0 innodb_thread_sleep_delay = 500 innodb_concurrency_tickets = 1000 log_bin_trust_function_creators = 1 innodb_flush_method = O_DIRECT innodb_file_per_table innodb_read_io_threads = 16 innodb_write_io_threads = 16 innodb_io_capacity = 2000 innodb_file_format = Barracuda innodb_purge_threads=1 innodb_purge_batch_size = 32 innodb_old_blocks_pct=75 innodb_change_buffering=all innodb_stats_on_metadata=OFF sql_mode=ONLY_FULL_GROUP_BY,NO_AUTO_VALUE_ON_ZERO,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION log-error=/var/lib/mysql/mysql.log pid-file=/var/lib/mysql/mysql.pid
配置文件中須要增長(以上文件已加) mysql
server-id=123 log-bin=mysql-bin binlog_format=MIXED read-only=0 auto-increment-increment=10 auto-increment-offset=1 read-only:標識數據庫是否爲只讀,這裏咱們設置爲0即非只讀,該參數針對用戶沒有SUPER權限設置。 auto-increment-increment和auto-increment-offset這兩個參數主要控制MySQL自增列的值, 用於Master-Master之間的複製,防止出現重複值。作了如上配置後,咱們向該MySQLA服務中插入第一個id就是1, 第二行的id就是11,而不是2,那麼在MySQLB服務插入第一個id就是2,第二行的id就是12,這樣就不會出現主鍵衝突。
docker-compose up -d #docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bba3674e9c44 mysql:5.6.35 "docker-entrypoint.s…" 3 hours ago Up 41 minutes 0.0.0.0:3306->3306/tcp mysqlcompose_mysql_1
# 在另外一臺啓動mysql前,其配置文件my.cnf修改 server-id=190 log-bin=mysql-bin binlog_format=MIXED relay_log=mysql-relay-bin log-slave-updates=ON read-only=0 auto-increment-increment=10 auto-increment-offset=2
GRANT REPLICATION SLAVE ON *.* TO 'mysqla'@'172.16.%' IDENTIFIED BY 'mysqla'; #該同步帳號主要是給MySQLB使用。
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000006 | 120 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
change master to master_host='172.16.10.83', master_user='mysqla', master_password='mysqla', master_log_file='mysql-bin.000006', master_log_pos=120;
mysql> start slave;
mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.10.83 Master_User: mysqla Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000006 Read_Master_Log_Pos: 120 Relay_Log_File: mysql-relay-bin.000006 Relay_Log_Pos: 283 Relay_Master_Log_File: mysql-bin.000006 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table:
GRANT REPLICATION SLAVE ON *.* TO 'mysqlb'@'172.16.%' IDENTIFIED BY 'mysqlb'; #該同步帳號主要是給MySQLA使用。
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000006 | 473 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
change master to master_host='172.16.10.62', master_user='mysqlb', master_password='mysqlb', master_log_file='mysql-bin.000006', master_log_pos=473;
mysql> start slave;
mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 172.16.10.62 Master_User: mysqlb Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000006 Read_Master_Log_Pos: 756 Relay_Log_File: mysql-relay-bin.000006 Relay_Log_Pos: 467 Relay_Master_Log_File: mysql-bin.000006 Slave_IO_Running: Yes Slave_SQL_Running: Yes
mysql> create database test; Query OK, 1 row affected (0.00 sec) mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.00 sec)
數據庫同步過來了sql
確實已經不存在了,到此MySQL主主互備完成。docker
yum -y install keepalived systemctl enable keepalived
MySQL服務狀態監測腳本,主要監控MySQL服務狀態是否正常,若是不正常則將該MySQL所在服務的Keepalived服務殺死,監控MySQL服務是否正常的方法有多種能夠經過端口號、進程ID以及執行MySQL命令,這裏咱們使用mysladmin執行命令來監測MariaDB服務是否正常,腳本內容以下(check_mysql.sh)數據庫
cd /etc/keepalived vim check_mysql.sh #!/bin/bash MYSQL_PING=`docker exec mysqlcompose_mysql_1 mysqladmin -h127.0.0.1 -uroot -p123456 ping 2>/dev/null` MYSQL_OK="mysqld is alive" if [[ "$MYSQL_PING" != "$MYSQL_OK" ]];then echo "mysql is not running." killall keepalived else echo "mysql is running" fi
注:兩臺服務器都要配置vim
! Configuration File for keepalived global_defs { } vrrp_script check_mysql { script "/etc/keepalived/check_mysql.sh" interval 2 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 nopreempt authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.16.10.199 } track_script { check_mysql } }
注:這個是masterbash
! Configuration File for keepalived global_defs { } vrrp_script check_mysql { script "/etc/keepalived/check_mysql.sh" interval 2 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.16.10.199 } track_script { check_mysql } }
systemctl start keepalived
Master在172.16.10.83這個服務器上服務器
在兩臺服務器上pingVIPtcp
172.16.10.83
172.16.10.62
將keepalive的master這個停掉,VIP就應該不在這個服務器上了
# MySQLA服務器 systemctl stop keepalived
查看BACKUP服務器,確實已經漂移過來了
在MySQLA服務器上中止mysql容器
#docker stop mysqlcompose_mysql_1 mysqlcompose_mysql_1 [root@node1 /etc/keepalived] #docker ps | grep mysqlcompose_mysql_1 [root@node1 /etc/keepalived] #ip addr list eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether fa:36:67:e1:b4:00 brd ff:ff:ff:ff:ff:ff inet 172.16.10.83/24 brd 172.16.10.255 scope global eth0 valid_lft forever preferred_lft forever inet6 fe80::f836:67ff:fee1:b400/64 scope link valid_lft forever preferred_lft forever
可見VIP已經不在了
事實上keepalived也中止了
VIP自動漂移到MySQLB這臺服務器上了