mkdir -p /data/mysql chown mysql.mysql /data/mysql systemctl restart mysql #修改配置后重启服务 #yum安装后,mysql5.7的初始密码保存在mysql的error日志中。默认位置为/var/log/mysqld.log grep 'temporary password' /var/log/mysqld.log mysql_secure_installation #修改mysql密码并完成初始化 mysql -p #使用修改后的密码登陆数据库
1 2 3
> grant replication slave,replication client on *.* to repl@'%' identified by '123456'; #创建一个用来同步的用户 > flush privileges; #刷新权限 > flush tables with read lock; #锁表,待同步配置完成再解锁
node1 操作
1
> show master status; #查看当前节点状态
node2 操作
1 2 3 4 5 6 7 8 9 10
> unlock tables; #先解锁,将对方数据同步到自己的数据库中 > stop slave; > change master to master_host='172.20.10.1',master_user='repl',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=150; #根据实际情况修改参数 > start slave; > show slave status \G; #查看两个线程状态是否为YES # Slave_IO_Running: Yes # Slave_SQL_Running: Yes
> flush tables with read lock; #检查无误后将表锁起,准备另一个节点 > show master status; #查看当前节点状态
node1操作
1 2 3 4 5 6 7
> unlock tables; #先解锁,将对方数据同步到自己的数据库中 > stop slave; > change master to master_host='172.20.10.2',master_user='repl',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=150; #根据实际情况修改参数 > start slave; > show slave status \G; #查看两个线程状态是否为YES # Slave_IO_Running: Yes # Slave_SQL_Running: Yes
node2 操作
1
> unlock tables; #将表解锁
上述操作之后,mysql双主配置完毕。可以连接两个节点,验证集群是否可用。
安装配置keepalived
安装keepalived
采用 编译方式安装。具体操作步骤如下:
本步 node1,node2都执行相同操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14
cd /soft wget https://www.keepalived.org/software/keepalived-2.0.19.tar.gz tar xf keepalived-2.0.19.tar.gz cd keepalived-2.0.19 ./configure make && make install #上述编译安装时,可能发生确实依赖的情况 yum install -y openssl-devel cp /soft/keepalived-2.0.19/keepalived/etc/init.d/keepalived /etc/rc.d/init.d/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ mkdir /etc/keepalived/ cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ systemctl enable keepalived
配置keepalived
1 2
cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak vim /etc/keepalived/keepalived.conf