二、配置Mysql主從同步react
1)準備兩臺mariadb數據庫 centos7上的mariadb爲主庫,centos6上的mariadb爲從庫 mariadb 編譯安裝請參考http://www.javashuo.com/article/p-nmtmgzjv-bn.htmllinux
2)在主庫上開啓log-bin日誌,並配置主庫和從庫的server-id(組從庫server-id不能相同),若是從庫須要級聯其餘從庫需開啓log-binsql
主庫配置文件數據庫
一、編寫腳本,支持讓用戶自主選擇,使用mysqldump仍是xtraback全量備份。html
[root@test-centos7-node1 scripts]# cat chose_backup_mysql.sh #!/bin/bash # #************************************************************************ #Author: qiuhom #QQ: 467697313 #mail: qiuhom467697313@qq.com #Date: 2020-01-12 #FileName: chose_backup_mysql.sh #URL: https://www.cnblogs.com/qiuhom-1874/ #Description: #Copyright (C): 2020 All rights reserved #************************************************************************ [ -f /etc/init.d/functions ] && . /etc/init.d/functions fun_mysqldump(){ if `which mysqldump &> /dev/null` ;then mysqldump_cmd=`which mysqldump` else yum_cmd_path=`which yum` $yum_cmd_path install mariadb -y &> /dev/null [ $? -eq 0 ] && echo "mysqldump is installed ,please reselect " && exit 1 fi if [ "$passwd" != "null" ];then ${mysqldump_cmd} -u$user -p$passwd -h$host -A --compact > $backup_file else ${mysqldump_cmd} -u$user -h$host -A --compact > $backup_file fi [ $? -eq 0 ] && action " backup successful ,please cat $backup_file" /bin/true || action "mariadb backup is failed " /bin/false exit 0 } try_connection_mariadb(){ if [ "$2" != "null" ];then if `mysql -u"$1" -p"$2" -h"$3" -e "show databases" &> /dev/null` ;then user=$1 passwd=$2 host=$3 else echo "connection is lose , please check user or passwd or host " && exit 3 fi else if `mysql -u"$1" -h"$3" -e "show databases" &> /dev/null` ;then user=$1 host=$3 else echo "connection is lose , please check user or host " && exit 3 fi fi } check_backup_path(){ [ $# -eq 0 ] && backup_file="" if [[ $1 =~ ^(\/.*\/)$ ]];then [ ! -e $1 ] && mkdir -p $1 backup_file="${1}all_backup.sql" elif [[ $1 =~ ^([^\/].*\/)$ ]];then backup_path="${PWD}/$1" [ ! -e ${backup_path} ] && mkdir -p ${backup_path} backup_file="${backup_path}all_backup.sql" elif [[ $1 =~ ^(\/.*[^\/]$) ]];then dir=`dirname $1` [ ! -e "$dir" ] && mkdir -p $dir backup_file="$1" elif [[ $1 =~ ^([^\/].*[^\/]$) ]];then backup_path="${PWD}/$1" [ ! -e `dirname ${backup_path}` ] && mkdir -p `dirname ${backup_path}` backup_file="${backup_path}" else echo "you input backup file is error" && exit 4 fi } set_default_user_pass_host(){ [ "$1" != "" ] && user=$user || user=$USER [ "$2" != "" ] && passwd=$passwd || passwd="null" [ "$3" != "" ] && host=$host || host="localhost" } input_user_passwd_host(){ read -p "please input user(default $USER):" user read -p "please input passwd(default 'null'):" passwd read -p "please input host(default 127.0.0.1):" host } fun_xtrabackup(){ [ ! -e `which xtrabackup &> /dev/null` ] && yum install percona-xtrabackup -y &> /dev/null [ ! -e $backup_dir ] && mkdir -p $backup_dir if [ "$passwd" != "null" ];then xtrabackup --user=$1 --password=$2 --host=$3 --backup --target-dir=$4 &> /dev/null else xtrabackup --user=$1 --host=$3 --backup --target-dir=$4 &> /dev/null fi [ $? -eq 0 ] && action "xtrabackup completed OK!" /bin/true || action "xtrabackup completed failed" /bin/false exit } check_target_dir(){ [ $# -eq 0 ] && backup_dir="" if [[ $1 =~ ^(\/.*\/)$ ]];then [ ! -e $1 ] && mkdir -p $1 backup_dir=$1 elif [[ $1 =~ ^([^\/].*\/)$ ]];then backup_path="${PWD}/$1" [ ! -e ${backup_path} ] && mkdir -p ${backup_path} backup_dir="${backup_path}" else echo "target-dir must is directory " && exit 5 fi } while true do cat << EOF Please input a number choose you backup tool 1.mysqldump 2.xtrabackup 3.quit EOF read -p "you choose:" choose case $choose in 1) input_user_passwd_host set_default_user_pass_host $user $passwd $host if try_connection_mariadb $user $passwd $host ;then read -p "please input mariadb backup file path(default $HOME/backup.all.sql):" backupfile_path [ "$backupfile_path" != "" ] && check_backup_path $backupfile_path || backup_file="$HOME/backup.all.sql" fun_mysqldump $user $passwd $host $backup_file fi ;; 2) input_user_passwd_host set_default_user_pass_host $user $passwd $host if try_connection_mariadb $user $passwd $host ;then read -p "please input target-dir (default $HOME/backup/):" target_dir [ "$target_dir" != "" ] && check_target_dir $target_dir || backup_dir="$HOME/backup/" fun_xtrabackup $user $passwd $host $backup_dir fi ;; 3) echo "bye !!" && exit 6 ;; *) echo "choose error" && exit 7 ;; esac done [root@test-centos7-node1 scripts]#
驗證:node
[root@test-centos7-node1 scripts]# ls / bin boot dev etc home lib lib64 media mnt opt proc root run sbin snap srv sys tmp usr var [root@test-centos7-node1 scripts]# sh chose_backup_mysql.sh Please input a number choose you backup tool 1.mysqldump 2.xtrabackup 3.quit you choose:1 please input user(default root):test please input passwd(default 'null'):admin please input host(default 127.0.0.1):192.168.0.10 please input mariadb backup file path(default /root/backup.all.sql):/backup/mariadb/all.sql backup successful ,please cat /backup/mariadb/all.sql [ OK ] [root@test-centos7-node1 scripts]# ll /backup/mariadb/all.sql -rw-r--r--. 1 root root 512222 Jan 12 10:01 /backup/mariadb/all.sql [root@test-centos7-node1 scripts]# ll / total 20 drwxr-xr-x. 3 root root 21 Jan 12 10:01 backup lrwxrwxrwx. 1 root root 7 Jan 1 07:19 bin -> usr/bin dr-xr-xr-x. 5 root root 4096 Jan 1 07:24 boot drwxr-xr-x. 20 root root 3280 Jan 11 22:55 dev drwxr-xr-x. 76 root root 8192 Jan 12 08:41 etc drwxr-xr-x. 2 root root 6 Jan 12 07:27 home lrwxrwxrwx. 1 root root 7 Jan 1 07:19 lib -> usr/lib lrwxrwxrwx. 1 root root 9 Jan 1 07:19 lib64 -> usr/lib64 drwxr-xr-x. 2 root root 6 Nov 5 2016 media drwxr-xr-x. 2 root root 6 Nov 5 2016 mnt drwxr-xr-x. 2 root root 6 Nov 5 2016 opt dr-xr-xr-x. 129 root root 0 Jan 11 22:54 proc dr-xr-x---. 5 root root 4096 Jan 12 09:56 root drwxr-xr-x. 25 root root 720 Jan 12 08:53 run lrwxrwxrwx. 1 root root 8 Jan 1 07:19 sbin -> usr/sbin drwxr-xr-x. 2 root root 6 Jan 11 03:24 snap drwxr-xr-x. 2 root root 6 Nov 5 2016 srv dr-xr-xr-x. 13 root root 0 Jan 11 22:55 sys drwxrwxrwt. 9 root root 280 Jan 12 10:00 tmp drwxr-xr-x. 13 root root 155 Jan 1 07:19 usr drwxr-xr-x. 19 root root 267 Jan 1 07:24 var [root@test-centos7-node1 scripts]# ll /backup/ total 0 drwxr-xr-x. 2 root root 21 Jan 12 10:01 mariadb [root@test-centos7-node1 scripts]# sh chose_backup_mysql.sh Please input a number choose you backup tool 1.mysqldump 2.xtrabackup 3.quit you choose:2 please input user(default root): please input passwd(default 'null'): please input host(default 127.0.0.1): please input target-dir (default /root/backup/):/backup/xtrabackups/ xtrabackup completed OK! [ OK ] [root@test-centos7-node1 scripts]# ll /backup/ total 0 drwxr-xr-x. 2 root root 21 Jan 12 10:01 mariadb drwxr-xr-x. 6 root root 187 Jan 12 10:03 xtrabackups [root@test-centos7-node1 scripts]# ll /backup/xtrabackups/ total 18456 -rw-r-----. 1 root root 431 Jan 12 10:03 backup-my.cnf drwxr-x---. 2 root root 272 Jan 12 10:03 hellodb -rw-r-----. 1 root root 18874368 Jan 12 10:03 ibdata1 drwxr-x---. 2 root root 4096 Jan 12 10:03 mysql drwxr-x---. 2 root root 4096 Jan 12 10:03 performance_schema drwxr-x---. 2 root root 20 Jan 12 10:03 test -rw-r-----. 1 root root 113 Jan 12 10:03 xtrabackup_checkpoints -rw-r-----. 1 root root 461 Jan 12 10:03 xtrabackup_info -rw-r-----. 1 root root 2560 Jan 12 10:03 xtrabackup_logfile [root@test-centos7-node1 scripts]#
說明:以上腳本實現了用戶選擇一款工具的名稱作備份,而後指定鏈接數據庫的用戶名,若是未指定用戶名,默認是當前Linux登陸用戶的用戶名做爲鏈接mariadb數據庫的用戶;指定鏈接數據庫的密碼,若未指定默認是空;指定數據庫地址,若未指定,默認是localhost 或者127.0.0.1 ,最後還要指定備份到那個的地方,若是使用mysqldump 那麼須要指定其存放文件的全路徑(包括文件名稱,若只是給定了一個目錄,那麼mysqldump默認會在指定的目錄下建立一個all_backup.sql文件),若未指定存放文件的全路徑,則默認放在當前用戶家目錄下,並取名backup.all.sql;若是選擇的是xtrabackup備份工具有份數據,也須要指定其數據庫用戶名,密碼,數據庫地址,以及存放備份文件的目錄,用戶名和密碼和數據庫地址 ,若都沒有指定,那麼用戶名就是用的當前Linux登陸用戶,密碼爲空,數據庫地址爲localhost或127.0.0.1 同mysqldump 工具的默認值相同。最後就是存放備份數據庫文件目錄,若未指定默認存放在當前用戶的家目錄的backup下存放。mysql
二、配置Mysql主從同步react
1)準備兩臺mariadb數據庫 centos7上的mariadb爲主庫,centos6上的mariadb爲從庫 mariadb 編譯安裝請參考http://www.javashuo.com/article/p-nmtmgzjv-bn.htmllinux
2)在主庫上開啓log-bin日誌,並配置主庫和從庫的server-id(組從庫server-id不能相同),若是從庫須要級聯其餘從庫需開啓log-binsql
主庫配置文件數據庫
[root@test-centos7-node1 ~]# grep -Eiv ^"#|^$" /etc/my.cnf [client] port = 3306 socket = /data/mysql/mysql.sock [mysqld] port = 3306 socket = /data/mysql/mysql.sock skip-external-locking key_buffer_size = 384M max_allowed_packet = 1M table_open_cache = 512 sort_buffer_size = 2M read_buffer_size = 2M read_rnd_buffer_size = 8M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size = 32M thread_concurrency = 8 log-bin=mysql-bin server-id = 1 [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 256M sort_buffer_size = 256M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout [root@test-centos7-node1 ~]#
說明:主庫配置文件中只須要開啓log-bin和server-id 便可centos
從庫配置文件bash
[root@test-centos6-node1 ~]# grep -Eiv ^"#|^$" /etc/my.cnf [client] port = 3306 socket = /data/mysql/mysql.sock [mysqld] port = 3306 socket = /data/mysql/mysql.sock skip-external-locking key_buffer_size = 384M max_allowed_packet = 1M table_open_cache = 512 sort_buffer_size = 2M read_buffer_size = 2M read_rnd_buffer_size = 8M myisam_sort_buffer_size = 64M thread_cache_size = 8 query_cache_size = 32M thread_concurrency = 8 server-id = 2 read_only =ON relay_log =relay-log relay_log_index =relay-log.index [mysqldump] quick max_allowed_packet = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 256M sort_buffer_size = 256M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout [root@test-centos6-node1 ~]#
說明:從庫須要開啓中繼日誌,並把從庫設置爲只讀(普通用戶只能讀,不能修改),server-id 不一樣於主庫便可。更改了主從庫的配置文件後須要重啓服務才能生效。服務器
3)重啓主從庫數據庫,使其配置文件生效
主庫
[root@test-centos7-node1 ~]# /etc/init.d/mysqld restart Restarting mysqld (via systemctl): [ OK ] [root@test-centos7-node1 ~]#
從庫
[root@test-centos6-node1 ~]# /etc/init.d/mysqld restart Shutting down MariaDB.. SUCCESS! Starting MariaDB.200113 09:00:23 mysqld_safe Logging to '/data/mysql/test-centos6-node1.err'. 200113 09:00:23 mysqld_safe Starting mysqld daemon with databases from /data/mysql SUCCESS! [root@test-centos6-node1 ~]#
說明:本次實驗我是源碼編譯安裝的mariadb,因此啓動都是直接用腳本啓動。
4)在主庫上建立用於同步的帳號
MariaDB [(none)]> select user,password,host from mysql.user; +------+----------+--------------------+ | user | password | host | +------+----------+--------------------+ | root | | localhost | | root | | test-centos7-node1 | | root | | 127.0.0.1 | | root | | ::1 | +------+----------+--------------------+ 4 rows in set (0.00 sec) MariaDB [(none)]> grant replication slave on *.* to 'rep_user'@'192.168.0.%' identified by 'admin'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> select user,password,host from mysql.user; +----------+-------------------------------------------+--------------------+ | user | password | host | +----------+-------------------------------------------+--------------------+ | root | | localhost | | root | | test-centos7-node1 | | root | | 127.0.0.1 | | root | | ::1 | | rep_user | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | 192.168.0.% | +----------+-------------------------------------------+--------------------+ 5 rows in set (0.00 sec) MariaDB [(none)]>
說明:受權只須要給replication slave 權限便可,有關mysql建立用戶受權可參考http://www.javashuo.com/article/p-tgcxrxmu-km.html
5)在從庫上測試建立的帳號是否可以登陸到主庫
[root@test-centos6-node1 ~]# mysql -urep_user -padmin -h192.168.0.10 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 22 Server version: 10.2.19-MariaDB-log Source distribution 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)]>
說明:是可以鏈接上主庫的,說明帳號沒有問題。若是建立的帳號沒法鏈接主庫,須要檢查主庫是否開啓了防火牆,檢查帳號是否正確,最後還要檢查主庫的監聽端口等。
5)在主庫上查看二進制文件名和位置點,並記錄
MariaDB [(none)]> show master logs; +------------------+-----------+ | Log_name | File_size | +------------------+-----------+ | mysql-bin.000001 | 328 | +------------------+-----------+ 1 row in set (0.00 sec) MariaDB [(none)]>
6)在從庫上配置鏈接主庫用於複製到帳號信息
MariaDB [(none)]> show slave status\G Empty set (0.00 sec) MariaDB [(none)]> CHANGE MASTER TO -> MASTER_HOST='192.168.0.10', -> MASTER_USER='rep_user', -> MASTER_PASSWORD='admin', -> MASTER_PORT=3306, -> MASTER_LOG_FILE='mysql-bin.000001', -> MASTER_LOG_POS=328; Query OK, 0 rows affected (0.02 sec) MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.0.10 Master_User: rep_user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 328 Relay_Log_File: relay-log.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: No Slave_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 328 Relay_Log_Space: 256 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos: Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: conservative SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: 1 row in set (0.00 sec) MariaDB [(none)]>
說明:change master to 這個命令太長了,可用help change master to 查看其幫助。咱們須要配置好主庫地址,用於複製到帳號,密碼,以及主庫的端口,二進制文件名,二進制日誌位置點信息便可,配置好後就能夠用show slave status\G 查看獲得剛纔咱們配置的信息
7)從庫開啓同步
MariaDB [(none)]> start slave ; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.10 Master_User: rep_user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 328 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 555 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 328 Relay_Log_Space: 858 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos: Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: conservative SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it 1 row in set (0.00 sec) MariaDB [(none)]>
說明:可看到IO線程和sql線程都已是yes的了。到此mariadb的主從複製就作好了,接下來測試
測試:在主庫上導入數據,看看從庫是否可以及時的同步過來
1)主庫導入數據並查看導入到數據
[root@test-centos7-node1 ~]# rz rz waiting to receive. zmodem trl+C ȡ 100% 7 KB 7 KB/s 00:00:01 0 Errors [root@test-centos7-node1 ~]# mysql < hellodb_innodb.sql [root@test-centos7-node1 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 21 Server version: 10.2.19-MariaDB-log Source distribution 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)]> show databases; +--------------------+ | Database | +--------------------+ | hellodb | | information_schema | | mysql | | performance_schema | | test | +--------------------+ 5 rows in set (0.00 sec) MariaDB [(none)]> use hellodb Database changed MariaDB [hellodb]> show tables; +-------------------+ | Tables_in_hellodb | +-------------------+ | classes | | coc | | courses | | scores | | students | | teachers | | toc | +-------------------+ 7 rows in set (0.00 sec) MariaDB [hellodb]> select * from students; +-------+---------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+---------------+-----+--------+---------+-----------+ | 1 | Shi Zhongyu | 22 | M | 2 | 3 | | 2 | Shi Potian | 22 | M | 1 | 7 | | 3 | Xie Yanke | 53 | M | 2 | 16 | | 4 | Ding Dian | 32 | M | 4 | 4 | | 5 | Yu Yutong | 26 | M | 3 | 1 | | 6 | Shi Qing | 46 | M | 5 | NULL | | 7 | Xi Ren | 19 | F | 3 | NULL | | 8 | Lin Daiyu | 17 | F | 7 | NULL | | 9 | Ren Yingying | 20 | F | 6 | NULL | | 10 | Yue Lingshan | 19 | F | 3 | NULL | | 11 | Yuan Chengzhi | 23 | M | 6 | NULL | | 12 | Wen Qingqing | 19 | F | 1 | NULL | | 13 | Tian Boguang | 33 | M | 2 | NULL | | 14 | Lu Wushuang | 17 | F | 3 | NULL | | 15 | Duan Yu | 19 | M | 4 | NULL | | 16 | Xu Zhu | 21 | M | 1 | NULL | | 17 | Lin Chong | 25 | M | 4 | NULL | | 18 | Hua Rong | 23 | M | 7 | NULL | | 19 | Xue Baochai | 18 | F | 6 | NULL | | 20 | Diao Chan | 19 | F | 7 | NULL | | 21 | Huang Yueying | 22 | F | 6 | NULL | | 22 | Xiao Qiao | 20 | F | 1 | NULL | | 23 | Ma Chao | 23 | M | 4 | NULL | | 24 | Xu Xian | 27 | M | NULL | NULL | | 25 | Sun Dasheng | 100 | M | NULL | NULL | +-------+---------------+-----+--------+---------+-----------+ 25 rows in set (0.00 sec) MariaDB [hellodb]>
說明:可看到主庫已經有數據生成
2)從庫查看數據是否同主庫一致
[root@test-centos6-node1 ~]# mysql -e "show databases;" +--------------------+ | Database | +--------------------+ | hellodb | | information_schema | | mysql | | performance_schema | | test | +--------------------+ [root@test-centos6-node1 ~]# mysql -e "use hellodb;show tables;" +-------------------+ | Tables_in_hellodb | +-------------------+ | classes | | coc | | courses | | scores | | students | | teachers | | toc | +-------------------+ [root@test-centos6-node1 ~]# mysql -e "use hellodb;select * from students;" +-------+---------------+-----+--------+---------+-----------+ | StuID | Name | Age | Gender | ClassID | TeacherID | +-------+---------------+-----+--------+---------+-----------+ | 1 | Shi Zhongyu | 22 | M | 2 | 3 | | 2 | Shi Potian | 22 | M | 1 | 7 | | 3 | Xie Yanke | 53 | M | 2 | 16 | | 4 | Ding Dian | 32 | M | 4 | 4 | | 5 | Yu Yutong | 26 | M | 3 | 1 | | 6 | Shi Qing | 46 | M | 5 | NULL | | 7 | Xi Ren | 19 | F | 3 | NULL | | 8 | Lin Daiyu | 17 | F | 7 | NULL | | 9 | Ren Yingying | 20 | F | 6 | NULL | | 10 | Yue Lingshan | 19 | F | 3 | NULL | | 11 | Yuan Chengzhi | 23 | M | 6 | NULL | | 12 | Wen Qingqing | 19 | F | 1 | NULL | | 13 | Tian Boguang | 33 | M | 2 | NULL | | 14 | Lu Wushuang | 17 | F | 3 | NULL | | 15 | Duan Yu | 19 | M | 4 | NULL | | 16 | Xu Zhu | 21 | M | 1 | NULL | | 17 | Lin Chong | 25 | M | 4 | NULL | | 18 | Hua Rong | 23 | M | 7 | NULL | | 19 | Xue Baochai | 18 | F | 6 | NULL | | 20 | Diao Chan | 19 | F | 7 | NULL | | 21 | Huang Yueying | 22 | F | 6 | NULL | | 22 | Xiao Qiao | 20 | F | 1 | NULL | | 23 | Ma Chao | 23 | M | 4 | NULL | | 24 | Xu Xian | 27 | M | NULL | NULL | | 25 | Sun Dasheng | 100 | M | NULL | NULL | +-------+---------------+-----+--------+---------+-----------+ [root@test-centos6-node1 ~]#
說明:可看到從庫把主庫裏新加的庫和表都複製過來了
有關mysql主從複製詳細說明請參考http://www.javashuo.com/article/p-uekmbias-be.html
三、使用MHA實現Mysql高可用。
1)環境說明 3臺centos7爲mariadb數據庫主從複製環境,centos6爲mha管理節點,其中node1爲主從複製主節點,二、3爲從節點
2)前期準備工做,關閉全部服務器上的selinu和防火牆
[root@test-centos7-node1 ~]# systemctl stop firewalld [root@test-centos7-node1 ~]# systemctl is-enabled firewalld enabled [root@test-centos7-node1 ~]# 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@test-centos7-node1 ~]# systemctl is-enabled firewalld disabled [root@test-centos7-node1 ~]# sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config [root@test-centos7-node1 ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX=disabled # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of three two values: # targeted - Targeted processes are protected, # minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection. SELINUXTYPE=targeted [root@test-centos7-node1 ~]# setenforce 0 [root@test-centos7-node1 ~]# getenforce Permissive [root@test-centos7-node1 ~]#
說明:在主從複製全部節點以及管理節點都關閉防火牆和selinux
[root@test-centos6-node1 ~]# /etc/init.d/iptables stop iptables: Setting chains to policy ACCEPT: filter [ OK ] iptables: Flushing firewall rules: [ OK ] iptables: Unloading modules: [ OK ] [root@test-centos6-node1 ~]# chkconfig iptables off [root@test-centos6-node1 ~]# chkconfig --list|grep iptables iptables 0:off 1:off 2:off 3:off 4:off 5:off 6:off [root@test-centos6-node1 ~]# sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/selinux/config [root@test-centos6-node1 ~]# cat /etc/selinux/config # This file controls the state of SELinux on the system. # SELINUX=disabled # enforcing - SELinux security policy is enforced. # permissive - SELinux prints warnings instead of enforcing. # disabled - No SELinux policy is loaded. SELINUX=disabled # SELINUXTYPE= can take one of these two values: # targeted - Targeted processes are protected, # mls - Multi Level Security protection. SELINUXTYPE=targeted [root@test-centos6-node1 ~]# setenforce 0 [root@test-centos6-node1 ~]# getenforce Permissive [root@test-centos6-node1 ~]#
說明:centos6是管理節點,也須要關閉防火牆和selinux,這樣作就是排除後續作實驗,防火牆和selinux帶來的沒必要要的錯誤。
3)搭建mariadb的主從複製
主節點配置文件
[root@test-centos7-node1 my.cnf.d]# cat /etc/my.cnf.d/master.cnf [mysqld] log-bin server_id=1 skip_name_resolve=1 [root@test-centos7-node1 my.cnf.d]#
說明:能夠在server.cnf裏添加以上配置,也能夠在my.cnf裏面加,固然也能夠單獨創建獨立的配置文件,這樣方便管理
從節點配置文件
root@test-centos7-node2 ~]# cat /etc/my.cnf.d/slave.cnf [mysqld] server_id=2 log-bin read_only relay_log_purge=0 skip_name_resolve=1 [root@test-centos7-node2 ~]#
說明:從節點須要加上relay_log_purge=0表示不清除中繼日誌。注意這裏須要說明一點的是,在備用的主節點(未來可能成爲主的服務器)上須要開啓log-bin,server_id 的值不一樣於其餘主機便可
[root@test-centos7-node3 ~]# cat /etc/my.cnf.d/slave.cnf [mysqld] server_id=3 read_only relay_log_purge=0 skip_name_resolve=1 [root@test-centos7-node3 ~]#
4)從新啓動全部節點的數據庫服務,在主庫上查看二進制日誌文件名和日誌位置點
[root@test-centos7-node1 ~]# systemctl restart mariadb [root@test-centos7-node1 ~]# [root@test-centos7-node1 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 2 Server version: 5.5.56-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show master logs; +--------------------+-----------+ | Log_name | File_size | +--------------------+-----------+ | mariadb-bin.000001 | 245 | +--------------------+-----------+ 1 row in set (0.00 sec) MariaDB [(none)]>
說明:之因此查看主庫二進制日誌名稱和位置點數方便待會從庫裏配置
5)主庫建立用於從庫鏈接主庫複製的帳號
MariaDB [(none)]> grant replication slave on *.* to repuser@'192.168.0.%' identified by 'admin'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> select user,host,password from mysql.user; +---------+--------------------+-------------------------------------------+ | user | host | password | +---------+--------------------+-------------------------------------------+ | root | localhost | | | root | test-centos7-node1 | | | root | 127.0.0.1 | | | root | ::1 | | | | localhost | | | | test-centos7-node1 | | | repuser | 192.168.0.% | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | +---------+--------------------+-------------------------------------------+ 7 rows in set (0.00 sec) MariaDB [(none)]>
6)在從庫上測試主庫剛纔創建的帳號是否可登陸主庫
[root@test-centos7-node2 ~]# mysql -urepuser -padmin -h192.168.0.10 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 3 Server version: 5.5.56-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show grants for repuser@'192.168.0.%'; +------------------------------------------------------------------------------------------------------------------------------+ | Grants for repuser@192.168.0.% | +------------------------------------------------------------------------------------------------------------------------------+ | GRANT REPLICATION SLAVE ON *.* TO 'repuser'@'192.168.0.%' IDENTIFIED BY PASSWORD '*4ACFE3202A5FF5CF467898FC58AAB1D615029441' | +------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.01 sec) MariaDB [(none)]>
7)在從庫上配置鏈接主庫進行復制的帳號和二進制日誌名稱及位置點信息
[root@test-centos7-node2 ~]# mysql Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 3 Server version: 5.5.56-MariaDB MariaDB Server Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show slave status\G Empty set (0.01 sec) MariaDB [(none)]> CHANGE MASTER TO -> MASTER_HOST='192.168.0.10', -> MASTER_USER='repuser', -> MASTER_PASSWORD='admin', -> MASTER_PORT=3306, -> MASTER_LOG_FILE='mariadb-bin.000001', -> MASTER_LOG_POS=245; Query OK, 0 rows affected (0.08 sec) MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 192.168.0.10 Master_User: repuser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mariadb-bin.000001 Read_Master_Log_Pos: 245 Relay_Log_File: mariadb-relay-bin.000001 Relay_Log_Pos: 4 Relay_Master_Log_File: mariadb-bin.000001 Slave_IO_Running: No Slave_SQL_Running: No Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 245 Relay_Log_Space: 245 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: NULL Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0 1 row in set (0.01 sec) MariaDB [(none)]>
說明:兩從庫都執行上面相同的change master to 命令便可
8)從庫開啓複製
MariaDB [(none)]> start slave; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.10 Master_User: repuser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mariadb-bin.000001 Read_Master_Log_Pos: 397 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 683 Relay_Master_Log_File: mariadb-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 397 Relay_Log_Space: 979 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 1 row in set (0.00 sec) MariaDB [(none)]>
說明:到此主從複製就你們完成,接下來在主庫上建立用於管理端管理數據庫的帳號
9)在主庫上建立用於管理端管理數據庫的管理賬號
MariaDB [(none)]> grant all on *.* to repmanage@'192.168.0.%' identified by 'admin'; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> select user,host,password from mysql.user; +-----------+--------------------+-------------------------------------------+ | user | host | password | +-----------+--------------------+-------------------------------------------+ | root | localhost | | | root | test-centos7-node1 | | | root | 127.0.0.1 | | | root | ::1 | | | | localhost | | | | test-centos7-node1 | | | repuser | 192.168.0.% | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | | repmanage | 192.168.0.% | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 | +-----------+--------------------+-------------------------------------------+ 8 rows in set (0.00 sec) MariaDB [(none)]>
說明:此刻就能夠去從節點看剛纔在主庫建立的帳號是否同步到從庫裏去了,若是同步了,說明mariadb的主從複製是沒有問題的。
10)在全部節點上作ssh key驗證包括管理節點上,實現雙向key驗證
[root@test-centos6-node1 ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: 99:5f:36:b0:d1:cb:d2:85:f3:fa:65:ac:68:86:0e:e3 root@test-centos6-node1 The key's randomart image is: +--[ RSA 2048]----+ | | | . . | | o + . | | o * = | | S o B . | | . + o . | | o ... +| | . o. oo + | | E..o. o | +-----------------+ [root@test-centos6-node1 ~]# ssh-copy-id 192.168.0.11 The authenticity of host '192.168.0.11 (192.168.0.11)' can't be established. RSA key fingerprint is f7:d4:c0:12:41:4a:46:4e:8b:d6:eb:80:06:ca:5e:fe. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.0.11' (RSA) to the list of known hosts. root@192.168.0.11's password: Now try logging into the machine, with "ssh '192.168.0.11'", and check in: .ssh/authorized_keys to make sure we haven't added extra keys that you weren't expecting. [root@test-centos6-node1 ~]# scp -rp /root/.ssh 192.168.0.10:/root/ The authenticity of host '192.168.0.10 (192.168.0.10)' can't be established. RSA key fingerprint is 7e:4a:a2:53:1b:fa:7b:52:c3:b6:9d:f7:7a:8d:4d:23. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.0.10' (RSA) to the list of known hosts. root@192.168.0.10's password: authorized_keys 100% 405 0.4KB/s 00:00 id_rsa 100% 1675 1.6KB/s 00:00 id_rsa.pub 100% 405 0.4KB/s 00:00 known_hosts 100% 788 0.8KB/s 00:00 [root@test-centos6-node1 ~]# scp -rp /root/.ssh 192.168.0.20:/root/ The authenticity of host '192.168.0.20 (192.168.0.20)' can't be established. RSA key fingerprint is 7e:4a:a2:53:1b:fa:7b:52:c3:b6:9d:f7:7a:8d:4d:23. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.0.20' (RSA) to the list of known hosts. root@192.168.0.20's password: authorized_keys 100% 405 0.4KB/s 00:00 id_rsa 100% 1675 1.6KB/s 00:00 id_rsa.pub 100% 405 0.4KB/s 00:00 known_hosts 100% 1182 1.2KB/s 00:00 [root@test-centos6-node1 ~]# scp -rp /root/.ssh 192.168.0.30:/root/ The authenticity of host '192.168.0.30 (192.168.0.30)' can't be established. RSA key fingerprint is 7e:4a:a2:53:1b:fa:7b:52:c3:b6:9d:f7:7a:8d:4d:23. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.0.30' (RSA) to the list of known hosts. root@192.168.0.30's password: authorized_keys 100% 405 0.4KB/s 00:00 id_rsa 100% 1675 1.6KB/s 00:00 id_rsa.pub 100% 405 0.4KB/s 00:00 known_hosts 100% 1576 1.5KB/s 00:00 [root@test-centos6-node1 ~]#
說明:這樣在管理端作好了ssh key驗證後,管理端能夠任意登陸被管理端,同時被管理端也能夠鏈接管理端。到此全部環境的準備都已經準備好了,接下來裝包
11)在管理端安裝兩個包mha4mysql-manager和mha4mysql-node
[root@test-centos6-node1 ~]# rz rz waiting to receive. zmodem trl+C ȡ 100% 85 KB 85 KB/s 00:00:01 0 Errors-0.el6.noarch.rpm... [root@test-centos6-node1 ~]# rz rz waiting to receive. zmodem trl+C ȡ 100% 35 KB 35 KB/s 00:00:01 0 Errorsel6.noarch.rpm... [root@test-centos6-node1 ~]# ls mha4mysql-manager-0.56-0.el6.noarch.rpm mha4mysql-node-0.56-0.el6.noarch.rpm [root@test-centos6-node1 ~]# yum install mha4mysql-* Loaded plugins: fastestmirror Setting up Install Process Examining mha4mysql-manager-0.56-0.el6.noarch.rpm: mha4mysql-manager-0.56-0.el6.noarch Marking mha4mysql-manager-0.56-0.el6.noarch.rpm to be installed Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Examining mha4mysql-node-0.56-0.el6.noarch.rpm: mha4mysql-node-0.56-0.el6.noarch Marking mha4mysql-node-0.56-0.el6.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mha4mysql-manager.noarch 0:0.56-0.el6 will be installed --> Processing Dependency: perl(Config::Tiny) for package: mha4mysql-manager-0.56-0.el6.noarch --> Processing Dependency: perl(Config::Tiny) for package: mha4mysql-manager-0.56-0.el6.noarch --> Processing Dependency: perl(DBI) for package: mha4mysql-manager-0.56-0.el6.noarch --> Processing Dependency: perl(Log::Dispatch) for package: mha4mysql-manager-0.56-0.el6.noarch --> Processing Dependency: perl(Log::Dispatch) for package: mha4mysql-manager-0.56-0.el6.noarch --> Processing Dependency: perl(Log::Dispatch::File) for package: mha4mysql-manager-0.56-0.el6.noarch --> Processing Dependency: perl(Log::Dispatch::Screen) for package: mha4mysql-manager-0.56-0.el6.noarch --> Processing Dependency: perl(Parallel::ForkManager) for package: mha4mysql-manager-0.56-0.el6.noarch --> Processing Dependency: perl(Parallel::ForkManager) for package: mha4mysql-manager-0.56-0.el6.noarch --> Processing Dependency: perl(Time::HiRes) for package: mha4mysql-manager-0.56-0.el6.noarch ---> Package mha4mysql-node.noarch 0:0.56-0.el6 will be installed --> Processing Dependency: perl(DBD::mysql) for package: mha4mysql-node-0.56-0.el6.noarch --> Running transaction check ---> Package perl-Config-Tiny.noarch 0:2.12-7.1.el6 will be installed ---> Package perl-DBD-MySQL.x86_64 0:4.013-3.el6 will be installed ---> Package perl-DBI.x86_64 0:1.609-4.el6 will be installed ---> Package perl-Log-Dispatch.noarch 0:2.27-1.el6 will be installed --> Processing Dependency: perl(MIME::Lite) for package: perl-Log-Dispatch-2.27-1.el6.noarch --> Processing Dependency: perl(Mail::Send) for package: perl-Log-Dispatch-2.27-1.el6.noarch --> Processing Dependency: perl(Mail::Sender) for package: perl-Log-Dispatch-2.27-1.el6.noarch --> Processing Dependency: perl(Mail::Sendmail) for package: perl-Log-Dispatch-2.27-1.el6.noarch --> Processing Dependency: perl(Params::Validate) for package: perl-Log-Dispatch-2.27-1.el6.noarch ---> Package perl-Parallel-ForkManager.noarch 0:1.20-1.el6 will be installed ---> Package perl-Time-HiRes.x86_64 4:1.9721-144.el6 will be installed --> Running transaction check ---> Package perl-MIME-Lite.noarch 0:3.027-2.el6 will be installed --> Processing Dependency: perl(MIME::Types) >= 1.28 for package: perl-MIME-Lite-3.027-2.el6.noarch --> Processing Dependency: perl(Email::Date::Format) for package: perl-MIME-Lite-3.027-2.el6.noarch ---> Package perl-Mail-Sender.noarch 0:0.8.16-3.el6 will be installed ---> Package perl-Mail-Sendmail.noarch 0:0.79-12.el6 will be installed ---> Package perl-MailTools.noarch 0:2.04-4.el6 will be installed --> Processing Dependency: perl(Date::Parse) for package: perl-MailTools-2.04-4.el6.noarch --> Processing Dependency: perl(Date::Format) for package: perl-MailTools-2.04-4.el6.noarch ---> Package perl-Params-Validate.x86_64 0:0.92-3.el6 will be installed --> Running transaction check ---> Package perl-Email-Date-Format.noarch 0:1.002-5.el6 will be installed ---> Package perl-MIME-Types.noarch 0:1.28-2.el6 will be installed ---> Package perl-TimeDate.noarch 1:1.16-13.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================= Package Arch Version Repository Size ================================================================================================= Installing: mha4mysql-manager noarch 0.56-0.el6 /mha4mysql-manager-0.56-0.el6.noarch 325 k mha4mysql-node noarch 0.56-0.el6 /mha4mysql-node-0.56-0.el6.noarch 102 k Installing for dependencies: perl-Config-Tiny noarch 2.12-7.1.el6 base 23 k perl-DBD-MySQL x86_64 4.013-3.el6 base 134 k perl-DBI x86_64 1.609-4.el6 base 705 k perl-Email-Date-Format noarch 1.002-5.el6 base 16 k perl-Log-Dispatch noarch 2.27-1.el6 epel 71 k perl-MIME-Lite noarch 3.027-2.el6 base 82 k perl-MIME-Types noarch 1.28-2.el6 base 32 k perl-Mail-Sender noarch 0.8.16-3.el6 epel 54 k perl-Mail-Sendmail noarch 0.79-12.el6 epel 28 k perl-MailTools noarch 2.04-4.el6 base 101 k perl-Parallel-ForkManager noarch 1.20-1.el6 epel 27 k perl-Params-Validate x86_64 0.92-3.el6 base 75 k perl-Time-HiRes x86_64 4:1.9721-144.el6 base 49 k perl-TimeDate noarch 1:1.16-13.el6 base 37 k Transaction Summary ================================================================================================= Install 16 Package(s) Total size: 1.8 M Total download size: 1.4 M Installed size: 3.5 M Is this ok [y/N]: y Downloading Packages: (1/14): perl-Config-Tiny-2.12-7.1.el6.noarch.rpm | 23 kB 00:00 (2/14): perl-DBD-MySQL-4.013-3.el6.x86_64.rpm | 134 kB 00:00 (3/14): perl-DBI-1.609-4.el6.x86_64.rpm | 705 kB 00:00 (4/14): perl-Email-Date-Format-1.002-5.el6.noarch.rpm | 16 kB 00:00 (5/14): perl-Log-Dispatch-2.27-1.el6.noarch.rpm | 71 kB 00:00 (6/14): perl-MIME-Lite-3.027-2.el6.noarch.rpm | 82 kB 00:00 (7/14): perl-MIME-Types-1.28-2.el6.noarch.rpm | 32 kB 00:00 (8/14): perl-Mail-Sender-0.8.16-3.el6.noarch.rpm | 54 kB 00:00 (9/14): perl-Mail-Sendmail-0.79-12.el6.noarch.rpm | 28 kB 00:00 (10/14): perl-MailTools-2.04-4.el6.noarch.rpm | 101 kB 00:00 (11/14): perl-Parallel-ForkManager-1.20-1.el6.noarch.rpm | 27 kB 00:00 (12/14): perl-Params-Validate-0.92-3.el6.x86_64.rpm | 75 kB 00:00 (13/14): perl-Time-HiRes-1.9721-144.el6.x86_64.rpm | 49 kB 00:00 (14/14): perl-TimeDate-1.16-13.el6.noarch.rpm | 37 kB 00:00 ------------------------------------------------------------------------------------------------- Total 669 kB/s | 1.4 MB 00:02 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : perl-DBI-1.609-4.el6.x86_64 1/16 Installing : perl-DBD-MySQL-4.013-3.el6.x86_64 2/16 Installing : mha4mysql-node-0.56-0.el6.noarch 3/16 Installing : perl-MIME-Types-1.28-2.el6.noarch 4/16 Installing : perl-Config-Tiny-2.12-7.1.el6.noarch 5/16 Installing : perl-Parallel-ForkManager-1.20-1.el6.noarch 6/16 Installing : perl-Params-Validate-0.92-3.el6.x86_64 7/16 Installing : 4:perl-Time-HiRes-1.9721-144.el6.x86_64 8/16 Installing : perl-Mail-Sender-0.8.16-3.el6.noarch 9/16 Installing : 1:perl-TimeDate-1.16-13.el6.noarch 10/16 Installing : perl-MailTools-2.04-4.el6.noarch 11/16 Installing : perl-Mail-Sendmail-0.79-12.el6.noarch 12/16 Installing : perl-Email-Date-Format-1.002-5.el6.noarch 13/16 Installing : perl-MIME-Lite-3.027-2.el6.noarch 14/16 Installing : perl-Log-Dispatch-2.27-1.el6.noarch 15/16 Installing : mha4mysql-manager-0.56-0.el6.noarch 16/16 Verifying : mha4mysql-manager-0.56-0.el6.noarch 1/16 Verifying : perl-Email-Date-Format-1.002-5.el6.noarch 2/16 Verifying : perl-Mail-Sendmail-0.79-12.el6.noarch 3/16 Verifying : mha4mysql-node-0.56-0.el6.noarch 4/16 Verifying : perl-DBD-MySQL-4.013-3.el6.x86_64 5/16 Verifying : 1:perl-TimeDate-1.16-13.el6.noarch 6/16 Verifying : perl-MIME-Lite-3.027-2.el6.noarch 7/16 Verifying : perl-Mail-Sender-0.8.16-3.el6.noarch 8/16 Verifying : perl-DBI-1.609-4.el6.x86_64 9/16 Verifying : 4:perl-Time-HiRes-1.9721-144.el6.x86_64 10/16 Verifying : perl-Params-Validate-0.92-3.el6.x86_64 11/16 Verifying : perl-MailTools-2.04-4.el6.noarch 12/16 Verifying : perl-Parallel-ForkManager-1.20-1.el6.noarch 13/16 Verifying : perl-Config-Tiny-2.12-7.1.el6.noarch 14/16 Verifying : perl-Log-Dispatch-2.27-1.el6.noarch 15/16 Verifying : perl-MIME-Types-1.28-2.el6.noarch 16/16 Installed: mha4mysql-manager.noarch 0:0.56-0.el6 mha4mysql-node.noarch 0:0.56-0.el6 Dependency Installed: perl-Config-Tiny.noarch 0:2.12-7.1.el6 perl-DBD-MySQL.x86_64 0:4.013-3.el6 perl-DBI.x86_64 0:1.609-4.el6 perl-Email-Date-Format.noarch 0:1.002-5.el6 perl-Log-Dispatch.noarch 0:2.27-1.el6 perl-MIME-Lite.noarch 0:3.027-2.el6 perl-MIME-Types.noarch 0:1.28-2.el6 perl-Mail-Sender.noarch 0:0.8.16-3.el6 perl-Mail-Sendmail.noarch 0:0.79-12.el6 perl-MailTools.noarch 0:2.04-4.el6 perl-Parallel-ForkManager.noarch 0:1.20-1.el6 perl-Params-Validate.x86_64 0:0.92-3.el6 perl-Time-HiRes.x86_64 4:1.9721-144.el6 perl-TimeDate.noarch 1:1.16-13.el6 Complete! [root@test-centos6-node1 ~]
說明:安裝這兩個包須要開啓epel源,由於它們的依賴包有些來自epel源裏
12)在各個被管理端安裝mha4mysql-node包
[root@test-centos7-node1 ~]# ls mha4mysql-node-0.56-0.el6.noarch.rpm [root@test-centos7-node1 ~]# yum install mha4mysql-node-0.56-0.el6.noarch.rpm Loaded plugins: fastestmirror Examining mha4mysql-node-0.56-0.el6.noarch.rpm: mha4mysql-node-0.56-0.el6.noarch Marking mha4mysql-node-0.56-0.el6.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package mha4mysql-node.noarch 0:0.56-0.el6 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================================================= Package Arch Version Repository Size ================================================================================================================================= Installing: mha4mysql-node noarch 0.56-0.el6 /mha4mysql-node-0.56-0.el6.noarch 102 k Transaction Summary ================================================================================================================================= Install 1 Package Total size: 102 k Installed size: 102 k Is this ok [y/d/N]: y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : mha4mysql-node-0.56-0.el6.noarch 1/1 Verifying : mha4mysql-node-0.56-0.el6.noarch 1/1 Installed: mha4mysql-node.noarch 0:0.56-0.el6 Complete! [root@test-centos7-node1 ~]#
說明:到此mha的軟件都已部署完畢,接下來是在管理端創建配置文件
13)在管理節點新建配置文件
[root@test-centos6-node1 ~]# mkdir /etc/mastermh/ [root@test-centos6-node1 ~]# cd /etc/mastermh/ [root@test-centos6-node1 mastermh]# cat >> mariadb.cnf << EOF > [server default] > user=repmanage > password=admin > manager_workdir=/data/mastermha/mariadb/ > manager_log=/data/mastermha/mariadb/manager.log > remote_workdir=/data/mastermha/mariadb/ > ssh_user=root > repl_user=repuser > repl_password=admin > ping_interval=1 > > > [server1] > hostname=192.168.0.10 > candidate_master=1 > [server2] > hostname=192.168.0.20 > candidate_master=1 > [server3] > hostname=192.168.0.30 > EOF [root@test-centos6-node1 mastermh]# cat mariadb.cnf [server default] user=repmanage password=admin manager_workdir=/data/mastermha/mariadb/ manager_log=/data/mastermha/mariadb/manager.log remote_workdir=/data/mastermha/mariadb/ ssh_user=root repl_user=repuser repl_password=admin ping_interval=1 [server1] hostname=192.168.0.10 candidate_master=1 [server2] hostname=192.168.0.20 candidate_master=1 [server3] hostname=192.168.0.30 [root@test-centos6-node1 mastermh]#
說明:此文件的文件能夠說任意名稱,只要本身知道就行,也沒有特定規定放在哪一個位置,看本身的喜愛便可。由於待會啓動mha 咱們是要指定配置文件的路徑,配置文件中主要配置了用於管理mariadb節點的帳號密碼以及ssh管理的用戶以及主從複製到帳號和密碼信息還有就是各個節點的地址,candidate_master=1表示未來可能選舉成爲主節點。ping_interval=1表示檢測主庫的時間間隔,心跳值;配置文件中manager的工做目錄和日誌目錄咱們不須要提早創建好,它這個目錄只要咱們指定了會自動生成的。
14)mha驗證ssh基於KEY驗證是否正常
[root@test-centos6-node1 ~]# masterha_check_ssh --conf=/etc/mastermh/mariadb.cnf Tue Jan 14 08:05:04 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Tue Jan 14 08:05:04 2020 - [info] Reading application default configuration from /etc/mastermh/mariadb.cnf.. Tue Jan 14 08:05:04 2020 - [info] Reading server configuration from /etc/mastermh/mariadb.cnf.. Tue Jan 14 08:05:04 2020 - [info] Starting SSH connection tests.. Tue Jan 14 08:05:05 2020 - [debug] Tue Jan 14 08:05:04 2020 - [debug] Connecting via SSH from root@192.168.0.10(192.168.0.10:22) to root@192.168.0.20(192.168.0.20:22).. Warning: Permanently added '192.168.0.20' (ECDSA) to the list of known hosts. Tue Jan 14 08:05:05 2020 - [debug] ok. Tue Jan 14 08:05:05 2020 - [debug] Connecting via SSH from root@192.168.0.10(192.168.0.10:22) to root@192.168.0.30(192.168.0.30:22).. Warning: Permanently added '192.168.0.30' (ECDSA) to the list of known hosts. Tue Jan 14 08:05:05 2020 - [debug] ok. Tue Jan 14 08:05:06 2020 - [debug] Tue Jan 14 08:05:05 2020 - [debug] Connecting via SSH from root@192.168.0.30(192.168.0.30:22) to root@192.168.0.10(192.168.0.10:22).. Tue Jan 14 08:05:06 2020 - [debug] ok. Tue Jan 14 08:05:06 2020 - [debug] Connecting via SSH from root@192.168.0.30(192.168.0.30:22) to root@192.168.0.20(192.168.0.20:22).. Tue Jan 14 08:05:06 2020 - [debug] ok. Tue Jan 14 08:05:06 2020 - [debug] Tue Jan 14 08:05:04 2020 - [debug] Connecting via SSH from root@192.168.0.20(192.168.0.20:22) to root@192.168.0.10(192.168.0.10:22).. Tue Jan 14 08:05:05 2020 - [debug] ok. Tue Jan 14 08:05:05 2020 - [debug] Connecting via SSH from root@192.168.0.20(192.168.0.20:22) to root@192.168.0.30(192.168.0.30:22).. Warning: Permanently added '192.168.0.30' (ECDSA) to the list of known hosts. Tue Jan 14 08:05:06 2020 - [debug] ok. Tue Jan 14 08:05:06 2020 - [info] All SSH connection tests passed successfully. [root@test-centos6-node1 ~]#
說明:若是沒有報錯表示SSH key驗證是沒有問題的,在配置文件中配置的ssh信息是正確的
15)mha驗證配置文件中配置的主從複製信息是否正確
[root@test-centos6-node1 ~]# masterha_check_repl --conf=/etc/mastermh/mariadb.cnf Tue Jan 14 09:55:54 2020 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Tue Jan 14 09:55:54 2020 - [info] Reading application default configuration from /etc/mastermh/mariadb.cnf.. Tue Jan 14 09:55:54 2020 - [info] Reading server configuration from /etc/mastermh/mariadb.cnf.. Tue Jan 14 09:55:54 2020 - [info] MHA::MasterMonitor version 0.56. Tue Jan 14 09:55:55 2020 - [info] GTID failover mode = 0 Tue Jan 14 09:55:55 2020 - [info] Dead Servers: Tue Jan 14 09:55:55 2020 - [info] Alive Servers: Tue Jan 14 09:55:55 2020 - [info] 192.168.0.10(192.168.0.10:3306) Tue Jan 14 09:55:55 2020 - [info] 192.168.0.20(192.168.0.20:3306) Tue Jan 14 09:55:55 2020 - [info] 192.168.0.30(192.168.0.30:3306) Tue Jan 14 09:55:55 2020 - [info] Alive Slaves: Tue Jan 14 09:55:55 2020 - [info] 192.168.0.20(192.168.0.20:3306) Version=5.5.56-MariaDB (oldest major version between slaves) log-bin:enabled Tue Jan 14 09:55:55 2020 - [info] Replicating from 192.168.0.10(192.168.0.10:3306) Tue Jan 14 09:55:55 2020 - [info] Primary candidate for the new Master (candidate_master is set) Tue Jan 14 09:55:55 2020 - [info] 192.168.0.30(192.168.0.30:3306) Version=5.5.56-MariaDB (oldest major version between slaves) log-bin:disabled Tue Jan 14 09:55:55 2020 - [info] Replicating from 192.168.0.10(192.168.0.10:3306) Tue Jan 14 09:55:55 2020 - [info] Current Alive Master: 192.168.0.10(192.168.0.10:3306) Tue Jan 14 09:55:55 2020 - [info] Checking slave configurations.. Tue Jan 14 09:55:55 2020 - [warning] log-bin is not set on slave 192.168.0.30(192.168.0.30:3306). This host cannot be a master. Tue Jan 14 09:55:55 2020 - [info] Checking replication filtering settings.. Tue Jan 14 09:55:55 2020 - [info] binlog_do_db= , binlog_ignore_db= Tue Jan 14 09:55:55 2020 - [info] Replication filtering check ok. Tue Jan 14 09:55:55 2020 - [info] GTID (with auto-pos) is not supported Tue Jan 14 09:55:55 2020 - [info] Starting SSH connection tests.. Tue Jan 14 09:55:58 2020 - [info] All SSH connection tests passed successfully. Tue Jan 14 09:55:58 2020 - [info] Checking MHA Node version.. Tue Jan 14 09:55:58 2020 - [info] Version check ok. Tue Jan 14 09:55:58 2020 - [info] Checking SSH publickey authentication settings on the current master.. Tue Jan 14 09:55:59 2020 - [info] HealthCheck: SSH to 192.168.0.10 is reachable. Tue Jan 14 09:55:59 2020 - [info] Master MHA Node version is 0.56. Tue Jan 14 09:55:59 2020 - [info] Checking recovery script configurations on 192.168.0.10(192.168.0.10:3306).. Tue Jan 14 09:55:59 2020 - [info] Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/lib/mysql,/var/log/mysql --output_file=/data/mastermha/mariadb//save_binary_logs_test --manager_version=0.56 --start_file=mariadb-bin.000002 Tue Jan 14 09:55:59 2020 - [info] Connecting to root@192.168.0.10(192.168.0.10:22).. Creating /data/mastermha/mariadb if not exists.. ok. Checking output directory is accessible or not.. ok. Binlog found at /var/lib/mysql, up to mariadb-bin.000002 Tue Jan 14 09:55:59 2020 - [info] Binlog setting check done. Tue Jan 14 09:55:59 2020 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers.. Tue Jan 14 09:55:59 2020 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='repmanage' --slave_host=192.168.0.20 --slave_ip=192.168.0.20 --slave_port=3306 --workdir=/data/mastermha/mariadb/ --target_version=5.5.56-MariaDB --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info --relay_dir=/var/lib/mysql/ --slave_pass=xxx Tue Jan 14 09:55:59 2020 - [info] Connecting to root@192.168.0.20(192.168.0.20:22).. Checking slave recovery environment settings.. Opening /var/lib/mysql/relay-log.info ... ok. Relay log found at /var/lib/mysql, up to mariadb-relay-bin.000007 Temporary relay log file is /var/lib/mysql/mariadb-relay-bin.000007 Testing mysql connection and privileges.. done. Testing mysqlbinlog output.. done. Cleaning up test file(s).. done. Tue Jan 14 09:55:59 2020 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user='repmanage' --slave_host=192.168.0.30 --slave_ip=192.168.0.30 --slave_port=3306 --workdir=/data/mastermha/mariadb/ --target_version=5.5.56-MariaDB --manager_version=0.56 --relay_log_info=/var/lib/mysql/relay-log.info --relay_dir=/var/lib/mysql/ --slave_pass=xxx Tue Jan 14 09:55:59 2020 - [info] Connecting to root@192.168.0.30(192.168.0.30:22).. Checking slave recovery environment settings.. Opening /var/lib/mysql/relay-log.info ... ok. Relay log found at /var/lib/mysql, up to mariadb-relay-bin.000009 Temporary relay log file is /var/lib/mysql/mariadb-relay-bin.000009 Testing mysql connection and privileges.. done. Testing mysqlbinlog output.. done. Cleaning up test file(s).. done. Tue Jan 14 09:56:00 2020 - [info] Slaves settings check done. Tue Jan 14 09:56:00 2020 - [info] 192.168.0.10(192.168.0.10:3306) (current master) +--192.168.0.20(192.168.0.20:3306) +--192.168.0.30(192.168.0.30:3306) Tue Jan 14 09:56:00 2020 - [info] Checking replication health on 192.168.0.20.. Tue Jan 14 09:56:00 2020 - [info] ok. Tue Jan 14 09:56:00 2020 - [info] Checking replication health on 192.168.0.30.. Tue Jan 14 09:56:00 2020 - [info] ok. Tue Jan 14 09:56:00 2020 - [warning] master_ip_failover_script is not defined. Tue Jan 14 09:56:00 2020 - [warning] shutdown_script is not defined. Tue Jan 14 09:56:00 2020 - [info] Got exit code 0 (Not master dead). MySQL Replication Health is OK. [root@test-centos6-node1 ~]#
說明:看到最後提示MySQL Replication Health is OK 表明咱們mariadb主從複製環境是健康的。接下來就能夠開啓監控
16)啓動mha
說明:mha開啓默認是前臺執行,因此咱們開啓後光標一直在屏幕上閃爍,看到這種狀況說明mha已經在監控咱們的主從複製環境了,生產環境中 通常建議後臺運行,前臺運行關閉了CRT,mha也跟隨着中止了工做,因此後臺方式運行最佳。還須要說明一點的是mha它只是一次使用,也就是說它不能重複使用,若是咱們主從環境中主節點宕機了,它的工做就是把主切換到咱們預先配置的從節點,使其變爲主節點,後續它就退出。接下來測試
測試:把主從環境中的主節點宕機,看看mha是否可以將咱們預先設置好的從切換成主
說明:咱們在主庫上中止了mariadb服務後,管理節點上的mha立馬就有反應了,事後就退出了,從上面的mha打印的信息看不出來具體哪一個服務器成爲主庫了。咱們能夠查看manager的日誌能夠看到
[root@test-centos6-node1 ~]# tail -20 /data/mastermha/mariadb/manager.log Tue Jan 14 10:07:03 2020 - [info] Master failover to 192.168.0.20(192.168.0.20:3306) completed successfully. Tue Jan 14 10:07:03 2020 - [info] ----- Failover Report ----- mariadb: MySQL Master failover 192.168.0.10(192.168.0.10:3306) to 192.168.0.20(192.168.0.20:3306) succeeded Master 192.168.0.10(192.168.0.10:3306) is down! Check MHA Manager logs at test-centos6-node1:/data/mastermha/mariadb/manager.log for details. Started automated(non-interactive) failover. The latest slave 192.168.0.20(192.168.0.20:3306) has all relay logs for recovery. Selected 192.168.0.20(192.168.0.20:3306) as a new master. 192.168.0.20(192.168.0.20:3306): OK: Applying all logs succeeded. 192.168.0.30(192.168.0.30:3306): This host has the latest relay log events. Generating relay diff files from the latest slave succeeded. 192.168.0.30(192.168.0.30:3306): OK: Applying all logs succeeded. Slave started, replicating from 192.168.0.20(192.168.0.20:3306) 192.168.0.20(192.168.0.20:3306): Resetting slave info succeeded. Master failover to 192.168.0.20(192.168.0.20:3306) completed successfully. [root@test-centos6-node1 ~]#
說明:從manage.log裏記錄的日誌能夠看到主庫從192.168.0.10成功轉移到192.168.0.20上,而且192.168.0.30已經從192.168.0.20哪裏把全部的日誌應用成功,並啓動了slave,也就是告訴咱們30已經切換新主去作主從同步數據了
[root@test-centos7-node3 ~]# mysql -e "show slave status\G" *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.20 Master_User: repuser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mariadb-bin.000003 Read_Master_Log_Pos: 245 Relay_Log_File: mariadb-relay-bin.000002 Relay_Log_Pos: 531 Relay_Master_Log_File: mariadb-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 245 Relay_Log_Space: 827 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 2 [root@test-centos7-node3 ~]#
說明:能夠看到原來的從庫已經擁護如今新的主庫了
[root@test-centos7-node2 ~]# mysql -e " show slave status" [root@test-centos7-node2 ~]#
說明:原來的從節點上已經沒有原主庫同步的信息了
[root@test-centos7-node2 ~]# mysql -e " show variables like 'read_only'" +---------------+-------+ | Variable_name | Value | +---------------+-------+ | read_only | OFF | +---------------+-------+ [root@test-centos7-node2 ~]#
說明:原來的從節點只讀屬性已經關閉了,這是由於原來的主節點宕機後,管理端把它提高爲主的同時,關閉了它的只讀屬性。這裏還須要說明一點,mha切換了主後,若是原來的主庫後續又恢復正常,此時它也不能頂替如今的主庫,至關於它和如今的集羣環境沒有關係了,只是一臺單獨的主機。
到此mha高可用實驗就作完了,以上就是mha高可用實驗的整個過程。