IP地址 | 服务器/角色 | 安装服务 |
---|---|---|
192.168.91.131 | master | Keepalived、MariaDB-server、MariaDB |
192.168.91.132 | backup | Keepalived、MariDB-server、MariaDB |
192.168.91.133 | 测试连接mysql | MariaDB |
192.168.91.254(VIP) | master、backup | ----------------------------------------- |
[root@master ~]# curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
[root@master ~]# sed -i 's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo
[root@master ~]# yum -y install epel-release
[root@master ~]# yum -y install keepalived mariadb mariadb-server
[root@master ~]# cp -a /etc/keepalived/keepalived.conf{,.bak}
[root@master ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id 001
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.91.254
}
}
virtual_server 192.168.91.254 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.91.131 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.91.132 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@backup ~]# cp -a /etc/keepalived/keepalived.conf{,.bak}
[root@backup ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id 002
}
vrrp_instance VI_1 {
state BACKUP
interface eno16777736
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.91.254
}
}
virtual_server 192.168.91.254 3306 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.91.131 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.91.132 3306 {
weight 1
TCP_CHECK {
connect_port 3306
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
[root@master ~]# systemctl start keepalived
[root@master ~]# ps aux | grep keepalived
root 12929 0.0 0.1 118624 1364 ? Ss 04:26 0:00 /usr/sbin/keepalived -D
root 12930 0.0 0.2 118740 2532 ? S 04:26 0:00 /usr/sbin/keepalived -D
root 12931 0.0 0.1 118624 1832 ? S 04:26 0:00 /usr/sbin/keepalived -D
## 在master主机上查看 ##
[root@master ~]# ip a
2: eno16777736: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:34:95:c3 brd ff:ff:ff:ff:ff:ff
inet 192.168.91.131/24 brd 192.168.91.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet 192.168.91.254/32 scope global eno16777736
valid_lft forever preferred_lft forever
## 在backup上查看 ##
[root@backup ~]# ip a
2: eno16777736: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:c7:d2:e7 brd ff:ff:ff:ff:ff:ff
inet 192.168.91.132/24 brd 192.168.91.255 scope global eno16777736
valid_lft forever preferred_lft forever
[root@master ~]# systemctl start mariadb
[root@master ~]# systemctl enable mariadb
ln -s '/usr/lib/systemd/system/mariadb.service' '/etc/systemd/system/multi-user.target.wants/mariadb.service'
[root@master ~]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 50 *:3306 *:*
[root@master ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): ##按回车
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] y
New password: ##输入密码
Re-enter new password: ##确认密码
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] y ##移除匿名用户
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n ##不禁止root远程登录
... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] y ##移除测试数据库
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] y ##重新加载特权表
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
注:也可以使用mysqladmin设置mysql密码
[root@backup ~]# mysqladmin -uroot password 123456
[root@master ~]# mysql -uroot -p123456
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> grant all on keepalived.* to [email protected] identified by '123456';
Query OK, 0 rows affected (0.01 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> create database keepalived;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use keepalived;
Database changed
MariaDB [keepalived]> create table master(id int null,name char null);
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> create database keepalived;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> use keepalived;
Database changed
MariaDB [keepalived]> create table backup(id int null,name char null);
Query OK, 0 rows affected (0.00 sec)
[root@localhost ~]# mysql -ukeepalived -p123456 -h192.168.91.254
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 16
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
[root@master ~]# mkdir /etc/keepalived/scripts
[root@master ~]# vim /etc/keepalived/scripts/check_m.sh
#!/bin/bash
pidof mysqld
if [ $? -ne 0 ];then
systemctl stop keepalived
fi
[root@master ~]# vim /etc/keepalived/keepalived.conf
global_defs {
router_id 001
}
vrrp_script mysql_check {
script "/etc/keepalived/scripts/check_m.sh"
interval 1
weight -20
}
......
virtual_ipaddress {
192.168.91.254
}
track_script {
mysql_check
}
......
[root@master ~]# chmod +x /etc/keepalived/scripts/check_m.sh
[root@master ~]# systemctl restart keepalived
[root@master ~]# ps aux | grep -v 'grep' | grep keepalived
root 13364 0.0 0.1 118624 1368 ? Ss 06:36 0:00 /usr/sbin/keepalived -D
root 13365 0.0 0.2 118740 2532 ? S 06:36 0:00 /usr/sbin/keepalived -D
root 13366 0.0 0.2 120720 2456 ? S 06:36 0:00 /usr/sbin/keepalived -D
[root@master ~]# systemctl stop mariadb
[root@master ~]# ps aux | grep -v 'grep' | grep keepalived
## master主机上无VIP地址 ##
[root@master ~]# ip a
2: eno16777736: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:34:95:c3 brd ff:ff:ff:ff:ff:ff
inet 192.168.91.131/24 brd 192.168.91.255 scope global eno16777736
valid_lft forever preferred_lft forever
## backup主机上有VIP地址 ##
[root@backup ~]# ip a
2: eno16777736: mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:c7:d2:e7 brd ff:ff:ff:ff:ff:ff
inet 192.168.91.132/24 brd 192.168.91.255 scope global eno16777736
valid_lft forever preferred_lft forever
inet 192.168.91.254/32 scope global eno16777736
valid_lft forever preferred_lft forever
[root@localhost ~]# mysql -ukeepalived -p123456 -h192.168.91.254
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 82
Server version: 5.5.60-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> exit
Bye