MySQL主从笔记

Master:192.168.30.45
Slave: 192.168.30.46

Master配置:

    #my.cnf
    server-id=100
    log_bin = mysql-bin
    binlog-format = row
    log-slave-updates = true

    #创建同步用户
    >grant replication slave on *.* to 'repl'@'%' identified by '123';

Slave配置:
#my.cnf
server-id=200
log_bin = mysql-bin
relay_log = relay-bin
binlog-format = row
log-slave-updates = true

修改好配置文件后,分别重启主从库

Master查看:

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

配置Slave:
#多行写法:
mysql> CHANGE MASTER TO
-> MASTER_HOST='192.168.30.45',
-> MASTER_USER='repl',
-> MASTER_PASSWORD='123',
-> MASTER_LOG_FILE='mysql-bin.000008',
-> MASTER_LOG_POS=589;
#一行写法:
>CHANGE MASTER TO MASTER_HOST='192.168.30.45', MASTER_USER='repl', MASTER_PASSWORD='123', MASTER_LOG_FILE='mysql-bin.000008', MASTER_LOG_POS=589;

    #启动从服务器复制线程
    >start slave;
    
    mysql> show slave status\G;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 192.168.30.45
                      Master_User: repl
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000008
              Read_Master_Log_Pos: 925
                   Relay_Log_File: relay-bin.000002
                    Relay_Log_Pos: 656
            Relay_Master_Log_File: mysql-bin.000008
                 Slave_IO_Running: Yes
                Slave_SQL_Running: Yes
                  Replicate_Do_DB: 
              Replicate_Ignore_DB: 


    Slave_IO_State #从站的当前状态 
    Slave_IO_Running: Yes #读取主程序二进制日志的I/O线程是否正在运行 
    Slave_SQL_Running: Yes #执行读取主服务器中二进制日志事件的SQL线程是否正在运行。与I/O线程一样 
    Seconds_Behind_Master #是否为0,0就是已经同步了

创建测试案例:

Master主库:
>create database zhang;
>use zhang;
>create table zhang(id int(11) not null);

    mysql> show master status;
    +------------------+----------+--------------+------------------+-------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +------------------+----------+--------------+------------------+-------------------+
    | mysql-bin.000008 |      925 |              |                  |                   |
    +------------------+----------+--------------+------------------+-------------------+
    1 row in set (0.00 sec)
    
    mysql> show slave hosts;
    +-----------+------+------+-----------+--------------------------------------+
    | Server_id | Host | Port | Master_id | Slave_UUID                           |
    +-----------+------+------+-----------+--------------------------------------+
    |        90 |      | 3306 |       100 | d0213846-8e33-11e7-becc-000c29cf61b1 |
    +-----------+------+------+-----------+--------------------------------------+
    1 row in set (0.00 sec)
    
    mysql> show binary logs;
    +------------------+-----------+
    | Log_name         | File_size |
    +------------------+-----------+
    | mysql-bin.000001 |       177 |
    | mysql-bin.000002 |       177 |
    | mysql-bin.000003 |       177 |
    | mysql-bin.000004 |       177 |
    | mysql-bin.000005 |       177 |
    | mysql-bin.000006 |       177 |
    | mysql-bin.000007 |       177 |
    | mysql-bin.000008 |       925 |
    +------------------+-----------+
    8 rows in set (0.00 sec)


    mysql> show binlog events;
    +------------------+-----+----------------+-----------+-------------+---------------------------------------+
    | Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
    +------------------+-----+----------------+-----------+-------------+---------------------------------------+
    | mysql-bin.000001 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.19-log, Binlog ver: 4 |
    | mysql-bin.000001 | 123 | Previous_gtids |         1 |         154 |                                       |
    | mysql-bin.000001 | 154 | Stop           |         1 |         177 |                                       |
    +------------------+-----+----------------+-----------+-------------+---------------------------------------+
    3 rows in set (0.00 sec)


    mysql> show binlog events in 'mysql-bin.000001';
    +------------------+-----+----------------+-----------+-------------+---------------------------------------+
    | Log_name         | Pos | Event_type     | Server_id | End_log_pos | Info                                  |
    +------------------+-----+----------------+-----------+-------------+---------------------------------------+
    | mysql-bin.000001 |   4 | Format_desc    |         1 |         123 | Server ver: 5.7.19-log, Binlog ver: 4 |
    | mysql-bin.000001 | 123 | Previous_gtids |         1 |         154 |                                       |
    | mysql-bin.000001 | 154 | Stop           |         1 |         177 |                                       |
    +------------------+-----+----------------+-----------+-------------+---------------------------------------+
    3 rows in set (0.01 sec)

Slave数据库启动:
>start slave;

    >stop slave;

半同步复制安装配置:

    [root@localhost plugin]# cd /app/local/mysql/lib/plugin/
    [root@localhost plugin]# ls semisync_*
    semisync_master.so  semisync_slave.so
    [root@localhost plugin]# 

Master配置:
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so';
mysql> set global rpl_semi_sync_master_enabled=1;
Query OK, 0 rows affected (0.00 sec)

Slave配置:

    mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so';
    mysql> set global rpl_semi_sync_slave_enabled=1;


    #my.cnf
    rpl_semi_sync_master_enabled = 1
    rpl_semi_sync_slave_enabled = 1


    set global rpl_semi_sync_master_timeout = 10000    10000毫秒=10秒

    rpm_semi_sync_master_wait_no_salve

    rpl_semi_sync_slave_trace_level = 32


    mysql> show status like '%semi%';
    +--------------------------------------------+-------+
    | Variable_name                              | Value |
    +--------------------------------------------+-------+
    | Rpl_semi_sync_master_clients               | 0     |
    | Rpl_semi_sync_master_net_avg_wait_time     | 0     |
    | Rpl_semi_sync_master_net_wait_time         | 0     |
    | Rpl_semi_sync_master_net_waits             | 0     |
    | Rpl_semi_sync_master_no_times              | 0     |
    | Rpl_semi_sync_master_no_tx                 | 0     |
    | Rpl_semi_sync_master_status                | ON    |
    | Rpl_semi_sync_master_timefunc_failures     | 0     |
    | Rpl_semi_sync_master_tx_avg_wait_time      | 0     |
    | Rpl_semi_sync_master_tx_wait_time          | 0     |
    | Rpl_semi_sync_master_tx_waits              | 0     |
    | Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
    | Rpl_semi_sync_master_wait_sessions         | 0     |
    | Rpl_semi_sync_master_yes_tx                | 0     |
    +--------------------------------------------+-------+
    14 rows in set (0.00 sec)


    >show variables like '%rpl_semi_sync%';
    >show status like '%rpl_semi_sync%';

你可能感兴趣的:(MySQL主从笔记)