一键搭建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.178linux.com/76528

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

相关推荐

  • shell脚本编程3

    补充,shift使用方法 root@localhost wang]# cat jiaoben1.sh  #!/bin/bash echo "$1" echo "$*" shift echo "$1" echo &quot…

    Linux干货 2016-08-24
  • 权限(用户、特殊、ACL)

    root权限(id=0) root无论在什么情况下都有rw权限,但是是否拥有x权限,要分情况: 第一,文件所有者和所属组都无x权限,root也无x权限; 第二,文件所有者和所属组二者任何一个有x权限,root就有x权限。 用户得到的权限 匹配顺序:文件所有者——文件所属组——其他人(从左到右) 用户访问文件,一旦按次序匹配成功,其获得的权限就是匹配选项所对应…

    Linux干货 2017-05-30
  • Linux运维实战之2-2:bash的工作特性

    这次博文我们主要来谈谈bash的工作特性哈。 主要内容: bash是一种shell解释程序     bash工作特性之命令状态返回值 bash工作特性之命令行展开 bash工作特性之命令补全 bash工作特性之路径补齐 bash工作特性之命令引用 bash工作特性之命令别名 bash工作特性之文件名通配 bash工作特性之命令hash 问…

    Linux干货 2016-11-06
  • grep和正则表达式

    grep       作用:文本搜索工具,根据用户指定的“模式(过滤条件)”对目标文件逐行进行匹配检查;打印匹配到的行;       模式:由正则表达式的元字符及文本所编写出的过滤条件    语法:【选项】【文件】  &…

    2017-08-04
  • ​linux文件类型及颜色标识整理

          linux文件类型及颜色标识整理                      &…

    Linux干货 2016-10-17
  • VMware虚拟机三种连网原理简介

    一、前言         虚拟机(Virtual Machine),在计算机科学中的体系结构里,是指一种特殊的软件,可以在计算机平台和终端用户之间创建一种环境,而终端用户则是基于这个软件所创建的环境来操作系统软件。虚拟机根据它们运用和与直接机器的相关性分为两大类。系统虚拟机和程序虚拟机,系…

    Linux干货 2015-08-03

评论列表(1条)

  • 娅娃爱
    娅娃爱 2017-11-27 11:00

    没有脚本源码呀,还想看一下呢