Linux基礎(chǔ)入門之mysql集群搭建實(shí)戰(zhàn)(二)
mysql 5.7 主從復(fù)制
本節(jié)是在上一節(jié)的基礎(chǔ)之上做的操作,上節(jié)我們通過腳本實(shí)現(xiàn)了一鍵自動(dòng)化安裝mysql5.7,這次我們要在三臺(tái)機(jī)器上部署msyql5.7,并實(shí)現(xiàn)主從復(fù)制,實(shí)現(xiàn)的宗旨是,能自動(dòng)化,就盡量自動(dòng)化,實(shí)在不能自動(dòng)化,那就手工化
用的腳本和文件說明
1.install_mysql.sh 自動(dòng)化安裝mysql腳本
2.ntpdate.sh 時(shí)間同步腳本
3.system_init.sh 系統(tǒng)初始化腳本
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
我們?cè)谝浑A段已經(jīng)闡述,一鍵安裝mysql了,現(xiàn)在依照上次的方法,依次在三臺(tái)主機(jī)上安裝mysql(詳情請(qǐng)看一階段)修改主機(jī)名和ip地址(三臺(tái)),實(shí)現(xiàn)方法,依次在三臺(tái)機(jī)器下,下載腳本,執(zhí)行腳本
更改ip和主機(jī)名的示例:
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機(jī)器來分發(fā)文件,所有需要將被管理的ip集合起來送給ip_hosts.sh.格式如下
#mysql從服務(wù)器2臺(tái)
192.168.42.81
192.168.42.82
- 依次初始化系統(tǒng)和安裝mysql(參照一階段)
- 在mysql-master主機(jī)上編輯hosts文件,等下統(tǒng)一分發(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主機(jī)有沒有magedu賬號(hào),沒有就創(chuàng)建(這里默認(rèn)是用magedu賬號(hào)來統(tǒng)一分發(fā)文件) 需要手工創(chuàng)建
id magedu
useradd magedu
echo 123456 | passwd --stdin magedu
yum install rsync -y
6.其他兩臺(tái)從機(jī)器也是同樣需要?jiǎng)?chuàng)建magedu賬號(hào),我們這里使用腳本創(chuàng)建(注意是master機(jī)器上執(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家目錄下進(jìn)行分發(fā)
在master機(jī)器上將hosts文件復(fù)制到家目錄下,執(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機(jī)器配置,因?yàn)樵诎惭b的時(shí)候默認(rèn)已經(jīng)開啟了
#開啟二進(jìn)制日志
bin-log
#指定server-id
server-id=1
#主服務(wù)器添加復(fù)制賬號(hào)
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)
得到的結(jié)果是給下面的連接主服務(wù)器使用的(需要注意)
10.從服務(wù)器配置 (兩臺(tái)都要配置哦)
#關(guān)閉二進(jìn)制日志
#bin-log
#開啟中繼日志
relay-log = relay-log
relay-log-index = relay-log.index
sever-id = 11 #類推
read_only = on #設(shè)置成只讀
*****************************以上是配置文件中 *********************************************
#查看當(dāng)前狀態(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:
看到以上就是正常的
自此一主多從的配置已經(jīng)搭建完成了,在主服務(wù)器上創(chuàng)建數(shù)據(jù)庫(kù),試試,看看是否同步到兩臺(tái)從服務(wù)器上