1.兩臺MySQL互為主從
2.采用keepalived來實現MySQL的故障轉移
3.同時只有一臺MySQL能進行讀寫,另一臺MySQL只能讀
集群環境
Master/Slave Centos7.2 192.168.1.10
Slave/Master Centos7.2 192..168.1.20
vip 192.168.1.100
- 關閉防火墻以及Selinux
systemctl stop firewalld;systemctl disable firewalld
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux
- 安裝mariadb,并啟動服務
yum install mariadb mariadb-server
systemctl restart mariadb;systemctl enable mariadb
- 修改mariadb配置文件
vim /etc/my.conf
[mysqld]
server-id=1 節點標識(主,從不能一樣)
log-bin=mysql-bin 日志文件命名格式
relay-log=myslql-relay-bin relay-log日志文件命名格式
replicate_wild_ignore_table=mysql.% 復制過濾(不復制mysql庫)
replicate_wild_ignore_table=test.%
replicate_wild_ignore_table=information_schema.%
(relicate_wild_do_table)定義需要復制的表或者庫
- 重置數據庫密碼,復制DB1mysql文件到DB2上覆蓋
DB1
mysql_secure_installation
cd /var/lib
tar zcvf mysql.tar.gz mysql
scp mysql.tar.gz 192.168.1.20:/var/lib
DB2
tar zxvf mysql.tar.gz
-
創建復制用戶
在DB1創建復制用戶
mysql -u root -p
12345(數據庫登錄密碼)
grant replication slave on *.* to ‘test'@'192.168.1.20' identified by 'passwd';
show master status;
在DB2上創建復制用戶
mysql -u root -p
12345(數據庫登錄密碼)
grant replication slave on . to ‘test'@'192.168.1.10' identified by 'passwd';
show master status;
- **分別在兩臺mariadb服務器上將對方設置為自己的主服務器
在DB2上將DB1設為自己的主服務器
mysql -u root -p
12345(數據庫登錄密碼)
change master to
master_host='192.168.1.10',master_user='test',master_pass
ord='passwd' master_log_file='mysql
bin.0000006',master_log_pos=245;
(master_log_file和master_log_pos是在DB1上用 show master
status查詢到的信息)
在DB1上將DB2設為自己的主服務
change master to master_host='192.168.1.20',master_user='test',master_password='passwd' master_log_file='mysql-bin.0000005',master_log_pos=245;
(master_log_file和master_log_pos是在DB2上用 show master status查詢到的信息)
- 使用show slave status\G查看復制是否搭建成功。
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
表示成功
- 在兩個節點上啟動slave服務
mariadb> start slave
使用Keepalived實現雙主高可用
編輯配置文件,用下面類容覆蓋
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
212129925@qq.com
}
#notification_email_from 212129925@qq.com
#smtp_server smtp.qq.com
#smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_mysql_port {
script "</dev/tcp/127.0.0.1/3306"
interval 1
weight -2
}
vrrp_instance VI_1 {
state BACKUP 兩個節點上同時為BACKUP
interface ens33
virtual_router_id 51
priority 101
advert_int 1
nopreempt 優先級高的上面配置非搶占
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
track_script {
chk_mysql_port
}
track_interface {
ens33
}
}