zoukankan      html  css  js  c++  java
  • Mysql主从

    安装Mysql

    环境

    [root@node1 ~]#  cat /etc/redhat-release
    CentOS Linux release 7.1.1503 (Core)
    [root@node1 ~]# uname -a
    Linux node4 3.10.0-229.el7.x86_64 #1 SMP Fri Mar 6 11:36:42 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

    主机

    Master:10.0.0.10 node1
    Slave:10.0.0.11 node2

    检查系统是否自带安装mysql

    [root@node1 ~]# yum list installed | grep mysql 
    [root@node1 ~]# 

    如果有将其删除

    [root@node1 ~]# yum -y remove mysql-libs.x86_64
    Loaded plugins: fastestmirror
    No Match for argument: mysql-libs.x86_64
    No Packages marked for removal

    由于mysql的yum源服务器在国外,所以下载速度会比较慢,还好mysql5.6只有79M大,而mysql5.7就有182M了,所以这里想安装mysql5.6

    下载rpm包

    [root@node1 ~]# wget http://repo.mysql.com/mysql-community-release-el7-7.noarch.rpm

    解压

    [root@node1 ~]# rpm -ivh mysql-community-release-el7-7.noarch.rpm 

    检查mysql源是否安装成功

    [root@node2 ~]# yum repolist enabled | grep "mysql.*-community.*"
    mysql-connectors-community/x86_64 MySQL Connectors Community                  42
    mysql-tools-community/x86_64      MySQL Tools Community                       51
    mysql56-community/x86_64          MySQL 5.6 Community Server              

    查看是否已经有mysql可安装文件

    复制代码
    root@node1 ~]#  yum repolist all | grep mysql
    mysql-connectors-community/x86_64 MySQL Connectors Community     enabled:     42
    mysql-connectors-community-source MySQL Connectors Community - S disabled
    mysql-tools-community/x86_64      MySQL Tools Community          enabled:     51
    mysql-tools-community-source      MySQL Tools Community - Source disabled
    mysql55-community/x86_64          MySQL 5.5 Community Server     disabled
    mysql55-community-source          MySQL 5.5 Community Server - S disabled
    mysql56-community/x86_64          MySQL 5.6 Community Server     enabled:    344
    mysql56-community-source          MySQL 5.6 Community Server - S disabled
    mysql57-community/x86_64          MySQL 5.7 Community Server     disabled
    mysql57-community-source          MySQL 5.7 Community Server - S disabled
    复制代码

    可以修改 mysql-community.repo 源,更改默认安装的mysql版本比如要安装5.6版本,将5.7源的enabled=1改成enabled=0。然后再将5.6源的enabled=0改成enabled=1即可。改完之后的效果如下所示: 

    复制代码
    [root@node2 ~]# vim /etc/yum.repos.d/mysql-community.repo
    
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    [mysql-tools-community]
    name=MySQL Tools Community
    baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    # Enable to use MySQL 5.5
    [mysql55-community]
    name=MySQL 5.5 Community Server
    baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/
    enabled=0
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    # Enable to use MySQL 5.6
    [mysql56-community]
    name=MySQL 5.6 Community Server
    baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
    enabled=1
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    
    [mysql57-community]
    name=MySQL 5.7 Community Server
    baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
    enabled=0
    gpgcheck=1
    gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
    复制代码

    安装mysql 服务器(网速决定安装速度)

    [root@node1 ~]# yum -y install mysql-community-server

    启动 mysql 服务

    [root@node1 ~]# systemctl start mysql

    查看mysql 的启动状态

    复制代码
    [root@node1 ~]# systemctl status mysqld
    mysqld.service - MySQL Community Server
       Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled)
       Active: active (running) since Wed 2017-08-23 11:28:23 EDT; 38min ago
      Process: 2904 ExecStartPost=/usr/bin/mysql-systemd-start post (code=exited, status=0/SUCCESS)
      Process: 2843 ExecStartPre=/usr/bin/mysql-systemd-start pre (code=exited, status=0/SUCCESS)
     Main PID: 2903 (mysqld_safe)
    复制代码

    开机启动

    [root@node1 ~]# systemctl enable mysqld
    [root@node1 ~]# systemctl daemon-reload

    以上操作在10.0.0.10和10.0.0.11 上做相同操作

    配置主从

    修改主服务器master

    [root@node1 ~]# vim /etc/my.cnf
    [mysqld]
    log-bin=mysql-bin   #[不是必须]启用二进制日志
    server-id=10     #[必须]服务器唯一ID,默认是1,一般取IP最后一段

    修改从服务器Slave

    [root@node2 ~]# vim /etc/my.cnf
    [mysqld]
    log-bin=mysql-bin   #[不是必须]启用二进制日志
    server-id=11     #[必须]服务器唯一ID,默认是1,一般取IP最后一段

    重启两台服务器的Mysql

    [root@node1 ~]# systemctl restart mysql
    [root@node2 ~]# systemctl restart mysql

    在主服务器上建立账号并授权从服务器(由于Mysql没有设置密码,只需输入mysql登录即可)

    [root@node1 ~]#mysql
    mysql> GRANT REPLICATION SLAVE ON *.* to 'mysync'@'%' identified by '123456';
    一般不用root帐号,“%”表示所有客户端都可能连,只要帐号,密码正确,此处可用具体
    客户端IP代替,如192.168.145.226,加强安全。

    登录主服务器的mysql,查询master的状态

    mysql> show master status;
    +------------------+----------+--------------+------------------+-------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +------------------+----------+--------------+------------------+-------------------+
    | mysql-bin.000002 |      760 |              |                  |                   |
    +------------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)

    注意 File 和 Position 的值;执行完此步骤后不要再操作主服务器MYSQL,防止主服务器状态值变化

    配置从服务器Slave

    mysql>change master to master_host='10.0.0.10',master_user='mysync',master_password='123456',
           master_log_file='mysql-bin.000002',master_log_pos=760;   # 注意不要断开,master_log_file 和 master_log_pos 的值在主服务器上面

    启动从服务器复制功能

     Mysql>start slave;

    检查从服务器复制功能状态

    mysql> show slave statusG
    
       *************************** 1. row ***************************
    
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.0.0.10  #主服务器地址
                  Master_User: mysync   #授权帐户名,尽量避免使用root
                  Master_Port: 3306    #数据库端口,部分版本没有此行
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 600    #同步读取二进制日志的位置,大于等于Exec_Master_Log_Pos
               Relay_Log_File: ddte-relay-bin.000002
                Relay_Log_Pos: 760
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes    #此状态必须YES
            Slave_SQL_Running: Yes    #此状态必须YES

    以上操作过程,主从服务器配置完成

    主从服务器测试

    主服务器Mysql,建立数据库,并在这个库中建表插入一条数据

     mysql> create database hi_db;
      Query OK, 1 row affected (0.00 sec)
    
      mysql> use hi_db;
      Database changed
    
      mysql>  create table hi_tb(id int(3),name char(10));
      Query OK, 0 rows affected (0.00 sec)
     
      mysql> insert into hi_tb values(001,'bobu');
      Query OK, 1 row affected (0.00 sec)
    
      mysql> show databases;
       +--------------------+
       | Database           |
       +--------------------+
       | information_schema |
       | hi_db                |
       | mysql                |
       | test                 |
       +--------------------+
       4 rows in set (0.00 sec)

    从服务器Mysql查询

     mysql> show databases;
    
       +--------------------+
       | Database               |
       +--------------------+
       | information_schema |
       | hi_db                 |       #看到了吧
       | mysql                 |
       | test          |
       +--------------------+
       4 rows in set (0.00 sec)
    
       mysql> use hi_db
       Database changed
       mysql> select * from hi_tb;      #查看主服务器上新增的具体数据
       +------+------+
       | id   | name |
       +------+------+
       |    1 | bobu |
       +------+------+
       1 row in set (0.00 sec)
     

    完成

     编写一shell脚本,用nagios监控slave的两个yes(Slave_IO及Slave_SQL进程),如发现只有一个或零个yes,就表明主从有问题了,发短信警报

    参考文档:

    http://blog.51cto.com/369369/790921
  • 相关阅读:
    Python学习 :面向对象 -- 三大特性
    Python学习 :面向对象(一)
    小米oj #40 找小“3”
    第三次作业
    排序算法模板
    树状数组求逆序数
    最短路模板
    字典树模板
    LCS
    多项式朴素乘法
  • 原文地址:https://www.cnblogs.com/wanglan/p/7930090.html
Copyright © 2011-2022 走看看