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

    先安装mysql

    注意指定编码为utf8mb4

    5.6的话,单机:

    https://www.cnblogs.com/yunns/p/4877333.html

    5.7的话看最后

    主节点: 添加2行

    [mysql]
    default-character-set=utf8mb4
    socket=/var/lib/mysql/mysql.sock
    
    [mysqld]
    skip-name-resolve
    port = 3306
    socket=/var/lib/mysql/mysql.sock
    basedir=/main/mysql5.6.41
    datadir=/main/mysql5.6.41/data
    max_connections=200
    character-set-server=utf8mb4
    default-storage-engine=INNODB
    lower_case_table_names=1
    max_allowed_packet=16M
    collation-server = utf8mb4_unicode_ci
    init_connect='SET NAMES utf8mb4'
    character-set-client-handshake = FALSE
    # master-slave 需要主从复制的时候才添加下面两行
    log-bin=mysql-bin
    server-id=1

    从节点: 添加一行id

    [mysql]
    default-character-set=utf8mb4
    socket=/var/lib/mysql/mysql.sock
    
    [mysqld]
    skip-name-resolve
    port = 3306
    socket=/var/lib/mysql/mysql.sock
    basedir=/main/mysql5.6.41
    datadir=/main/mysql5.6.41/data
    max_connections=200
    character-set-server=utf8mb4
    default-storage-engine=INNODB
    lower_case_table_names=1
    max_allowed_packet=16M
    collation-server = utf8mb4_unicode_ci
    init_connect='SET NAMES utf8mb4'
    character-set-client-handshake = FALSE
    # master-slave 需要主从复制的时候才添加下面1行
    server-id=2

    然后安装:

    ./scripts/mysql_install_db --user=mysql  --defaults-file=/etc/my.cnf --basedir=/main/mysql5.6.41/  --datadir=/main/mysql5.6.41/data/

    或者正常单节点安装完了再来设置最后那两行也行.

    然后开始master上配置一个用户,允许slave从远端登录:

    ip是slave的ip:

    CREATE USER 'repl'@'172.17.222.161' IDENTIFIED BY '迷马密码米吗';#创建用户
    GRANT REPLICATION SLAVE ON *.* TO 'repl'@'172.17.222.161';
    flush privileges;

    然后查看master的状态,尤其注意File和Position:

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

    然后去从节点,登录mysql后配置slave:

    MASTER_LOG_FILE和position信息和上面相同, 配置让他从主节点同步信息:

    mysql> CHANGE MASTER TO
        ->      MASTER_HOST='172.17.222.160',
        ->      MASTER_USER='repl',
        ->      MASTER_PASSWORD='迷马密码米吗',
    -> MASTER_LOG_FILE='mysql-bin.000003',
    -> MASTER_LOG_POS=120;


    Query OK,
    0 rows affected, 2 warnings (0.01 sec)

    启动slave:

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

    查看slave状态:

    mysql> show slave statusG;
    *************************** 1. row ***************************
                   Slave_IO_State: Waiting for master to send event
                      Master_Host: 172.17.222.160
                      Master_User: repl
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000003
              Read_Master_Log_Pos: 120
                   Relay_Log_File: iz2zej5nlztmm1frnyl4wxz-relay-bin.000002
                    Relay_Log_Pos: 283
            Relay_Master_Log_File: mysql-bin.000003
                 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: 120
                  Relay_Log_Space: 474
                  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: 29c0bb13-9199-11e8-92f2-00163e123ea4
                 Master_Info_File: /main/mysql5.6.41/data/master.info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
               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
    1 row in set (0.00 sec)

    如果遇到错误:

    这是000003这个文件在master上不存在:

    mysql> show slave statusG;
    *************************** 1. row ***************************
                   Slave_IO_State: 
                      Master_Host: 172.17.222.160
                      Master_User: repl
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000003
              Read_Master_Log_Pos: 73
                   Relay_Log_File: iz2zej5nlztmm1frnyl4wxz-relay-bin.000001
                    Relay_Log_Pos: 4
            Relay_Master_Log_File: mysql-bin.000003
                 Slave_IO_Running: No
                Slave_SQL_Running: No
                  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: 73
                  Relay_Log_Space: 120
                  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: 1236
                    Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file'
                   Last_SQL_Errno: 0
                   Last_SQL_Error: 
      Replicate_Ignore_Server_Ids: 
                 Master_Server_Id: 1
                      Master_UUID: 29c0bb13-9199-11e8-92f2-00163e123ea4
                 Master_Info_File: /main/mysql5.6.41/data/master.info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: 
               Master_Retry_Count: 86400
                      Master_Bind: 
          Last_IO_Error_Timestamp: 180727 21:10:17
         Last_SQL_Error_Timestamp: 
                   Master_SSL_Crl: 
               Master_SSL_Crlpath: 
               Retrieved_Gtid_Set: 
                Executed_Gtid_Set: 
                    Auto_Position: 0
    1 row in set (0.00 sec)

    这是position和master的状态不匹配:

    mysql> show slave statusG;
    *************************** 1. row ***************************
                   Slave_IO_State: 
                      Master_Host: 172.17.222.160
                      Master_User: repl
                      Master_Port: 3306
                    Connect_Retry: 60
                  Master_Log_File: mysql-bin.000003
              Read_Master_Log_Pos: 73
                   Relay_Log_File: iz2zej5nlztmm1frnyl4wxz-relay-bin.000002
                    Relay_Log_Pos: 283
            Relay_Master_Log_File: mysql-bin.000003
                 Slave_IO_Running: No
                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: 73
                  Relay_Log_Space: 474
                  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: 1236
                    Last_IO_Error: 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.000003' at 73, the last event read from './mysql-bin.000003' at 73, the last byte read from './mysql-bin.000003' at 92.'
                   Last_SQL_Errno: 0
                   Last_SQL_Error: 
      Replicate_Ignore_Server_Ids: 
                 Master_Server_Id: 1
                      Master_UUID: 29c0bb13-9199-11e8-92f2-00163e123ea4
                 Master_Info_File: /main/mysql5.6.41/data/master.info
                        SQL_Delay: 0
              SQL_Remaining_Delay: NULL
          Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
               Master_Retry_Count: 86400
                      Master_Bind: 
          Last_IO_Error_Timestamp: 180727 21:12:45
         Last_SQL_Error_Timestamp: 
                   Master_SSL_Crl: 
               Master_SSL_Crlpath: 
               Retrieved_Gtid_Set: 
                Executed_Gtid_Set: 
                    Auto_Position: 0
    1 row in set (0.00 sec)

    5.7的话,不安装主从备份的话:

    # 创建用户
    groupadd mysql
    useradd -g mysql mysql
    yum -y install numactl
    yum install -y perl-Module-Install.noarch
    yum install -y libaio*

    配置文件同上面,不要最后两行主从复制即可。

    初始化:

    # 上面配置的地址

    [root@localhost bin]# mkdir /var/lib/mysql/
    [root@localhost bin]# chown mysql:mysql /var/lib/mysql/
    ./mysqld  --initialize --user=mysql

    注意看日志,会生成一个随机密码

    https://www.jb51.net/article/133835.htm

  • 相关阅读:
    火狐插件火狐黑客插件将Firefox变成黑客工具的七个插件
    memcache安装环境:WINDOWS 7
    PHP正则表达式
    968. 监控二叉树 力扣(困难) dfs 官方说DP
    375. 猜数字大小 II 力扣(中等) 区间动态规划、记忆化搜索
    629. K个逆序对数组 力扣(困难) 区间动态规划
    剑指 Offer 51. 数组中的逆序对 力扣(困难) 巧用归并排序算法
    488. 祖玛游戏 力扣(困难) dfs
    16. 最接近的三数之和 力扣(中等) 双指针
    319. 灯泡开关 力扣(中等) 数论
  • 原文地址:https://www.cnblogs.com/radio/p/9379718.html
Copyright © 2011-2022 走看看