一、系統環境
主機:兩台CentOS7.6虛擬機
IP地址:master01(IP:192.168.27.47),master02(IP:192.168.27.57),VIP:192.168.27.100
軟件:mysql-8.0.17-linux-glibc2.12-x86_64.tar.xz,Keepalived(yum源)
二、實現過程
2.1、實現MySQL的安裝配置
(1) 下載MySQL的安裝包,下載地址: https://downloads.mysql.com/archives/community/
[root@mysql-master01 ~]# ll mysql-8.0.17-linux-glibc2.12-x86_64.tar.xz -rw-r--r-- 1 root root 480209016 May 9 11:25 mysql-8.0.17-linux-glibc2.12-x86_64.tar.xz [root@mysql-master02 ~]# ll mysql-8.0.17-linux-glibc2.12-x86_64.tar.xz -rw-r--r-- 1 root root 480209016 May 9 11:25 mysql-8.0.17-linux-glibc2.12-x86_64.tar.xz
(2) 兩台主機都創建mysql用戶,以master01為例
[root@mysql-master01 ~]# useradd -r -s /sbin/nologin mysql
(3) 解壓MySQL安裝包,創建軟鏈接
[root@mysql-master01 ~]# tar -Jxvf mysql-8.0.17-linux-glibc2.12-x86_64.tar.xz -C /usr/local/ [root@mysql-master01 ~]# ln -s /usr/local/mysql-8.0.17-linux-glibc2.12-x86_64 /usr/local/mysql
(4) 初始化數據庫
[root@mysql-master01 ~]# cd /usr/local/mysql [root@mysql-master01 mysql]# ./bin/mysqld --initialize --user=mysql --datadir=/data/mysql 2020-05-11T02:55:47.592126Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. 2020-05-11T02:55:47.673278Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.17-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.17) initializing of server in progress as process 56424 2020-05-11T02:56:00.647204Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: W:66pBoq;hkF #root連接數據庫的初始密碼 2020-05-11T02:56:04.547704Z 0 [System] [MY-013170] [Server] /usr/local/mysql-8.0.17-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.17) initializing of server has completed [root@mysql-master02 mysql]# ./bin/mysqld --initialize --user=mysql --datadir=/data/mysql 2020-05-11T02:56:54.701359Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release. 2020-05-11T02:56:54.780973Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.17-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.17) initializing of server in progress as process 56410 2020-05-11T02:57:07.447552Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: <ga,2Yf&2:qA #root連接數據庫的初始密碼 2020-05-11T02:57:11.244150Z 0 [System] [MY-013170] [Server] /usr/local/mysql-8.0.17-linux-glibc2.12-x86_64/bin/mysqld (mysqld 8.0.17) initializing of server has completed
(5) 修改配置文件 /etc/my.cnf
[root@mysql-master01 mysql]# vim /etc/my.cnf [mysqld] port=3306 datadir=/data/mysql socket=/data/mysql/mysql.sock character-set-server=utf8mb4 innodb_file_per_table=on skip_name_resolve=on log-error=/data/mysql/log/mysql.log pid-file=/data/mysql/run/mysql.pid # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Settings user and group are ignored when systemd is used. # If you need to run mysqld under a different user or group, # customize your systemd unit file for mariadb according to the # instructions in http://fedoraproject.org/wiki/Systemd [client] socket=/data/mysql/mysql.sock # # include all files from the config directory # !includedir /etc/my.cnf.d #創建日志目錄與文件,防止啟動報錯 [root@mysql-master01 ~]# mkdir /data/mysql/{log,run} [root@mysql-master01 ~]# touch /data/mysql/log/mysql.log [root@mysql-master01 ~]# chown -R mysql:mysql /data/mysql
(7) 准備啟動腳本,並配置環境變量
[root@mysql-master01 mysql]# cp support-files/mysql.server /etc/init.d/mysqld [root@mysql-master01 mysql]# chkconfig --add mysqld [root@mysql-master01 mysql]# echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh [root@mysql-master01 mysql]# . /etc/profile.d/mysql.sh
(8) 啟動數據庫服務,並修改root用戶密碼
[root@mysql-master01 ~]# service mysqld start Starting MySQL........... SUCCESS! [root@mysql-master01 mysql]# mysqladmin -uroot -p"W:66pBoq;hkF" password '123456' #修改密碼方式一,不安全,不推薦 [root@mysql-master02 ~]# mysql -u root -p Enter password: #輸入mysql-master02的數據庫密碼:<ga,2Yf&2:qA Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.17 Copyright (c) 2000, 2019, 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> alter user 'root'@'localhost' identified by '123456'; #修改密碼方式二 Query OK, 0 rows affected (0.03 sec)
2.2、實現MySQL的雙主配置
(1) 修改mysql-master01的配置文件,可以直接修改 /etc/my.cnf 文件,也可以在 /etc/my.cnf.d/ 下新建一個配置文件
[root@mysql-master01 ~]# vim /etc/my.cnf.d/mysql-master.cnf [mysqld] server_id = 1 log-bin = /data/mysql/log/log_bin binlog-format = ROW #指定二進制格式 relay-log = /data/mysql/log/relay-bin relay-log-index = relay-bin.index auto_increment_offset = 1 #起始值,一般填寫第n台主機mysql auto_increment_increment = 2 #步進值auto_imcrement 。一般有n台主mysql就填n #binlog_do_db = DBNAME #設置要同步的數據庫 #replicate-ignore-db = test #設置不同步的數據庫 sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
(2) 修改mysql-master02的配置文件
[root@mysql-master02 ~]# vim /etc/my.cnf.d/mysql-master.cnf [mysqld] server_id = 2 log-bin = /data/mysql/log/log_bin binlog-format = ROW relay-log = /data/mysql/log/relay-bin relay-log-index = relay-bin.index auto_increment_offset = 2 auto_increment_increment = 2 #binlog_do_db = DBNAME #replicate-ignore-db = test sql_mode = NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
(3) 在mysql-master01與mysql-master02上分別創建授權用戶
[root@mysql-master01 ~]# mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 10 Server version: 8.0.17 MySQL Community Server - GPL mysql> create user 'repl'@'192.168.27.%' identified with mysql_native_password by 'repl123456'; Query OK, 0 rows affected (0.04 sec) mysql> grant replication slave on *.* to 'repl'@'192.168.27.%'; Query OK, 0 rows affected (0.02 sec) mysql> flush privileges; Query OK, 0 rows affected (0.02 sec) mysql> show master status; +----------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +----------------+----------+--------------+------------------+-------------------+ | log_bin.000001 | 862 | | | | +----------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec) #以下為mysql-master02上的操作 [root@mysql-master02 ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.0.17 MySQL Community Server - GPL mysql> create user 'repl'@'192.168.27.%' identified with mysql_native_password by 'repl123456'; Query OK, 0 rows affected (0.05 sec) mysql> grant replication slave on *.* to 'repl'@'192.168.27.%'; Query OK, 0 rows affected (0.02 sec) mysql> flush privileges; Query OK, 0 rows affected (0.02 sec) mysql> show master status; +----------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +----------------+----------+--------------+------------------+-------------------+ | log_bin.000001 | 862 | | | | +----------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
(4) 在mysql-master02上配置同步信息
mysql> change master to -> master_host='192.168.27.47', -> master_user='repl', -> master_password='repl123456', -> master_log_file='log_bin.000001', -> master_log_pos=862; Query OK, 0 rows affected, 2 warnings (0.11 sec) mysql> start slave; #啟動slave同步進程 Query OK, 0 rows affected (0.05 sec) mysql> show slave status\G; #查看slave狀態,看是否有錯誤
(5) 在mysql-master01上配置同步信息
mysql> change master to -> master_host='192.168.27.57', -> master_user='repl', -> master_password='repl123456', -> master_log_file='log_bin.000001', -> master_log_pos=862; Query OK, 0 rows affected, 2 warnings (0.12 sec) mysql> start slave; Query OK, 0 rows affected (0.05 sec) mysql> show slave status\G;
(6) 測試同步
#在mysql-master01創建一個庫testA mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.06 sec) mysql> create database testA; Query OK, 1 row affected (0.03 sec) #在mysql-master02查看,可以看到同步了 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | testA | +--------------------+ 5 rows in set (0.06 sec) #再在mysql-master02創建一個庫testB mysql> create database testB; Query OK, 1 row affected (0.03 sec) #在mysql-master01上查看,可以看到也同步了 mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | testA | | testB | +--------------------+ 6 rows in set (0.01 sec)
2.3、實現MySQL雙主的高可用配置
(1) 兩台主機都安裝Keepalived服務,此處使用光盤自帶yum源安裝
[root@mysql-master01 ~]# yum install -y keepalived [root@mysql-master02 ~]# yum install -y keepalived
(2) 修改mysql-master01上的keepalived配置
[root@mysql-master01 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from root@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id master01 vrrp_skip_check_adv_addr
vrrp_iptables vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } #定義檢查腳本 vrrp_script check_mysql_status { script "/etc/keepalived/chk_mysql.sh" interval 2 weight -50 fall 3 rise 5 timeout 3 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 100 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.27.100/24 dev eth0 label eth0:0 } track_script { check_mysql_status } }
(3) 修改mysql-master02上的keepalived配置
[root@mysql-master02 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from root@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id master02 vrrp_skip_check_adv_addr
vrrp_iptables vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script check_mysql_status { script "/etc/keepalived/chk_mysql.sh" interval 2 weight -50 fall 3 rise 5 timeout 3 } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 51 priority 80 nopreempt advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.27.100/24 dev eth0 label eth0:0 } track_script { check_mysql_status } }
(4) 編寫檢查腳本,兩台主機都一樣
[root@mysql-master01 ~]# vim /etc/keepalived/chk_mysql.sh #!/bin/bash mysqlcmd="/usr/local/mysql/bin/mysql" user="root" password="123456" $mysqlcmd -u$user -p$password -e "show status;" &> /dev/null if [ $? -eq 0 ];then echo "mysql_status=1" exit 0 else /usr/bin/systemctl stop keepalived fi [root@mysql-master01 ~]# chmod +x /etc/keepalived/chk_mysql.sh [root@mysql-master01 ~]# scp /etc/keepalived/chk_mysql.sh 192.168.27.57:/etc/keepalived/
(5) 啟動兩台主機的keepalived服務,並設為開機啟動
[root@mysql-master01 ~]# systemctl start keepalived [root@mysql-master01 ~]# systemctl enable keepalived [root@mysql-master02 ~]# systemctl start keepalived [root@mysql-master02 ~]# systemctl enable keepalived
(6) 測試
#剛開始時,VIP在mysql-master01上 [root@mysql-master01 ~]# ip a|grep 192.168.27.100 inet 192.168.27.100/24 scope global secondary eth0:0 #關閉mysql-master01上的keepalived,VIP也轉移到了mysql-master02上 [root@mysql-master01 ~]# systemctl stop keepalived [root@mysql-master01 ~]# ip a|grep 192.168.27.100 [root@mysql-master02 keepalived]# ip a|grep 192.168.27.100 inet 192.168.27.100/24 scope global secondary eth0:0 #重新啟動mysql-master01的keepalived,可以看到VIP還是在mysql-master02上,因為是非搶占模式,是正確的 [root@mysql-master01 ~]# systemctl start keepalived [root@mysql-master01 ~]# ip a|grep 192.168.27.100 [root@mysql-master02 keepalived]# ip a|grep 192.168.27.100 inet 192.168.27.100/24 scope global secondary eth0:0 #接着,關閉mysql-master02上mysql服務,制造mysql故障 #此時,mysql-master02上的keepalived服務將被腳本控制關閉了,並且VIP應該轉移至mysql-master01上 [root@mysql-master02 ~]# service mysqld stop Shutting down MySQL.......... SUCCESS! [root@mysql-master02 ~]# ip a|grep 192.168.27.100 [root@mysql-master02 ~]# ps -ef|grep keepalived root 10478 6769 0 11:37 pts/0 00:00:00 grep --color=auto keepalived #以下可以看到,VIP已轉移到mysql-master01上 [root@mysql-master01 ~]# ip a|grep 192.168.27.100 inet 192.168.27.100/24 scope global secondary eth0:0