Linux学习-MySQL主从复制

Posted 丢爸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux学习-MySQL主从复制相关的知识,希望对你有一定的参考价值。

复制的作用:

  • 辅助实现备份
  • 高可用
  • 异地灾备
  • Scale out:分摊负载
    主从架构中,不使用mysql代理,如何实现主的写,从的读??
    双主模型:无法减轻MySQL写操作

读写分离:
mysql-proxy
amoeba
cobar

MySQL5.6:GTID, Multi-thread Replication
配置MySQL复制基本步骤

  1. master
  • 启用二进制日志
  • log-bin=master-bin
  • log-bin-index=master-bin.index
  • 选择一个惟一的server-id
    server-id={0…2^32}
  • 创建具有复制权限的用户
    REPLICATION SLAVE
    REPLICATION CLIENT
  1. slave
  • 启用中继日志
  • relay-log=relay-log
  • relay-log=index #
  • 选择一个惟一的server-id
    server-id={0…2^32}
  • 连接至主服务器,并开始复制数据
    mysql>CHANGE MASTER TO MASTER_HOST=’’,MASTER_PORT=’’,MASTER_LOG_FILE=’’,MASTER_LOG_FILE_POS=’’,MASTER_USER=’’,MASTER_PASSWORD=’’;
    mysql>START SLAVE IO_Thread;
    mysql>START SLAVE SQL_Thread;

复制线程
master:dump
slave:IO_Thread,SQL_Thread

部署MySQL Master服务器

#创建数据目录
[root@mysql1 ~]# mkdir -v /data
mkdir: created directory `/data'
#添加mysql用户
[root@mysql1 ~]# useradd -r mysql -M
#修改数据目录权限为mysql
[root@mysql1 ~]# chown -R mysql.mysql /data
#解压mysql到/usr/local目录下
[root@mysql1 ~]# tar -xf mysql-5.6.10-linux-glibc2.5-x86_64.tar.gz -C /usr/local
#创建链接
[root@mysql1 ~]# cd /usr/local
[root@mysql1 local]# ln -sv /usr/local/mysql-5.6.10-linux-glibc2.5-x86_64 /usr/local/mysql
`/usr/local/mysql' -> `/usr/local/mysql-5.6.10-linux-glibc2.5-x86_64'
#修改目录下所有文件权限
[root@mysql1 local]# cd mysql
[root@mysql1 mysql]# chown -R root.mysql ./*
#初使化mysql数据
[root@mysql1 mysql]# scripts/mysql_install_db --user=mysql --datadir=/data
#复制MySQL配置文件和服务启动脚本
[root@mysql1 mysql]# cp support-files/my-default.cnf /etc/my.cnf
cp: overwrite `/etc/my.cnf'? y
[root@mysql1 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
#设置开机启动
[root@mysql1 mysql]# chkconfig --add mysqld
[root@mysql1 mysql]# chkconfig mysqld on
#/etc/my.cnf配置文件内容
[root@mysql1 mysql]# grep -v '^#' /etc/my.cnf

[client]
port=3306
socket=/tmp/mysql.sock
[mysqld]
port=3306
socket=/tmp/mysql.sock
datadir=/data
skip-external-locking
key_buffer_size=256M
max_allowed_packet=1M
table_open_cache=256
sort_buffer_size=1M
read_rnd_buffer_size=4M
myisam_sort_buffer_size=64M
thread_cache_size=8
query_cache_size=16M
thread_concurrency=8
log-bin=master-bin
log-bin-index=master-bin.index
binlog_format=mixed
server-id=1
innodb_file_per_table=1
#配置文件修改完成后,启动mysql服务
[root@mysql1 etc]# service mysqld start
Starting MySQL. SUCCESS! 
#导出mysql执行目录到环境变量
[root@mysql1 etc]# cat profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
#执行一下脚本
[root@mysql1 etc]# . /etc/profile.d/mysql.sh
#Master服务器配置完成,
[root@mysql1 etc]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 1
Server version: 5.6.10-log MySQL Community Server (GPL)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

#授权用户192.168.0.136服务器上的repluser用户有复制权限
mysql> GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.0.136' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

#将安装文件复制到Slave服务器上
[root@mysql1 ~]# scp mysql-5.6.10-linux-glibc2.5-x86_64.tar.gz 192.168.0.136:/root
The authenticity of host '192.168.0.136 (192.168.0.136)' can't be established.
RSA key fingerprint is 2f:b6:90:d7:24:13:57:f3:ed:19:2a:9a:33:b3:19:c8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.136' (RSA) to the list of known hosts.
root@192.168.0.136's password: 
mysql-5.6.10-linux-glibc2.5-x86_64.tar.gz                                                                                                                  100%  289MB  96.4MB/s   00:03 
#复制配置文件到从服务器进行修改
[root@mysql1 ~]# scp /etc/my.cnf 192.168.0.136:/etc/my.cnf
root@192.168.0.136's password: 
my.cnf                                                                                                                                                     100% 1514     1.5KB/s   00:00   

部署MySQL Slave服务器

#解压mysql至/usr/local目录下
[root@mysql2 ~]# tar xf mysql-5.6.10-linux-glibc2.5-x86_64.tar.gz -C /usr/local
#创建文件链接
[root@mysql2 ~]# cd /usr/local
[root@mysql2 local]# ln -sv mysql-5.6.10-linux-glibc2.5-x86_64 mysql
`mysql' -> `mysql-5.6.10-linux-glibc2.5-x86_64'
#添加mysql用户
[root@mysql2 local]# useradd -r mysql -M
#修改目录文件权限
[root@mysql2 local]# chown mysql.mysql /data/
[root@mysql2 local]# chown root.mysql /usr/local/mysql/*
#初使化数据库
[root@mysql2 mysql]# scripts/mysql_install_db --user=mysql --datadir=/data
#复制启动文件
[root@mysql2 mysql]# cp support-files/mysql.server /etc/rc.d/init.d/mysqld
#修改配置文件
[root@mysql2 mysql]# grep -v '^#' /etc/my.cnf
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld]
port=3306
socket=/tmp/mysql.sock
datadir=/data
basedir=/usr/local/mysql/
skip-external-locking
key_buffer_size=256M
max_allowed_packet=1M
table_open_cache=256
sort_buffer_size=1M
read_rnd_buffer_size=4M
myisam_sort_buffer_size=64M
thread_cache_size=8
query_cache_size=16M
thread_concurrency=8
binlog_format=mixed
server-id=11
relay-log=relay-log
relay-log-index=relay-log.index
innodb_file_per_table=1
#启动服务
[root@mysql2 mysql]# service mysqld start
Starting MySQL. SUCCESS! 
#修改PATH环境变量
[root@mysql2 mysql]# cat /etc/profile.d/mysql.sh
export PATH=$PATH:/usr/local/mysql/bin
[root@mysql2 mysql]# . /etc/profile.d/mysql.sh
#启动mysql客户端
[root@mysql2 mysql]# mysql
mysql> show slave status \\G
Empty set (0.00 sec)
#查看Master服务器上二进制日志位置
mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |      413 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> SHOW BINLOG EVENTS IN 'master-bin.000001';
+-------------------+-----+-------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+
| Log_name          | Pos | Event_type  | Server_id | End_log_pos | Info                                                                                                                            |
+-------------------+-----+-------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+
| master-bin.000001 |   4 | Format_desc |         1 |         120 | Server ver: 5.6.10-log, Binlog ver: 4                                                                                           |
| master-bin.000001 | 120 | Query       |         1 |         334 | GRANT REPLICATION SLAVE ON *.* TO 'repluser'@'192.168.0.136' IDENTIFIED BY PASSWORD '*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9' |
| master-bin.000001 | 334 | Query       |         1 |         413 | flush privileges                                                                                                                |
+-------------------+-----+-------------+-----------+-------------+---------------------------------------------------------------------------------------------------------------------------------+
3 rows in set (0.00 sec)
#从服务器上执行
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.135',MASTER_USER='repluser',MASTER_PASSWORD='123456',MASTER_LOG_FILE='master-bin.000001',MASTER_LOG_POS=413;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
#查看Slave服务器的信息
mysql> show slave status \\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.0.135
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
                #Master服务器日志文件
              Master_Log_File: master-bin.000001
              #Master服务器日志文件位置
          Read_Master_Log_Pos: 413
          #中继日志文件
               Relay_Log_File: relay-log.000001
               #中继日志位置
                Relay_Log_Pos: 4
        Relay_Master_Log_File: master-bin.000001
             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
                 #执行Master日志的位置
          Exec_Master_Log_Pos: 413
              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: 
               #Slave服务器比Master服务器慢的时间
        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: 0
                  Master_UUID: 
             Master_Info_File: /data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           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)
#启动从服务器
mysql> START SLAVE;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW SLAVE STATUS \\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.135
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 413
               Relay_Log_File: relay-log.000002
                Relay_Log_Pos: 284
        Relay_Master_Log_File: master-bin.000001
             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: 413
              Relay_Log_Space: 451
              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: 94da1172-96b8-11eb-845a-000c291f647c
             Master_Info_File: /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)


#在Master服务器上创建mydb数据库
mysql> create database mydb;
Query OK, 1 row affected (0.00 sec)
#到从服务器上查看
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
**| mydb               |**
| mysql              |
| performance_schema |
| test               |
+--------------------+
5 rows in set (0.00 sec)

#将Slave服务器设置为只读
mysql> SHOW GLOBAL VARIABLES LIKE 'read%';
+----------------------+---------+
| Variable_name        | Value   |
+----------------------+---------+
| read_buffer_size     | 131072  |
**| read_only            | OFF     |**
| read_rnd_buffer_size | 4194304 |
+----------------------+---------+
3 rows in set (0.00 sec)
#修改/etc/my.cnf配置文件中添加以下语句
#注:对具有super用户权限的用户不生效
read_only=ON
#预防Master服务器上日志在缓冲区中没来得及写入二进制日志文件中,服务器出现故障情况,配置Master服务器的mysql配置参数sync_binlog直接同步二进制日志。
mysql> SHOW GLOBAL VARIABLES LIKE '%binlog%';
+-----------------------------------------+----------------------+
| Variable_name                           | Value                |
+-----------------------------------------+----------------------+
| binlog_cache_size                       | 32768                |
| binlog_checksum                         | CRC32                |
| binlog_direct_non_transactional_updates | OFF                  |
| binlog_format                           | MIXED                |
| binlog_max_flush_queue_time             | 0                    |
| binlog_order_commits                    | ON                   |
| binlog_row_image                        | FULL                 |
| binlog_rows_query_log_events            | OFF                  |
| binlog_stmt_cache_size                  | 32768                |
| innodb_api_enable_binlog                | OFF                  |
| innodb_locks_unsafe_for_binlog          | OFF                  |
| max_binlog_cache_size                   | 18446744073709547520 |
| max_binlog_size                         | 1073741824           |
| max_binlog_stmt_cache_size              | 18446744073709547520 |
| sync_binlog                             | 0                    |
+-----------------------------------------+----------------------+
#在Master服务器上/etc/my.cnf配置文件中添加以下行,用于事务安全
sync_binlog=ON

#停止Slave复制
mysql> STOP SLAVE;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW SLAVE STATUS \\G
*************************** 1. row ***************************
               Slave_IO_State: 
                  Master_Host: 192.168.0.135
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000001
          Read_Master_Log_Pos: 507
               Relay_Log_File: relay-log.000004
                Relay_Log_Pos: 284
        Relay_Master_Log_File: master-bin.000001
             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: 507
              Relay_Log_Space: 451
              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
                  Master_UUID: 94da1172-96b8-11eb-845a-000c291f647c
             Master_Info_File: /data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: 
           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)

实现主从服务器半同步复制

[root@mysql1 plugin]# pwd
/usr/local/mysql/lib/plugin
[root@mysql1 plugin]# ll
total 2112
-rwxr-xr-x. 1 root mysql  15221 Jan 23  2013 adt_null.so
-rwxr-xr-x. 1 root mysql  25603 Jan 23  2013 auth.so
-rwxr-xr-x. 1 root mysql  12364 Jan 23  2013 auth_socket.so
-rwxr-xr-x. 1 root mysql  25080 Jan 23  2013 auth_test_plugin.so
-rw-r--r--. 1 root mysql    227 Jan 23  2013 daemon_example.ini
drwxr-xr-x. 2 root mysql   4096 Apr  6 16:39 debug
-rwxr-xr-x. 1 root mysql 542939 Jan 23  2013 innodb_engine.so
-rwxr-xr-x. 1 root mysql  42601 Jan 23  2013 libdaemon_example.so
-rwxr-xr-x. 1 root mysql 586796 Jan 23  2013 libmemcached.so
-rwxr-xr-x. 1 root mysql  17539 Jan 23  2013 mypluglib.so
-rwxr-xr-x. 1 root mysql  18095 Jan 23  2013 qa_auth_client.so
-rwxr-xr-x. 1 root mysql  23806 Jan 23  2013 qa_auth_interface.so
-rwxr-xr-x. 1 root mysql  12926 Jan 23  2013 qa_auth_server.so
#半同步复制Master服务器使用的插件
**-rwxr-xr-x. 1 root mysql 410186 Jan 23  2013 semisync_master.so**
#半同步复制Slave服务器使用的插件
**-rwxr-xr-x. 1 root mysql 248342 Jan 23  2013 semisync_slave.so**
-rwxr-xr-x. 1 root mysql 139116 Jan 23  2013 validate_password.so

#连接至Master服务执行以下语句
mysql> install plugin rpl_semi_sync_master SONAME 'semisync_master.so';
Query OK, 0 rows affected (0.01 sec)
#rpl_semi_sync_master_enabled参数是否启用半同步主节点
mysql> SHOW GLOBAL VARIABLES LIKE '%rpl%';
+------------------------------------+-------+
| Variable_name                      | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled       | OFF   |
#超时时间,单位毫秒
| rpl_semi_sync_master_timeout       | 10000 |
#
| rpl_semi_sync_master_trace_level   | 32    |
#如果没有slave 是否等待
| rpl_semi_sync_master_wait_no_slave | ON    |
+------------------------------------+-------+
4 rows in set (0.00 sec)
#在主服务器/etc/my.cnf配置文件中添加以下信息
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=100  #1 second

#在从服务器上安装插件
mysql> install plugin rpl_semi_sync_slave SONAME 'semisync_slave.so';
Query OK, 0 rows affected (0.00 sec)
#查看全局变量
mysql> show global variables like '%rpl%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled     | OFF   |
| rpl_semi_sync_slave_trace_level | 32    |
+---------------------------------+-------+
2 rows in set (0.00 sec)
#在从服务器上/etc/my.cnf添加以下配置信息
rpl_semi_sync_slave_enabled=1

#在主服务器上查看rpl的状态,需要从服务器重新启动一下IO-Slave
mysql> show global status like '%rpl%';
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    1
Current database: *** NONE ***

+--------------------------------------------+-------+
| 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.01 sec)

mysql> show global status like '%rpl%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
**| Rpl_semi_sync_master_clients               | 1     |**
| 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)

GTID(Global Transaction Identifiers)全局事务标识符
Multi Thread Replication:每个数据库仅能使用一个线程,多线程复制只有在多个数据库复制时才有用
slave-parallel-workers=0
0:表示禁用此功能,数量要小于或等于数据库的数量

GTID-Master配置

[root@mysql1 ~]# grep -v '^#' /etc/my.cnf
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld]
port=3306
socket=/tmp/mysql.sock
datadir=/data
#log-bin二进制日志
log-bin=master-bin
#是否将日志记录至二进制日志
log-slave-updates=true
log-bin-index=master-bin.index
binlog_format=ROW
#启用GTID
gtid-mode=on
enforce-gtid-consistency=true
#将master-info中的信息记录到数据库表(mysql.slave_master_info)中,还可以记录到文件(master.info)中---master.info中让Master服务器记录每一个从服务器的连接信息,每一个从服务器所复制的二进制日志的文件名以及相关的事件位置等信息。
master-info-repository=TABLE
#将relay-log.info中的信息记录到数据库表(mysql.slave_master_info)中,还可以记录到文件(relay-log.info)中---slave服务器自己记录连接的主服务器的信息,主服务器的哪个二进制文件以及二进制文件的位置。
relay-log-info-repository=TABLE
#是控制 slave_master_info 表的更新频率,1表示每个事务执行完后更新
sync-master-info=1
#启动两个SQL_Thread线程
slave-parallel-workers=2
#二进制日志的校验方式
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log-events=1
report-host=mysql1
basedir=/usr/local/mysql/
skip-external-locking
key_buffer_size=256M
max_allowed_packet=1M
table_open_cache=256
sort_buffer_size=1M
read_rnd_buffer_size=4M
myisam_sort_buffer_size=64M
thread_cache_size=8
query_cache_size=16M
thread_concurrency=8
sync_binlog=1
server-id=1
rpl_semi_sync_master_enabled=1
rpl_semi_sync_master_timeout=100  #1 second
innodb_file_per_table=1

Slave服务器配置

[root@mysql2 mysql]# cat /etc/my.cnf
[mysqld]
port=3306
socket=/tmp/mysql.sock
datadir=/data
log-bin=mysql-bin
report-port=3306
log-slave-updates=true
binlog_format=ROW
gtid-mode=on
enforce-gtid-consistency=true
master-info-repository=TABLE
sync-master-info=1
slave-parallel-workers=2
binlog-checksum=CRC32
master-verify-checksum=1
slave-sql-verify-checksum=1
binlog-rows-query-log-events=1
report-host=mysql1
basedir=/usr/local/mysql/
server-id=11
#在Slave服务器上执行以下语句
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.135',master_user='repluser',master_password='123456',master_auto_position=1;
Query OK, 0 rows affected, 2 warnings (0.02 sec)

在Master服务器执行语句

mysql> create database test1;
Query OK, 1 row affected (0.14 sec)

mysql> show global variables like '%gtid%';
+--------------------------+----------------------------------------+
| Variable_name            | Value                                  |
+--------------------------+----------------------------------------+
| enforce_gtid_consistency | ON                                     |
| gtid_executed            | 94da1172-96b8-11eb-845a-000c291f647c:1 |
| gtid_mode                | ON                                     |
| gtid_owned               |                                        |
| gtid_purged              |                                        |
+--------------------------+----------------------------------------+
5 rows in set (0.01 sec)

mysql> show global variables like '%uuid%';
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| server_uuid   | 94da1172-96b8-11eb-845a-000c291f647c |
+---------------+--------------------------------------+
1 row in set (0.00 sec)

mysql> use test1;
Database changed
mysql> create table t1(id int,username varchar(30));
Query OK, 0 rows affected (0.02 sec)

mysql> show global variables like '%gtid%';
+--------------------------+------------------------------------------+
| Variable_name            | Value                                    |
+--------------------------+------------------------------------------+
| enforce_gtid_consistency | ON                                       |
| gtid_executed            | 94da1172-96b8-11eb-845a-000c291f647c:1-2 |
| gtid_mode                | ON                                       |
| gtid_owned               |                                          |
| gtid_purged              |                                          |
+--------------------------+------------------------------------------+
5 rows in set (0.00 sec)

mysql> create table t2(id int,username varchar(20));
Query OK, 0 rows affected (0.01 sec)

mysql> show global variables like '%gtid%';
+--------------------------+------------------------------------------+
| Variable_name            | Value                                    |
+--------------------------+------------------------------------------+
| enforce_gtid_consistency | ON                                       |
| gtid_executed            | 94da1172-96b8-11eb-845a-000c291f647c:1-3 |
| gtid_mode                | ON                                       |
| gtid_owned               |                                          |
| gtid_purged              |                                          |
+--------------------------+------------------------------------------+
5 rows in set (0.00 sec)

mysql> show slave hosts;
+-----------+--------+------+-----------+--------------------------------------+
| Server_id | Host   | Port | Master_id | Slave_UUID                           |
+-----------+--------+------+-----------+--------------------------------------+
|        11 | mysql1 | 3306 |         1 | 673611c2-9785-11eb-8991-000c29e19226 |
+-----------+--------+------+-----------+--------------------------------------+
1 row in set (0.00 sec)

Slave服务器状态信息

mysql> show global variables like '%gtid%';
+--------------------------+----------------------------------------+
| Variable_name            | Value                                  |
+--------------------------+----------------------------------------+
| enforce_gtid_consistency | ON                                     |
| gtid_executed            | 94da1172-96b8-11eb-845a-000c291f647c:1 |
| gtid_mode                | ON                                     |
| gtid_owned               |                                        |
| gtid_purged              |                                        |
+--------------------------+----------------------------------------+
5 rows in set (0.00 sec)

mysql> show global variables like '%uuid%';
+---------------+--------------------------------------+
| Variable_name | Value                                |
+---------------+--------------------------------------+
| server_uuid   | 673611c2-9785-11eb-8991-000c29e19226 |
+---------------+--------------------------------------+
1 row in set (0.00 sec)

mysql> show slave hosts;
Empty set (0.00 sec)

mysql> show slave status \\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.135
                  Master_User: repluser
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000005
          Read_Master_Log_Pos: 632
               Relay_Log_File: mysql2-relay-bin.000006
                Relay_Log_Pos: 844
        Relay_Master_Log_File: master-bin.000005
             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: 632
              Relay_Log_Space: 1261
              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: 94da1172-96b8-11eb-845a-000c291f647c
             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 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: 94da1172-96b8-11eb-845a-000c291f647c:1-3
            Executed_Gtid_Set: 94da1172-96b8-11eb-845a-000c291f647c:1-3
                Auto_Position: 1
1 row in set (0.00 sec)

启动mysql服务时错误提示1

[root@mysql1 etc]# service mysqld start
Starting MySQL... ERROR! The server quit without updating PID file (/data/mysql1.pid).

解决方法

1. 先判断是否对MySQL数据目录(本例为/data)有写权限
2. 通过查询/data目录下的mysql1.err错误日志根据日志报错,来进行判断,有可能是配置文件中参数配置有误造成的

以上是关于Linux学习-MySQL主从复制的主要内容,如果未能解决你的问题,请参考以下文章

Linux学习总结(五十一)mysql 主从配置

2018-05-16 Linux学习

Linux----------mysql主从复制和基于GTID主从复制

Linux学习-基于CentOS7的MariaDB数据库的主从复制

linux下mysql5.7数据库主从同步复制

linux笔记 第四十课 mysql主从复制