zoukankan      html  css  js  c++  java
  • Mysql主从同步配置

    一、主数据库的配置

    1. my.cnf(Linux)/my.ini(Windows)

    在配置文件参数选项

    [mysqld]

    下面添加如下内容

    log_bin=mysql-bin
    server_id=1
    innodb_flush_log_at_trx_commit=1
    sync_binlog=1
    binlog_do_db=omc
    binlog_ignore_db=mysql

    说明:

    server-id=1中的1可以任定义,只要是唯一的就行。
    binlog-do-db=omc是表示只备份库omc。
    binlog_ignore_db=mysql表示忽略备份库mysql。

    2. 以root身份登录主数据库后台

    2.1 创建用户

    mysql> create user 'mstest'@'从服务器的地址' identified by '123456';

    2.2 添加从数据库关系

    mysql> grant replication slave on *.* to 'mstest'@'从服务器的地址' identified by '123456';

    PS: 必须以root身份执行2.2,否则会提示没有权限之类的。

    如:Access denied for user 'remote'@'%' (using password: YES)

    3.重启数据库

    3.1 查看主数据库状态

    mysql> show master status;
    +------------------+----------+--------------+------------------+-------------------+
    | File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
    +------------------+----------+--------------+------------------+-------------------+
    | mysql-bin.000002 | 2122     | omc          | mysql            |                   |
    +------------------+----------+--------------+------------------+-------------------+

    3.2 如果看到如上图类似的数据,主数据库配置成功。

    记录下File和Position的值(从数据库需的配置要用)。

    二、从数据库的配置

    1. my.cnf(Linux)/my.ini(Windows)

    在配置文件参数选项

    [mysqld]

    下面添加如下内容

    server_id=2

    说明:

    server-id=1中的1可以任定义,只要是唯一的就行。

    2. 以root身份登录从数据库后台

    mysql> change master to
    mysql> master_host='主数据库的地址',
    mysql> master_user='mstest',
    mysql> master_port=3306,
    mysql> master_log_file='mysql-bin.000002',
    mysql> master_log_pos=2122,
    mysql> master_connect_retry=10;

    说明:

    master_user是主数据库中刚才创建的信息。
    master_log_file是主数据状态数据中的File项的值。
    master_log_pos是主数据状态数据中的Position项的值。

    2.1 查看从数据库状态

    ...
    Slave_IO_Running | Slave_SQL_Running
    Yes              | Yes
    ...

    三、运行测试

    手动主数据库中插入一条数据。

    从数据库中自动的增加了这一条数据。

    恭喜,主从数据库配置成功。


    四、一个可能的报错

    1. [ERROR] Slave I/O: Got fatal error 1236 from master when reading data from binary log:

    'binlog truncated in the middle of event; consider out of disk space on master; the first event 'mysql-bin.000002' at 2122

    2. 原因:

    主数据库的Position参数是不断变化的。这个错误提示就是从数据库的master_log_pos配置和主数据库的不匹配。

    3. 解决:

    1. 在主数据库执行show master status查看最新的Position值。

    2. 在从数据库执行以下命令:

    stop slave;
    change master to master_log_file='mysql-bin.000002',master_log_pos=2319;
    start slave;
  • 相关阅读:
    【拆点费用流】【HDU1853】【 Cyclic Tour】
    【最小费用最大流】【HDU1533】【Going Home】
    【最大流,二分图匹配】【hdu2063】【过山车】
    【最小费用最大流模板】【Uva10806+Spring Team PK】Dijkstra, Dijkstra,
    【最大流之sap】【HDU1532】模板题
    HDU 6130 Kolakoski 思维个屁 水题
    Codeforces 837 D Round Subset DP 思维
    Educational Codeforces Round 26
    Codeforces Round 427 B The name on the board 水题
    Codeforces Round 428 B Game of the rows 贪心 思维
  • 原文地址:https://www.cnblogs.com/yoyotl/p/7523034.html
Copyright © 2011-2022 走看看