ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MySQL主从复制

2020-06-30 18:55:59  阅读:178  来源: 互联网

标签:主从复制 slave log MySQL server MASTER mysql Master


1.修改主 vim /etc/my.cnf文件

 

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

character_set_server=utf8
init_connect='SET NAMES utf8'

#主服务器唯一ID,跟数字大小没有关系,只是唯一
server-id=1
#启用二进制日志
log-bin=mysql-bin
## 设置不要复制的数据库(可设置多个)
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
##设置需要复制的数据库
binlog-do-db=mydb
##设置logbin格式
binlog_format=STATEMENT

binlog日志三种格式:
1. STATEMENT 不能保证数据一致性 如insert into ...now();
2.ROW 把每条语句都写上,造成数据庞大。
3.MIXED 不能识别@@host name 语句

2.修改从   :

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

character_set_server=utf8
init_connect='SET NAMES utf8'
#主服务器唯一ID,跟数字大小没有关系,只是唯一
server-id=2
#启用中继日志
relay-log=mysql-relay





3. 主机、从机重启MySQL服务,主机从机都关闭防火墙,不关防火墙需开放端口,在主机上建立帐户并授权slave

     #在主机MySQL里执行授权命令   %表示任意IP

        进入主机MySQL客户端:   mysql -uroot -p密码;    从机也是如此

              GRANT REPLICATION SLAVE ON *.* TO 'lvym(可随意命名,但要与后面一致)'@'%' IDENTIFIED BY '密码';

              #查询master的状态

                       show master status;

          

 

 

 #记录下File和Position的值  #执行完此步骤后不要再操作主服务器MySQL,防止主服务器状态值变化

 

在从机上配置需要复制的主机:

      CHANGE MASTER TO MASTER_HOST='主机IP',
      MASTER_USER='lvym(授权用户)',
      MASTER_PASSWORD='Lvym777@(密码)',
      MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=154;

 

#启动从服务器复制功能:

               start slave;

#查看从服务器状态:

              show slave status\G;

               

 

 

 #上面两个参数都是Yes,则说明主从配置成功!#Slave_IO_Running: Yes    #Slave_SQL_Running: Yes

    如何重新配置主从:主机从机任意都可输入

            stop slave;

            reset master;

 

在主数据库写操作,从数据库会有,在从数据库写操作,主数据库不会有。

 ------------------------------------------------------Docker 搭建MySQL8主从复制------------------------------------------

1.主机新建my.cnf文件用于挂载     必须

 

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid character_set_server=utf8 #主服务器唯一ID,跟数字大小没有关系,只是唯一 server-id=1 #启用二进制日志 log-bin=mysql-bin ## 设置不要复制的数据库(可设置多个) binlog-ignore-db=mysql binlog-ignore-db=information_schema ##设置需要复制的数据库 binlog-do-db=mydb ##设置logbin格式 binlog_format=STATEMENT

主机新建data,logs文件夹 用于挂载     可选

 

 2.从机新建my.cnf文件用于挂载     必须

 

 

......
#主服务器唯一ID,跟数字大小没有关系,只是唯一
server-id=2
#启用中继日志
relay-log=mysql-relay

从机新建data,logs文件夹 用于挂载     可选

 

 3.拉取并运行MySQL

主机
docker run -d -p 3306:3306 -v /mydata/mysql/master/conf:/etc/mysql/conf.d -v /mydata/mysql/master/logs:/logs -v /mydata/mysql/master/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql
从机
docker run -d -p 3307:3306  -v /mydata/mysql/slave/conf:/etc/mysql/conf.d -v /mydata/mysql/slave/logs:/logs -v /mydata/mysql/slave/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456  mysql

4.进入MySQL容器,登录MySQL

主机操作
[root@lvym springboot]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                               NAMES
7238de2486fa        mysql               "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp, 33060/tcp   angry_euler
进入容器
[root@lvym springboot]# docker exec -it 7238de2486fa bash
登录
root@7238de2486fa:/# mysql -uroot -p123456 或 mysql -uroot -p123456 -h 192.168.146.140 -P 3306
主机授权

CREATE USER 'slave'@'%' IDENTIFIED BY 'slave';
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%';
alter user 'slave'@'%' identified with mysql_native_password by 'slave';
flush privileges;

mysql> CREATE USER 'slave'@'%' IDENTIFIED BY 'slave';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%';
Query OK, 0 rows affected (0.00 sec)

mysql> alter user 'slave'@'%' identified with mysql_native_password by 'slave';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges; 刷新权限
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+--------------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         | Executed_Gtid_Set |
+------------------+----------+--------------+--------------------------+-------------------+
| mysql-bin.000003 |     1132 | mydb         | mysql,information_schema |                   |
+------------------+----------+--------------+--------------------------+-------------------+
1 row in set (0.01 sec)

从机操作
root@lvym springboot]# docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                               NAMES
c13eb9c67a43        mysql               "docker-entrypoint.s…"   18 minutes ago       Up 18 minutes       33060/tcp, 0.0.0.0:3307->3306/tcp   strange_tesla
进入容器
[root@lvym springboot]# docker exec -it c13eb9c67a43 bash
登录
root@c13eb9c67a43:/# mysql -uroot -p123456 或 mysql -uroot -p123456 -P 3307 -h 192.168.146.141
连接主机

CHANGE MASTER TO MASTER_HOST='192.168.146.140',
MASTER_USER='slave',
master_port=3306,
MASTER_PASSWORD='slave',
MASTER_LOG_FILE='mysql-bin.000003',
MASTER_LOG_POS=1132;

mysql> CHANGE MASTER TO MASTER_HOST='192.168.146.140',
    -> MASTER_USER='slave',
    -> master_port=3306,
    -> MASTER_PASSWORD='slave',
    -> MASTER_LOG_FILE='mysql-bin.000003',
    -> MASTER_LOG_POS=1132;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected (0.01 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.146.140
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000003
          Read_Master_Log_Pos: 1132
               Relay_Log_File: mysql-relay.000002
                Relay_Log_Pos: 324
        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: 1132
              Relay_Log_Space: 529
              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: 6958c5fe-bab9-11ea-8bc7-0242ac110002
             Master_Info_File: mysql.slave_master_info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
           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
         Replicate_Rewrite_DB:
                 Channel_Name:
           Master_TLS_Version:
       Master_public_key_path:
        Get_master_public_key: 0
            Network_Namespace:
1 row in set (0.00 sec)

进行验证

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

翻译 朗读 复制 正在查询,请稍候…… 重试 朗读 复制 复制 朗读 复制 via 谷歌翻译(国内)

 

翻译 朗读 复制 正在查询,请稍候…… 重试 朗读 复制 复制 朗读 复制 via 谷歌翻译(国内)

标签:主从复制,slave,log,MySQL,server,MASTER,mysql,Master
来源: https://www.cnblogs.com/lvym/p/12669887.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有