部署
keepalived 的主要作用是为 Mariadb 提供 vip,在2个 Mariadb 实例之间切换,不间断的提供服务。
部署配置 Mariadb 主主复制
安装并启动 Mariadb
yum install -y mariadb-server systemctl enable --now mariadb 运行 Mariadb 安全配置向导,设置密码等
$ mysql_secure_installation
... ...
Change the root password? Y/n y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
... ...
Remove anonymous users? Y/n y
... Success!
... ...
Disallow root login remotely? Y/n y
... Success!
... ...
Remove test database and access to it? Y/n y
$ cat < /etc/my.cnf
mysqld
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
skip_name_resolve
expire_logs_days=30
innodb_file_per_table=ON
max_connections = 300
max_allowed_packet=20M
server-id = 1
auto_increment_offset = 1
auto_increment_increment = 2
log-bin = mysql-bin
binlog-format = row
log-slave-updates
max_binlog_size = 1G
replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema
max_connections = 1000
max_connect_errors = 0
max_allowed_packet = 1G
slave-net-timeout=10
master-retry-count=0
slow_query_log = 1
long_query_time = 2
slow_query_log_file = /var/log/mariadb/slow-query.log
mysql
no-auto-rehash
mysqld_safe
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
EOF
$ cat < /etc/my.cnf
mysqld
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
skip_name_resolve
expire_logs_days=30
innodb_file_per_table=ON
max_connections = 300
max_allowed_packet=20M
server-id = 2
auto_increment_offset = 2
auto_increment_increment = 2
log-bin = mysql-bin
binlog-format = row
log-slave-updates
max_binlog_size = 1G
replicate-ignore-db = information_schema
replicate-ignore-db = performance_schema
max_connections = 1000
max_connect_errors = 0
max_allowed_packet = 1G
slave-net-timeout=10
master-retry-count=0
slow_query_log = 1
long_query_time = 2
slow_query_log_file = /var/log/mariadb/slow-query.log
mysql
no-auto-rehash
mysqld_safe
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
!includedir /etc/my.cnf.d
EOF
$ systemctl restart mariadb
主节点创建只读账号,导出全部数据,导入备节点。记录binlog日志文件名和position。
$ MYSQL_PASSWD='your-sql-passwd'
$ mysql -uroot -pMYSQLPASSWD−e"GRANTALLPRIVILEGESON∗.∗TO′root′@′MYSQL_PASSWD -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'MYSQLPASSWD−e"GRANTALLPRIVILEGESON∗.∗TO′root′@′MYSQL_PASSWD' WITH GRANT OPTION;FLUSH PRIVILEGES"
$ mysql -u root -pMYSQLPASSWD−e"GRANTREPLICATIONSLAVEON∗.∗TOrepl@′MYSQL_PASSWD -e "GRANT REPLICATION SLAVE ON *.* TO repl@'%' IDENTIFIED BY 'MYSQLPASSWD−e"GRANTREPLICATIONSLAVEON∗.∗TOrepl@′MYSQL_PASSWD';FLUSH PRIVILEGES"
mysql -uroot -p MYSQL_PASSWD -e "SHOW PROCESSLIST" +----+------+-----------+------+---------+------+-------+------------------+----------+ | Id | User | Host | db | Command | Time | State | Info | Progress | +----+------+-----------+------+---------+------+-------+------------------+----------+ | 4 | root | localhost | NULL | Query | 0 | NULL | SHOW PROCESSLIST | 0.000 | +----+------+-----------+------+---------+------+-------+------------------+----------+
mysql -u root -p MYSQL_PASSWD -e "SHOW MASTER STATUS\G" *************************** 1. row *************************** File: mysql-bin.000001 Position: 2023 Binlog_Do_DB: Binlog_Ignore_DB:
mysqldump --all-databases -p MYSQL_PASSWD > alldb.db
$ scp alldb.db db2:/root/
$ MYSQL_PASSWD='your-sql-passwd'
mysql -u root -p$MYSQL_PASSWD < alldb.db
mysql -u root -p$MYSQL_PASSWD -e "FLUSH PRIVILEGES"
mysql -u root -p$MYSQL_PASSWD -e "SHOW MASTER STATUS\G"
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 509778
Binlog_Do_DB:
Binlog_Ignore_DB:
设置主主复制
mysql -u root -pMYSQLPASSWD−e"CHANGEMASTERTOMASTERHOST=′192.168.199.99′,MASTERUSER=′repl′,MASTERPASSWORD=′MYSQL_PASSWD -e "CHANGE MASTER TO MASTER_HOST='192.168.199.99',MASTER_USER='repl',MASTER_PASSWORD='MYSQLPASSWD−e"CHANGEMASTERTOMASTERHOST=′192.168.199.99′,MASTERUSER=′repl′,MASTERPASSWORD=′MYSQL_PASSWD',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=509778,MASTER_CONNECT_RETRY=2;START SLAVE"
mysql -u root -pMYSQLPASSWD−e"CHANGEMASTERTOMASTERHOST=′192.168.199.98′,MASTERUSER=′repl′,MASTERPASSWORD=′MYSQL_PASSWD -e "CHANGE MASTER TO MASTER_HOST='192.168.199.98',MASTER_USER='repl',MASTER_PASSWORD='MYSQLPASSWD−e"CHANGEMASTERTOMASTERHOST=′192.168.199.98′,MASTERUSER=′repl′,MASTERPASSWORD=′MYSQL_PASSWD',MASTER_PORT=3306,MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=2023,MASTER_CONNECT_RETRY=2;START SLAVE"
mysql -u root -p$MYSQL_PASSWD -e "SHOW SLAVE STATUS\G" | grep Running
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
至此,DB 主主复制部署完成,可以测试在任一节点进行数据库操作,另一节点验证。不过对外提供服务还是需要通过 vip,不然发生切换还需要业务端切换 ip,下面配置 keepalived 对外提供服务。
部署配置 keepalived
设置相关的环境变量,根据不同的环境自行配置。
export DB_VIP=192.168.199.97
export DBHA_KA_AUTH=onecloud
export DB_NETIF=eth0
设置 sysctl 选项
$ cat <>/etc/sysctl.conf
net.ipv4.ip_forward = 1
net.ipv4.ip_nonlocal_bind = 1
EOF
$ sysctl -p
安装 keepalived nc
$ yum install -y keepalived nc
添加配置
$ cat </etc/keepalived/keepalived.conf
global_defs {
router_id onecloud
}
vrrp_script chk_mysql {
script "/etc/keepalived/chk_mysql"
interval 1
}
vrrp_instance VI_1 {
state MASTER
interface $DB_NETIF
virtual_router_id 99
priority 100
advert_int 1
nopreempt
authentication {
auth_type PASS
auth_pass $DBHA_KA_AUTH
}
track_script {
chk_mysql
}
virtual_ipaddress {
$DB_VIP
}
}
EOF
$ cat < /etc/keepalived/chk_mysql
#!/bin/bash
echo | nc 127.0.0.1 3306 &>/dev/null
EOF
$ chmod +x /etc/keepalived/chk_mysql
启动 keepalived