一鍵搭建mysql集群系列二

mysql 5.7  主從復制

本節是在上一節的基礎之上做的操作,上節我們通過腳本實現了一鍵自動化安裝mysql5.7,這次我們要在三臺機器上部署msyql5.7,并實現主從復制,實現的宗旨是,能自動化,就盡量自動化,實在不能自動化,那就手工化

用的腳本和文件說明
1.install_mysql.sh 自動化安裝mysql腳本
2.ntpdate.sh 時間同步腳本
3.system_init.sh 系統初始化腳本
4.yum.sh yum源配置腳本
5.exec_commond.sh 批量執行命令腳本,附帶分發公鑰功能 6.fenfa.sh 分發文件
7.secret_key.sh 創建秘鑰腳本

執行步驟:

1.創建目錄和下載腳本

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地址(三臺),實現方法,依次在三臺機器下,下載腳本,執行腳本

更改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機器來分發文件,所有需要將被管理的ip集合起來送給ip_hosts.sh.格式如下

#mysql從服務器2臺
192.168.42.81
192.168.42.82
  1. 依次初始化系統和安裝mysql(參照一階段)
  2. 在mysql-master主機上編輯hosts文件,等下統一分發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賬號,沒有就創建(這里默認是用magedu賬號來統一分發文件) 需要手工創建

id magedu
useradd magedu
echo 123456 | passwd --stdin magedu
yum install rsync -y

6.其他兩臺從機器也是同樣需要創建magedu賬號,我們這里使用腳本創建(注意是master機器上執行)

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.創建秘鑰分發秘鑰

bash secret_key.sh 1 
bash exec_commond.sh "fenfa"

8.利用分發腳本分發hosts文件 注意切換用戶magedu,在magedu家目錄下進行分發
在master機器上將hosts文件復制到家目錄下,執行分發腳本

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 #設置成只讀

*****************************以上是配置文件中  *********************************************

#查看當前狀態
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:

看到以上就是正常的

自此一主多從的配置已經搭建完成了,在主服務器上創建數據庫,試試,看看是否同步到兩臺從服務器上

 

原創文章,作者:srayban,如若轉載,請注明出處:http://www.www58058.com/76528

(1)
sraybansrayban
上一篇 2017-05-21 12:29
下一篇 2017-05-21 12:34

相關推薦

  • 馬哥教育網絡班21第五周練習作業

    1、顯示/boot/grub/grub.conf中以至少一個空白字符開頭的行; [root@localhost ~]# grep "^[[:space:]]\+" /boot/grub2/grub.cfg   2、顯示/etc/rc.d/rc.sysinit文件中以#開頭,后面跟至少一個空…

    Linux干貨 2016-08-08
  • 防火墻

    iptables筆記整理

    Linux干貨 2018-03-05
  • 網絡21期第十周博客作業

    1、請詳細描述CentOS系統的啟動流程(詳細到每個過程系統做了哪些事情) Centos6啟動流程:       POST加電自檢 —> BOOT Sequence —> 加載內核啟動系統    &n…

    Linux干貨 2016-09-15
  • 馬哥教育網絡班N22期+第五周課程練習

    1、顯示當前系統上root、fedora或user1用戶的默認shell; ~]# grep -E "^(root|user1|fedora)" /etc/passwd | awk -F: '{ print $1 $7 }' root/bin/bash user1/bin/bash fedora/bin/bash 2、…

    Linux干貨 2016-10-17
  • tree命令 -P選項的用法探討

         tree命令是以樹狀圖的形式來列出目標文件夾的目錄結構,包括所有路徑和所有文件名,如圖1所示。                               &n…

    2017-05-29
  • 分布式文件系統之MogileFS

    MogileFS是一個開源的分布式文件存儲系統,由LiveJournal旗下的Danga Interactive公司開發。Danga團隊開發了包括 Memcached、MogileFS、Perlbal 等多個知名的開源項目。目前使用MogileFS 的公司非常多,如日本排名先前的幾個互聯公司及國內的yupoo(又拍)、digg、豆瓣、1號店、大眾點評、搜狗和…

    Linux干貨 2017-06-16

評論列表(1條)

  • 婭娃愛
    婭娃愛 2017-11-27 11:00

    沒有腳本源碼呀,還想看一下呢

欧美性久久久久