一、安装 MySQL
解压
## 卸载系统自带 mariadb
[root@data-01 ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@data-01 ~]# rpm -e --nodeps mariadb-libs-5.5.60-1.el7_5.x86_64
## 远程复制安装包并解压
[root@data-01 ~]# scp 192.168.0.92:/usr/local/src/* /usr/local/src/
[root@data-01 ~]# tar zxvf /usr/local/src/mysql-5.7.29-el7-x86_64.tar.gz -C /usr/local/
[root@data-01 ~]# ln -s /usr/local/mysql-5.7.29-el7-x86_64 /usr/local/mysql
添加运行 mysql 服务的 linux 用户
[root@data-01 ~]# cd /usr/local/mysql
# 添加运行 mysql 的伪用户(不需要登录,只为运行 mysql 服务),-r 系统用户,-s 登录shell
[root@data-01 mysql]# groupadd mysql
[root@data-01 mysql]# useradd -r -g mysql -s /bin/false mysql
设置 mysql 环境变量
## 设置 MySQL 的环境变量
[root@data-01 mysql]# vim /etc/profile.d/mysql-path.sh
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
[root@data-01 mysql]# source /etc/profile.d/mysql-path.sh
[root@data-01 mysql]# mysql --version
mysql Ver 14.14 Distrib 5.7.29, for el7 (x86_64) using EditLine wrapper
初始化 mysql 数据目录
## 初始化数据目录
[root@data-01 mysql]# mkdir mysql-files
[root@data-01 mysql]# chmod 750 mysql-files
[root@data-01 mysql]# chown -R mysql:mysql /usr/local/mysql/
# mysql-files 权限信息如下(ll 命令)
# drwxr-x---. 2 mysql mysql 6 5月 15 11:18 data
# 在系统 root 帐号下运行 mysqld,要添加命令选项 --user=mysql,这样 mysql service 才能正常读写 数据目录
# --initialize-insecure 选项表示使用空密码初始化 'root'@'localhost' 账户
[root@data-01 mysql]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/mysql-files
2020-05-15T10:32:27.535336Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-05-15T10:32:28.740104Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-05-15T10:32:28.826378Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-05-15T10:32:28.886516Z 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: 60588ae2-9697-11ea-a152-00163ed7d8f0.
2020-05-15T10:32:28.887901Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-05-15T10:32:29.841822Z 0 [Warning] CA certificate ca.pem is self signed.
2020-05-15T10:32:30.065992Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
启动 mysql 服务
# 启动服务
[root@data-01 mysql]# mysqld_safe --user=mysql --datadir=/usr/local/mysql/mysql-files --basedir=/usr/local/mysql &
[1] 23025
[root@data-01 mysql]# Logging to '/usr/local/mysql/mysql-files/data-01.err'.
2020-05-15T11:43:46.931645Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/mysql-files
#查看服务
[root@data-01 mysql]# ps -ef | grep mysql
root 7630 28808 0 19:54 pts/0 00:00:00 grep --color=auto mysql
root 23025 28808 0 19:43 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --user=mysql --datadir=/usr/local/mysql/mysql-files --basedir=/usr/local/mysql
mysql 23129 23025 0 19:43 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/mysql-files --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=data-01.err --pid-file=data-01.pid
设置 mysql root 用户密码
# 登录 mysql
[root@data-01 mysql]# mysql -uroot -p
# 设置 root 用户密码
mysql> alter user 'root'@'localhost' identified by 'root-password';
修改配置文件
# 添加配置文件
[root@data-01 mysql]# vim /etc/my.cnf
/ect/my.conf
[mysqld]
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/mysql-files
socket=/tmp/mysql.sock
user=mysql
symbolic-links=0
lower_case_table_names=1
# 允许最大连接数
max_connections=1000
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
max_allowed_packet=16M
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[client]
port=3306
[mysqld_safe]
log-error=/usr/local/mysql/log/mysqld.log
重启 MySQL
# 复制启动脚本
[root@data-01 mysql]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# 重启服务
[root@data-01 mysql]# touch /usr/local/mysql/log/mysqld.log
[root@data-01 mysql]# chown mysql:mysql /usr/local/mysql/log/mysqld.log
[root@data-01 mysql]# service mysqld start
Starting MySQL. SUCCESS!
远程访问 MySQL 报错,关闭服务器防火墙或者打开 MySQL 服务端口
# 远程连接连不上,确认端口是否开放
[root@data-01 mysql]# firewall-cmd --zone=public --query-port=3306/tcp
# 开放 3306 端口
[root@data-01 mysql]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
[root@data-01 mysql]# firewall-cmd --reload
在另外一台服务器上安装 MySQL。
在主节点上进行数据库的创建和初始化。
建立两个 MySQL 服务的互为主备关系
1)修改 MySQL 主从服务的配置,添加主从相关配置项,my.cnf 完整配置为下面的内容与上面已有配置内容的合并。
192.168.0.90:
[mysqld]
server-id=1
log-bin=mysql-bin
binlog-do-db=yxf-test
binlog-do-db=fission
slave-skip-errors=all
innodb_flush_log_at_trx_commit=1
sync_binlog=1
binlog_format=mixed
auto-increment-increment=2
auto-increment-offset=1
192.168.0.91:
[mysqld]
server-id=2
log-bin=mysql-bin
binlog-do-db=yxf-test
binlog-do-db=fission
slave-skip-errors=all
innodb_flush_log_at_trx_commit=1
sync_binlog=1
binlog_format=mixed
auto-increment-increment=2
auto-increment-offset=2
2)重启两个 MySQL 服务
192.168.0.90:
[root@data-01 ~]# service mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL. SUCCESS!
192.168.0.90:
[root@data-02 ~]# service mysqld restart
Shutting down MySQL.... SUCCESS!
Starting MySQL. SUCCESS!
4)创建主从同步 MySQL 帐号
192.168.0.90 和 192.168.0.91 执行相同操作:
mysql> create user 'repl'@'%' identified by 'NotRealPassword';
Query OK, 0 rows affected (0.00 sec)
mysql> grant replication slave on *.* to 'repl'@'%';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
5) 锁定当前主数据库,阻止写库操作,只允许读库,进行数据库备份
192.168.0.90:
[root@data-01 ~]# mysql -h192.168.0.90 -uroot -p
Enter password:
ERROR 1129 (HY000): Host '192.168.0.90' is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts'
[root@data-01 ~]# mysqladmin flush-hosts -p
Enter password:
[root@data-01 ~]# mysql -h192.168.0.90 -uroot -p
Enter password:
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec)
[root@data-01 ~]# mysqldump --databases yxf-test fission > ./yxf-and-fission.dump.db -p
Enter password:
[root@data-01 ~]# ll -h ./yxf-and-fission.dump.db
-rw-r--r--. 1 root root 14M 5月 21 21:03 ./yxf-and-fission.dump.db
6)将备份数据库导入到从库中
192.168.0.90:
# 复制备份数据文件到从服务器
[root@data-01 ~]# scp ./yxf-and-fission.dump.db 192.168.0.91:~/
192.168.0.91:
# 导入到从库
[root@data-02 ~]# mysql < ./yxf-and-fission.dump.db -p
Enter password:
# 确认从库中已导入备份数据
[root@data-02 ~]# mysql -uroot -p
Enter password:
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| fission |
| mysql |
| performance_schema |
| sys |
| yxf-test |
+--------------------+
6 rows in set (0.00 sec)
7)查看 MySQL 主节点状态
192.168.0.90:
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 154
Binlog_Do_DB: yxf-test,fission
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
8)在备份节点设置主从关系
192.168.0.91:
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.90', MASTER_USER='repl', MASTER_PASSWORD='NotRealPassword', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=154;
mysql> start slave;
# 下面值保留部分主要信息
mysql> show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.0.90
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 154
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Master_Server_Id: 1
Master_UUID: 60588ae2-9697-11ea-a152-00163ed7d8f0
Master_Info_File: /usr/local/mysql-5.7.29-el7-x86_64/mysql-files/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
1 row in set (0.00 sec)
9)交换节点执行 8 和 9 两步操作,建立双主关系,释放数据库 read lock
192.168.0.90:
mysql> UNLOCK TABLES;
10)在两个 MySQL 服务节点上分别进行数据更新,在对应的从节点验证数据更新被同步。
二、安装 keepalived
解压、编译、安装
[root@data-01 ~]# tar zxvf /usr/local/src/keepalived-2.0.20.tar.gz -C /usr/local/
[root@data-01 ~]# ln -s /usr/local/keepalived-2.0.20 /usr/local/keepalived
# 依赖根据实际情况安装
[root@data-01 keepalived]# yum install -y gcc-c++
[root@data-01 keepalived]# yum -y install openssl-devel libnl3-devel net-snmp-devel libnfnetlink-devel ipvsadm
# ./configure 不报错 执行 install
[root@data-01 keepalived]# ./configure
[root@data-01 keepalived]# make && make install
# 复制文件
[root@data-01 keepalived]# mkdir /etc/keepalived
[root@data-01 keepalived]# cp /usr/local/keepalived/keepalived/etc/init.d/keepalived /etc/init.d/
[root@data-01 keepalived]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
[root@data-01 keepalived]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
编辑配置文件
# 编辑配置文件
[root@data-01 keepalived]# vim /etc/keepalived/keepalived.conf
/etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
script_user root
enable_script_security
# vrrp_strict # 打开有可能造成物理机的浏览器无法访问应用
# vrrp_garp_interval 0 # 默认就是 0,当前版本配置说明自定义值应该小于1
# vrrp_gna_interval 0 # 默认就是 0,当前版本配置说明自定义值应该小于1
}
vrrp_instance VI_1 {
state BACKUP # 主备节点均设置为 BACKUP 状态
interface eth0
virtual_router_id 51 # 所属虚拟路由组 ID,主备节点相同
priority 100 # 主备优先级相同
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
# 集群虚拟IP,主备节点配置相同
virtual_ipaddress {
192.168.0.94
}
}
virtual_server 192.168.0.94 3306 {
delay_loop 2
lvs_sched wrr # 配置项当前版本 name,和许多网上博客内容不一样
lvs_method DR # 配置项当前版本 name,和许多网上博客内容不一样
persistence_timeout 50
protocol TCP
real_server 192.168.0.91 3306 { # 当前节点 IP 和 mysql 服务端口
weight 3
notify_down /root/shutdown_keepalived.sh
TCP_CHECK {
retry 3 # 配置项当前版本 name,和许多网上贴子不一样
connect_timeout 10
delay_before_retry 3
connect_port 3306
}
}
}
添加并编辑停止 keepalived 服务命令脚本
# 编辑 停止 keepalived 服务脚本
[root@data-01 keepalived]# vim ~/shutdown_keepalived.sh
shutdown_keepalived.sh
#!/bin/bash
pkill keepalived
确认服务器 selinux 状态关闭,防火墙关闭
[root@data-01 ~]# firewall-cmd --state
not running
[root@data-01 ~]# getenforce
Enforcing
# 关闭 selinux
[root@data-01 ~]# setenforce 0
[root@data-01 ~]# getenforce
Permissive
最后,先启动 MySQL 服务,再启动 keepalived 服务,keepalived 服务日志文件默认路径为 /var/log/messages。两节点服务都启动完成之后,通过查看服务器 IP 信息确认虚拟 IP 正确绑定到主节点上。
根据下面的查看信息可以看出,虚拟 IP 地址 192.168.0.94 绑定到主节点 192.168.0.90 上。
当前主节点:
# 当前主节点
[root@data-01 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:16:3e:d7:d8:f0 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.90/24 brd 192.168.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet 192.168.0.94/32 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::1e2d:8b9f:f605:6bbd/64 scope link noprefixroute
valid_lft forever preferred_lft forever
当前备份节点
[root@data-02 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:16:3e:57:c3:b6 brd ff:ff:ff:ff:ff:ff
inet 192.168.0.91/24 brd 192.168.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::ffc:9908:8b02:9e0d/64 scope link noprefixroute
valid_lft forever preferred_lft forever
完成