您的位置:68399皇家赌场 > 虚拟主机 > mysql五.6搭建主从复制

mysql五.6搭建主从复制

发布时间:2019-05-07 09:29编辑:虚拟主机浏览(88)

    应用MySQL 伍.陆,搭建主从复制。关于伍.陆的安装,能够参照《MySQL 伍.陆rpm安装方式和碰见的标题》。

    1、查看从库数据库状态:
    mysql> show processlist;
    ---- ------------- ----------------- -------- --------- ------- ----------------------------------------------------------------------------- ------------------
    | Id | User        | Host            | db     | Command | Time  | State                                                                       | Info             |
    ---- ------------- ----------------- -------- --------- ------- ----------------------------------------------------------------------------- ------------------
    |  2 | root        | 127.0.0.1:59012 | sakila | Query   |     0 | NULL                                                                        | show processlist |
    |  3 | system user |                 | NULL   | Connect | 17618 | Waiting for master to send event                                            | NULL             |
    |  4 | system user |                 | NULL   | Connect | 17305 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL             |
    ---- ------------- ----------------- -------- --------- ------- ----------------------------------------------------------------------------- ------------------
    3 rows in set (0.00 sec)

    安装情状

    MySQL主从故障修复

     

    mysql> show slave statusG
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 172.16.33.143
                      Master_User: master
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql.000003
              Read_Master_Log_Pos: 3296928
                   Relay_Log_File: slave-relay-bin.000002
                    Relay_Log_Pos: 3296845
            Relay_Master_Log_File: mysql.000003
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: 
              Replicate_Ignore_DB: test
               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: 3296928
                  Relay_Log_Space: 3297001
                  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
    1 row in set (0.00 sec)

    操作系统 :CentOS 6.5

    测试库:
    192.168.1.2 主
    192.168.1.3 从

    主库创造slave用户,设置复制权限,

    mysql> show processlist;
    ---- -------- --------------------- -------- ------------- ------- ----------------------------------------------------------------------- ------------------
    | Id | User   | Host                | db     | Command     | Time  | State                                                                 | Info             |
    ---- -------- --------------------- -------- ------------- ------- ----------------------------------------------------------------------- ------------------
    |  1 | root   | 127.0.0.1:33623     | sakila | Query       |     0 | NULL                                                                  | show processlist |
    |  2 | master | 172.16.33.144:48988 | NULL   | Binlog Dump | 17455 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL             |
    ---- -------- --------------------- -------- ------------- ------- ----------------------------------------------------------------------- ------------------
    2 rows in set (0.00 sec)

    数据库版本:MySQL 5.陆.2七

    192.168.1.4 主 4又是2的从库
    192.168.1.5 从

     

    mysql> show master status;
    -------------- ---------- -------------- ------------------
    | File         | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    -------------- ---------- -------------- ------------------
    | mysql.000003 |  3296928 |              |                  |
    -------------- ---------- -------------- ------------------
    1 row in set (0.00 sec)

    主机A:192.168.1.1 (Master)

    有人修改了1九二.16八.1.二和1玖2.16八.1.3的数据库参数后,重启数据库。
    忘却了1玖二.168.1.肆又是192.16八.一.2的从库,导致1九2.16八.一.二和1九2.16八.一.4的主干断掉。
    与此同时在1九二.168.壹.二上创办了新库还原数据删除等操作,导致1九二.16八.1.4提示错误。

    mysql> create user 'slave'@'1.1.1.2' identified by 'root';

    2、修改配置文件:
    主库:read_only = 1
    从库:#read_only = 1

    主机B:192.168.1.2 (Slave)

    效仿如下:
    由此从库查看主从状态:
    mysql> show slave statusG  
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 172.16.33.243
                      Master_User: master
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000006
              Read_Master_Log_Pos: 303
                   Relay_Log_File: relay-log.000005
                    Relay_Log_Pos: 340
            Relay_Master_Log_File: mysql-bin.000006
                 Slave_IO_Running: Yes
                Slave_SQL_Running: No
                  Replicate_Do_DB: 
              Replicate_Ignore_DB: test
               Replicate_Do_Table: 
           Replicate_Ignore_Table: 
          Replicate_Wild_Do_Table: 
      Replicate_Wild_Ignore_Table: 
                       Last_Errno: 1049
                       Last_Error: Error 'Unknown database 'sakila'' on query. Default database: 'sakila'. Query: 'create table sakila (id int,name varchar(2))'
                     Skip_Counter: 0
              Exec_Master_Log_Pos: 194
                  Relay_Log_Space: 745
                  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: NULL
    Master_SSL_Verify_Server_Cert: No
                    Last_IO_Errno: 0
                    Last_IO_Error: 
                   Last_SQL_Errno: 1049
                   Last_SQL_Error: Error 'Unknown database 'sakila'' on query. Default database: 'sakila'. Query: 'create table sakila (id int,name varchar(2))'
      Replicate_Ignore_Server_Ids: 
                 Master_Server_Id: 360360
    1 row in set (0.00 sec)

     

    三、操作从库:
    mysql> show processlist;
    ---- ------------- ----------------- -------- --------- ------- ----------------------------------------------------------------------------- ------------------
    | Id | User        | Host            | db     | Command | Time  | State                                                                       | Info             |
    ---- ------------- ----------------- -------- --------- ------- ----------------------------------------------------------------------------- ------------------
    |  2 | root        | 127.0.0.1:59012 | sakila | Query   |     0 | NULL                                                                        | show processlist |
    |  3 | system user |                 | NULL   | Connect | 17770 | Waiting for master to send event                                            | NULL             |
    |  4 | system user |                 | NULL   | Connect | 17457 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL             |
    ---- ------------- ----------------- -------- --------- ------- ----------------------------------------------------------------------------- ------------------
    3 rows in set (0.00 sec)

    Master的配置

    笔录下二进制日志的和点的职位:
    Relay_Master_Log_File: mysql-bin.000006
    Exec_Master_Log_Pos: 194

    Query OK, 0 rows affected (0.00 sec)

    mysql> stop slave io_thread;
    Query OK, 0 rows affected (0.00 sec)

    在Linux意况下MySQL的配备文件的职责是在 /etc/my.cnf ,在该公文下钦命Master的计划如下:

    停掉从库的slave
    mysql> stop slave;
    Query OK, 0 rows affected (0.01 sec)

     

    mysql> show processlist;    
    ---- ------------- ----------------- -------- --------- ------- ----------------------------------------------------------------------------- ------------------
    | Id | User        | Host            | db     | Command | Time  | State                                                                       | Info             |
    ---- ------------- ----------------- -------- --------- ------- ----------------------------------------------------------------------------- ------------------
    |  2 | root        | 127.0.0.1:59012 | sakila | Query   |     0 | NULL                                                                        | show processlist |
    |  4 | system user |                 | NULL   | Connect | 17526 | Slave has read all relay log; waiting for the slave I/O thread to update it | NULL             |
    ---- ------------- ----------------- -------- --------- ------- ----------------------------------------------------------------------------- ------------------
    2 rows in set (0.00 sec)

    log-bin=mysql-bin

    并在主库删除掉新建的库
    mysql> drop database sakila;
    Query OK, 1 row affected (0.04 sec)

        1. mysql> grant replication slave on *.* to 'slave'@'1.1.1.2' identified by 'root';
        1. Query OK, 0 rows affected (0.00 sec)

    承认从库的情景为:Slave has read all relay log

    server-id=2

    重新举办主从复制
    mysql> show master status;
    ------------------ ---------- -------------- ------------------
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    ------------------ ---------- -------------- ------------------
    | mysql-bin.000006 |      388 |              |                  |
    ------------------ ---------- -------------- ------------------
    1 row in set (0.00 sec)

     

    mysql> show slave statusG
    *************************** 1. row ***************************
                   Slave_IO_State: 
                      Master_Host: 172.16.33.143
                      Master_User: master
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql.000003
              Read_Master_Log_Pos: 3296928
                   Relay_Log_File: slave-relay-bin.000002
                    Relay_Log_Pos: 3296845
            Relay_Master_Log_File: mysql.000003
                 Slave_IO_Running: No
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: 
              Replicate_Ignore_DB: test
               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: 3296928
                  Relay_Log_Space: 3297001
                  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: NULL
    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
    1 row in set (0.00 sec)

    binlog-ignore-db=information_schema

    mysql> change master to master_host='192.168.1.2',master_port=3306,master_user='master',master_password='123456',master_log_file='mysql-bin.000006',master_log_pos=388;
    Query OK, 0 rows affected (0.01 sec)

    1. 编写my.cnf配置文件,设置主库server-id=1,定义须要复制的库为test,忽略mysql数据库

    四、查看主库状态:
    mysql> show master status;
    -------------- ---------- -------------- ------------------
    | File         | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    -------------- ---------- -------------- ------------------
    | mysql.000003 |  3296928 |              |                  |
    -------------- ---------- -------------- ------------------
    1 row in set (0.00 sec)

    binlog-ignore-db=cluster

    mysql> start slave ;
    Query OK, 0 rows affected (0.00 sec)

      1. [root@vm-kvm10000-app mysql]# vi /etc/my.cnf
      1. [mysqld]
      1. server-id=1
      1. log-bin=mysql-bin
      1. binlog_do_db=test
      1. style="font-size: 16px">binlog_ignore_db=mysql

    5、主从切换,从库变主库:
    mysql> slave stop;
    Query OK, 0 rows affected, 1 warning (0.00 sec)

    binlog-ignore-db=mysql

    翻开主从状态还提示原来的一无所能,使用sql_slave_skip_counter参数
    mysql> show variables like  '%skip%';
    ------------------------ -------
    | Variable_name          | Value |
    ------------------------ -------
    | skip_external_locking  | ON    |
    | skip_name_resolve      | ON    |
    | skip_networking        | OFF   |
    | skip_show_database     | OFF   |
    | slave_skip_errors      | OFF   |
    | sql_slave_skip_counter | 0     |
    ------------------------ -------
    6 rows in set (0.00 sec)

     

    mysql> reset master;
    Query OK, 0 rows affected (0.02 sec)

    binlog-do-db=ufind_db

    默认为0

    1. 重启主库MySQL服务,

    mysql> reset slave;
    Query OK, 0 rows affected (0.00 sec)

    这里的server-id用于标记唯1的数据库,这里安装为二,在安装从库的时候就需求设置为其余值。

    mysql> set global sql_slave_skip_counter = 1;

      1. [root@vm-kvm10000-app mysql]# service mysql restart
      1. Shutting down MySQL.. SUCCESS!
      1. Starting MySQL. SUCCESS!

    mysql> show master status;
    -------------- ---------- -------------- ------------------
    | File         | Position | Binlog_Do_DB | Binlog_Ignore_DB |
    -------------- ---------- -------------- ------------------
    | mysql.000001 |      107 |              |                  |
    -------------- ---------- -------------- ------------------
    1 row in set (0.00 sec)

    binlog-ignore-db:表示同步的时候ignore的数据库

    mysql> start slave;

     

    6、主从切换,主库变从库:
    mysql> reset master;
    Query OK, 0 rows affected (0.02 sec)

    binlog-do-db:钦点须求共同的数据库

    还提示错误,继续

    1. 看一下主库状态,

    mysql> reset slave;
    Query OK, 0 rows affected (0.00 sec)

    壹、然后重启mysql:service mysqld restart

    mysql> set global sql_slave_skip_counter = 1;

      1. mysql> show master status;
      1. style="font-size: 16px"> ------------------ ---------- -------------- ------------------ -------------------
      1. | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
      1. style="font-size: 16px"> ------------------ ---------- -------------- ------------------ -------------------
      1. | mysql-bin.000002 |      120 | test         | mysql            |                   |
      1. style="font-size: 16px"> ------------------ ---------- -------------- ------------------ -------------------
      1. 1 row in set (0.00 sec)

     

      1. 或者
      1. mysql> show master status G
      1. style="font-size: 16px">*************************** 1. row ***************************
      1. File: mysql-bin.000002
      1. Position: 120
      1. Binlog_Do_DB: test
      1. Binlog_Ignore_DB: mysql
      1. Executed_Gtid_Set:
      1. 1 row in set (0.00 sec)

    mysql> change master to master_host='172.16.33.144',master_port=3306,master_user='master',master_password='123456',master_log_file=' mysql-bin.000001',master_log_pos=107;
    Query OK, 0 rows affected (0.04 sec)

    2、进入mysql:[root@VM_221_4_centos ~]# mysql -u root -p 回车,输入mysql密码进入。

    mysql> start slave;

     

    七、重启主从:
    [root@yoon ~]# service mysqld restart
    Shutting down MySQL. SUCCESS! 
    Starting MySQL.. SUCCESS! 

    三、 赋予从库权限帐号,允许用户在主库上读取日志,赋予19二.16八.一.2也等于Slave机器有File权限,只授予Slave机器有File权限还特别,还要给它REPLICATION SLAVE的权力才具够。

    mysql> show slave statusG
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.1.2
                      Master_User: master
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000006
              Read_Master_Log_Pos: 388
                   Relay_Log_File: relay-log.000002
                    Relay_Log_Pos: 253
            Relay_Master_Log_File: mysql-bin.000006
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: 
              Replicate_Ignore_DB: test
               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: 388
                  Relay_Log_Space: 403
                  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: 360360
                 
                 
    终极在1玖二.168.一.2上海重机厂复建库并苏醒数据

    1. 从库,编辑my.cnf配置,设置server-id=2,区别于主库,

    八、查看主从是还是不是符合规律:
    主库:
    show processlist;
    show master status;

    在Master数据库命令行中输入:

      1. [root@vm-kvm10001-app mysql]# vi /etc/my.cnf
      1. [mysqld]
      1. server-id=2

    从库:
    show processlist;
    start slave;
    show slave statusG

    >GRANT FILE ON *.* TO 'root'@'192.168.1.2' IDENTIFIED BY 'mysql password';

     

     

    >GRANT REPLICATION SLAVE ON *.* TO 'root'@'192.168.1.2' IDENTIFIED BY 'mysql password';

    1. 重启MySQL服务,

    >FLUSH PRIVILEGES

      1. [root@vm-kvm10001-app mysql]# service mysql restart
      1. Shutting down MySQL.. SUCCESS!
      1. Starting MySQL. SUCCESS!

    四、重启mysql,登6mysql,展现主库消息

     

    mysql> show master status;

    1. 设置主库音信,

    1

      1. mysql> change master to master_host='1.1.1.1',master_user='slave',master_password='root',master_log_file='mysql-bin.000001',master_log_pos=120,master_connect_retry=10;
      1. Query OK, 0 rows affected, 2 warnings (0.03 sec)

    1

     

    澳门皇家赌场55533网址,mysql> show master status;

    1. 招来从库状态,

    ------------------ ---------- -------------- ---------------------------------- -------------------

      1. mysql> show slave status G;
      1. style="font-size: 16px">*************************** 1. row ***************************
      1. Slave_IO_State:
      1. Master_Host: 10.221.3.129
      1. Master_User: slave
      1. Master_Port: 3306
      1. Connect_Retry: 10
      1. Master_Log_File: mysql-bin.000001
      1. Read_Master_Log_Pos: 120
      1. Relay_Log_File: vm-kvm11853-app-relay-bin.000001
      1. Relay_Log_Pos: 4
      1. Relay_Master_Log_File: mysql-bin.000001
      1. Slave_IO_Running: No
      1. Slave_SQL_Running: No
      1. Replicate_Do_DB:
      1. style="font-size: 16px">Replicate_Ignore_DB:
      1. Replicate_Do_Table:
      1. style="font-size: 16px">Replicate_Ignore_Table:
      1. style="font-size: 16px">Replicate_Wild_Do_Table:
      1. style="font-size: 16px">Replicate_Wild_Ignore_Table:
      1. Last_Errno: 0
      1. Last_Error:
      1. Skip_Counter: 0
      1. Exec_Master_Log_Pos: 120
      1. Relay_Log_Space: 120
      1. Until_Condition: None
      1. Until_Log_File:
      1. Until_Log_Pos: 0
      1. Master_SSL_Allowed: No
      1. style="font-size: 16px">Master_SSL_CA_File:
      1. style="font-size: 16px">Master_SSL_CA_Path:
      1. Master_SSL_Cert:
      1. Master_SSL_Cipher:
      1. Master_SSL_Key:
      1. Seconds_Behind_Master: NULL
      1. style="font-size: 16px">Master_SSL_Verify_Server_Cert: No
      1. Last_IO_Errno: 0
      1. Last_IO_Error:
      1. Last_SQL_Errno: 0
      1. Last_SQL_Error:
      1. style="font-size: 16px">Replicate_Ignore_Server_Ids:
      1. Master_Server_Id: 0
      1. Master_UUID:
      1. Master_Info_File: /var/lib/mysql/master.info
      1. SQL_Delay: 0
      1. SQL_Remaining_Delay: NULL
      1. style="font-size: 16px">Slave_SQL_Running_State:
      1. Master_Retry_Count: 86400
      1. Master_Bind:
      1. style="font-size: 16px">Last_IO_Error_Timestamp:
      1. style="font-size: 16px">Last_SQL_Error_Timestamp:
      1. Master_SSL_Crl:
      1. style="font-size: 16px">Master_SSL_Crlpath:
      1. style="font-size: 16px">Retrieved_Gtid_Set:
      1. style="font-size: 16px">Executed_Gtid_Set:
      1. Auto_Position: 0
      1. 1 row in set (0.00 sec)

    | File            | Position | Binlog_Do_DB | Binlog_Ignore_DB                | Executed_Gtid_Set |

     

    ------------------ ---------- -------------- ---------------------------------- -------------------

    1. 当中最重要的正是,那四个参数,供给YES,但那边为NO,

    | mysql-bin.000004 |    28125 | ufind_db    | information_schema,cluster,mysql |                  |

      1. Slave_IO_Running: No
      1. Slave_SQL_Running: No

    ------------------ ---------- -------------- ---------------------------------- -------------------

     

    1 row in set (0.00 sec)

    1. 寻觅错误日志,提示不恐怕找见./performance_schema/cond_instances.frm文件,

    Slave的配置

      1. [root@vm-kvm10001-app mysql]# vm-kvm10001-app.err
      1. 2017-08-29 16:10:37 22933 [ERROR] /usr/sbin/mysqld: Can't find file: './performance_schema/cond_instances.frm' (errno: 13 - Permission denied)

    1、从库的布置,首先也是修改配置文件:/etc/my.cnf 如下:

     

    log-bin=mysql-bin

    1. 看一下其目录,开掘performance_schema文件夹,是root权限,mysql用户不能访问,由此供给修改一下其权力,

    server-id=3

    本文由68399皇家赌场发布于虚拟主机,转载请注明出处:mysql五.6搭建主从复制

    关键词: 68399皇家赌场 mysql 运维

上一篇:郝斌–SQL Server2007学习笔记

下一篇:没有了