系统信息 | 主机名 | IP | 工具 |
---|---|---|---|
redhat7 | hxdserver | 192.168.225.128 | keepalived MySQL |
redhat7 | bogon | 192.168.225.129 | keepalived MySQL |
redhat7 | test | 192.168.225.130 | Mariadb |
//关闭防火墙和selinux
[root@hxdserver ~]# systemctl stop firewalld
[root@hxdserver ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@hxdserver ~]# setenforce 0
setenforce: SELinux is disabled
[root@hxdserver ~]# sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/selinux/config
//安装keepalived
[root@hxdserver ~]# yum -y install epel-release vim wget gcc gcc-c++
[root@hxdserver ~]# yum -y install keepalived
在备机上做以上相同操作
//主备机上都安装MySQL
//创建用户和组
[root@hxdserver ~]# groupadd -r mysql
[root@hxdserver ~]# useradd -M -s /sbin/nologin -g mysql mysql
//解压之前下载好的安装包
[root@hxdserver ~]# cd /usr/local/
[root@hxdserver local]# ls
bin games lib libexec sbin src
etc include lib64 mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz share
[root@hxdserver local]# tar xf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz
[root@hxdserver local]# ls
bin include libexec sbin
etc lib mysql-5.7.23-linux-glibc2.12-x86_64 share
games lib64 mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz src
//创建一个软连接,方便使用
[root@hxdserver local]# ln -sv mysql-5.7.23-linux-glibc2.12-x86_64/ mysql
"mysql" -> "mysql-5.7.23-linux-glibc2.12-x86_64/"
[root@hxdserver local]# ll
lrwxrwxrwx. 1 root root 36 9月 25 17:24 mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
drwxr-xr-x. 9 root root 129 9月 25 17:19 mysql-5.7.23-linux-glibc2.12-x86_64
//修改目录的属主和属组
[root@hxdserver ~]# chown -R mysql.mysql /usr/local/mysql
[root@hxdserver ~]# ll /usr/local/mysql -d
lrwxrwxrwx. 1 mysql mysql 36 9月 25 17:24 /usr/local/mysql -> mysql-5.7.23-linux-glibc2.12-x86_64/
//添加环境变量
[root@hxdserver ~]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@hxdserver ~]# . /etc/profile.d/mysql.sh
[root@hxdserver ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
//创建数据储存的目录
[root@hxdserver ~]# mkdir /opt/data
[root@hxdserver ~]# chown -R mysql.mysql /opt/data/
[root@hxdserver ~]# ll /opt/
总用量 0
drwxr-xr-x. 2 mysql mysql 6 9月 25 17:35 data
//初始化数据库
[root@hxdserver ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data
2018-09-25T10:22:38.855635Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-09-25T10:22:39.512697Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-09-25T10:22:39.553504Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-09-25T10:22:39.640246Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: ee1a32a6-c0ac-11e8-be86-000c294b7cb8.
2018-09-25T10:22:39.641568Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-09-25T10:22:39.647861Z 1 [Note] A temporary password is generated for root@localhost: 08waBnuLEe(> //这里冒号后面的是初始密码
//生成配置文件
[root@hxdserver ~]# vim /etc/my.cnf
[root@hxdserver ~]# cat /etc/my.cnf
[mysqld]
basedir=/usr/local/mysql
datadir=/opt/data
socket=/tmp/mysql.sock
port=3306
pid-file=/opt/data/mysql.pid
user=mysql
skip-name-resolve
//配置服务启动脚本
[root@hxdserver ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@hxdserver ~]# sed -ri 's#^(basedir=).*#\1/usr/local/mysql#g' /etc/init.d/mysqld
[root@hxdserver ~]# sed -ri 's#^(datadir=).*#\1/opt/data#g' /etc/init.d/mysqld
//启动mysql
[root@hxdserver ~]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/opt/data/hxdserver.err'.
. SUCCESS!
[root@hxdserver ~]# ps -ef |grep mysql
root 13436 1 0 18:44 pts/1 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql 13614 13436 2 18:44 pts/1 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=hxdserver.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root 13648 12759 0 18:44 pts/1 00:00:00 grep --color=auto mysql
[root@hxdserver ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 80 :::3306 :::*
//修改密码,登录时使用前面所给的初始密码
[root@hxdserver ~]# /usr/local/mysql/bin/mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.23
Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
//设置新密码
mysql> set password = password('hxd123456.');
Query OK, 0 rows affected, 1 warning (0.00 sec)
//为了容易看出效果,这里在两台装有MySQL分别创建不同的库,并允许root用户在所有主机上能够登录本机数据库
mysql> set password = password('hxd123456.');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create database dubai123;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dubai123 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> grant all on *.* to 'root'@'%' identified by 'hxd123456!' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> set password = password('hxd123456.');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> create database hxd123;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hxd123 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> grant all on *.* to 'root'@'%' identified by 'hxd123456!' with grant option;
Query OK, 0 rows affected, 1 warning (0.00 sec)
[root@hxdserver ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass dubai
}
virtual_ipaddress {
192.168.225.250
}
}
virtual_server 192.168.225.250 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.225.128 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.225.129 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
//开启服务
[root@hxdserver ~]# systemctl start keepalived
[root@hxdserver ~]# systemctl enable keepalived
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
[root@bogon ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass dubai
}
virtual_ipaddress {
192.168.225.250
}
}
virtual_server 192.168.225.250 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.225.128 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.225.129 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
//开启服务
[root@bogon ~]# systemctl start keepalived
[root@bogon ~]# systemctl enable keepalived
Created symlink from /etc/systemd/system/multi-user.target.wants/keepalived.service to /usr/lib/systemd/system/keepalived.service.
root@test ~]# mysql -uroot -h192.168.225.250 -phxd123456!
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 283
Server version: 5.7.23 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dubai123 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
MySQL [(none)]>
//从库的名字判断出这是我们所设置的master
//写控制keepalived的脚本和发送邮件的脚本
[root@hxdserver ~]# mkdir /scripts/
[root@hxdserver ~]# vim /scripts/check_my.sh
#!/bin/bash
mysql_status=`ps -ef | grep mysql | grep -Ev "grep|$0" | wc -l`
if [ $mysql_status -eq 0 ];
systemctl stop keepalived
fi
[root@hxdserver ~]# chmod +x /scripts/check_my.sh
[root@hxdserver ~]# vim /scripts/notify.sh
#!/bin/bash
VIP=$1
sendmail (){
subject="${VIP} ip address drift" content="`date +'%F-%H%M%S'`:`ip a | grep eth0 | grep inet | head -1 | awk -F '[ /]+' '{print $3}'` change to master"
echo $conter | mail -s "$subject" [email protected]
}
sendmail
[root@hxdserver ~]# chmod +x /scripts/notify.sh
//再修改keepalived配置文件
[root@hxdserver ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb01
}
vrrp_script mysql_check {
script "/scripts/check_my.sh"
interval 1
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass dubai
}
virtual_ipaddress {
192.168.225.250
}
track_script {
mysql_check
}
notify_master "/scripts/notify.sh 192.168.225.250"
}
virtual_server 192.168.225.250 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.225.128 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.225.129 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@bogon ~]# mkdir /scripts/
[root@bogon ~]# vim /scripts/notify.sh
#!/bin/bash
sendmail (){
subject="${VIP} ip address drift" content="`date +'%F-%H%M%S'`:`ip a | grep ens33 | grep inet | head -1 | awk -F '[ /]+' '{print $3}'` change to master"
echo $conter | mail -s "$subject" [email protected]
}
sendmail
[root@bogon ~]# chmod +x /scripts/notify.sh
//修改keepalived配置文件
[root@bogon ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id lb02
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass dubai
}
virtual_ipaddress {
192.168.225.250
}
notify_master "/scripts/notify.sh 192.168.225.250"
}
virtual_server 192.168.225.250 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.225.128 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.225.129 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@hxdserver ~]# yum -y install mailx
在192.168.225.130(test)上访问虚拟IP显示的是master的数据库
[root@test ~]# mysql -uroot -h192.168.225.250 -phxd123456!
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 283
Server version: 5.7.23 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dubai123 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
//192.168.225.128
[root@hxdserver ~]# service mysqld stop
Shutting down MySQL.. SUCCESS!
[root@hxdserver ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@test ~]# mysql -uroot -h192.168.225.250 -phxd123456!
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 1555
Server version: 5.7.23 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| hxd123 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
MySQL [(none)]>
//192.168.225.128
[root@hxdserver ~]# systemctl start mysqld
[root@hxdserver ~]# systemctl start keepalived
//192.168.225.129
[root@bogon ~]# systemctl stop keepalived.service
//192.168.225.128
[root@hxdserver ~]# ip a
1: lo: mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:4b:7c:b8 brd ff:ff:ff:ff:ff:ff
inet 192.168.225.128/24 brd 192.168.225.255 scope global dynamic ens33
valid_lft 912sec preferred_lft 912sec
inet 192.168.225.250/32 scope global ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe4b:7cb8/64 scope link
valid_lft forever preferred_lft forever
//192.168.225.130
[root@test ~]# mysql -uroot -h192.168.225.250 -phxd123456!
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MySQL connection id is 99
Server version: 5.7.23 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| dubai123 |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)