Linux基礎入門之mysql集群搭建實戰(zhàn)(二)
mysql 5.7 主從復制
本節(jié)是在上一節(jié)的基礎之上做的操作,上節(jié)我們通過腳本實現了一鍵自動化安裝mysql5.7,這次我們要在三臺機器上部署msyql5.7,并實現主從復制,實現的宗旨是,能自動化,就盡量自動化,實在不能自動化,那就手工化
用的腳本和文件說明
1.install_mysql.sh 自動化安裝mysql腳本
2.ntpdate.sh 時間同步腳本
3.system_init.sh 系統初始化腳本
4.yum.sh yum源配置腳本
5.exec_commond.sh 批量執(zhí)行命令腳本,附帶分發(fā)公鑰功能 6.fenfa.sh 分發(fā)文件
7.secret_key.sh 創(chuàng)建秘鑰腳本
執(zhí)行步驟:
1.創(chuàng)建目錄和下載腳本
mkdir -p /server/script
cd /server/script
wget -c http://192.168.42.26/script/yum.sh
wget -c http://192.168.42.26/script/install_mysql.sh
wget -c http://192.168.42.26/script/ntpdate.sh
wget -c http://192.168.42.26/script/create_bash.sh
wget -c http://192.168.42.26/script/system_init.sh
wget -c http://192.168.42.26/script/change_ip.sh
wget -c http://192.168.42.26/script/change_hostname.sh
wget -c http://192.168.42.26/script/exec_commond.sh
wget -c http://192.168.42.26/script/fenfa.sh
wget -c http://192.168.42.26/script/secret_key.sh
我們在一階段已經闡述,一鍵安裝mysql了,現在依照上次的方法,依次在三臺主機上安裝mysql(詳情請看一階段)修改主機名和ip地址(三臺),實現方法,依次在三臺機器下,下載腳本,執(zhí)行腳本
更改ip和主機名的示例:
bash change_ip.sh 192.168.42.80 mysql-master
bash change_ip.sh 192.168.42.81 mysql-slave1
bash change_ip.sh 192.168.42.82 mysql-slave2
我們這里是用master機器來分發(fā)文件,所有需要將被管理的ip集合起來送給ip_hosts.sh.格式如下
#mysql從服務器2臺
192.168.42.81
192.168.42.82
- 依次初始化系統和安裝mysql(參照一階段)
- 在mysql-master主機上編輯hosts文件,等下統一分發(fā)hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.42.80 mysql-master
192.168.42.81 mysql-slave1
192.168.42.82 mysql-slave2
5.首先查看master主機有沒有magedu賬號,沒有就創(chuàng)建(這里默認是用magedu賬號來統一分發(fā)文件) 需要手工創(chuàng)建
id magedu
useradd magedu
echo 123456 | passwd --stdin magedu
yum install rsync -y
6.其他兩臺從機器也是同樣需要創(chuàng)建magedu賬號,我們這里使用腳本創(chuàng)建(注意是master機器上執(zhí)行)
bash exec_commond.sh "useradd magedu"
bash exec_commond.sh "echo 123456 | passwd --stdin magedu"
bash exec_commond.sh "yum install rsync -y"
bash exec_commond.sh "echo 'magedu ALL=(ALL) NOPASSWD: /bin/rsync'>>/etc/sudoers"
7.創(chuàng)建秘鑰分發(fā)秘鑰
bash secret_key.sh 1
bash exec_commond.sh "fenfa"
8.利用分發(fā)腳本分發(fā)hosts文件 注意切換用戶magedu,在magedu家目錄下進行分發(fā)
在master機器上將hosts文件復制到家目錄下,執(zhí)行分發(fā)腳本
cp /etc/hosts /home/magedu/
bash /server/script/master/fenfa.sh hosts
bash /server/script/master/fenfa.sh --commond "sudo rsync /home/magedu/hosts /etc/"
9.master機器配置,因為在安裝的時候默認已經開啟了
#開啟二進制日志
bin-log
#指定server-id
server-id=1
#主服務器添加復制賬號
mysql -e "grant replication slave on *.* to 'repluser'@'192.168.%.%' identified by 'replpass';"
mysql -e "flush privileges;"
mysql -e "show master status;"
腳本獲取(mysql -e "show master status;" | grep "master"| tr "[:space:]" " "| cut -d " " -f 1)
得到的結果是給下面的連接主服務器使用的(需要注意)
10.從服務器配置 (兩臺都要配置哦)
#關閉二進制日志
#bin-log
#開啟中繼日志
relay-log = relay-log
relay-log-index = relay-log.index
sever-id = 11 #類推
read_only = on #設置成只讀
*****************************以上是配置文件中 *********************************************
#查看當前狀態(tài)
mysql -e "show slave status\G;"
mysql -e "change master to master_host ='192.168.42.80',master_user='repluser',master_password='replpass',master_log_file='mysql-bin.000001',master_log_pos=863;"
mysql -e "show slave status \G;"
mysql -e "start slave;"
[root@mysql-slave2 script]# mysql -e "show slave status \G;"
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.42.80
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 863
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 320
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: 863
Relay_Log_Space: 521
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_UUID: 8250def6-3aa6-11e7-a782-000c29c8721f
Master_Info_File: /application/mysql/data/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
看到以上就是正常的
自此一主多從的配置已經搭建完成了,在主服務器上創(chuàng)建數據庫,試試,看看是否同步到兩臺從服務器上