mysql主从安装配置

1、mysql主从复制介绍

1、简介
         主从复制,主要是实现数据库的数据安全、提升IO性能和读写分离功能,建立一个和主数据库一致的数据库环境,称为从数据库,可以使用一台或多台服务器充当从数据库服务器,主服务器中的数据自动准实时复制到从服务器之中,从数据库可以是多级复制。
2、mysql复制的类型
1.STATEMENT模式(SBR)
每一条会修改数据的sql语句会记录到binlog中。优点是并不需要记录每一条sql语句和每一行的数据变化,减少了binlog日志量,节约IO,提高性能。缺点是在某些情况下会导致master-slave中的数据不一致(如sleep()函数, last_insert_id(),以及user-defined functions(udf)等会出现问题)

ROW模式(RBR)
不记录每条sql语句的上下文信息,仅需记录哪条数据被修改了,修改成什么样了。而且不会出现某些特定情况下的存储过程、或function、或trigger的调用和触发无法被正确复制的问题。缺点是会产生大量的日志,尤其是alter table的时候会让日志暴涨。

MIXED模式(MBR)
以上两种模式的混合使用,一般的复制使用STATEMENT模式保存binlog,对于STATEMENT模式无法复制的操作使用ROW模式保存binlog,MySQL会根据执行的SQL语句选择日志保存方式。

3、同步原理
1.Master 数据库只要发生变化,立马记录到Binary log 日志文件中
2.Slave数据库启动一个I/O thread连接Master数据库,请求Master变化的二进制日志
3.Slave I/O获取到的二进制日志,保存到自己的Relay log 日志文件中。
4.Slave 有一个 SQL thread定时检查Realy log是否变化,变化那么就更新数据

2、mysql安装

         在配置主从之前需要将主从的mysql数据库安装好,安装方式相同。
1、下载好安装包上传到服务器,并解压到mysql目录。

[root@host-10-253-234-32 soft]# tar -xvf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
[root@host-10-253-234-32 soft]# ls
mysql-5.7.31-linux-glibc2.12-x86_64  mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
[root@host-10-253-234-32 soft]#
[root@host-10-253-234-32 ~]# mv /soft/mysql-5.7.31-linux-glibc2.12-x86_64 /u01/mysql5.7

2、创建mysql用户,并授权。


[root@host-10-253-234-32 ~]# useradd -r -s /sbin/nologin mysql
chown -R mysql:mysql /u01/mysql5.7
[root@host-10-253-234-32 ~]# chown -R mysql:mysql /u01/mysql5.7
[root@host-10-253-234-32 mysql5.7]# 

3、配置my.cnf文件

vi /etc/my.cnf
[root@host-10-253-234-32 mysql5.7]# cp /etc/my.cnf /etc/my.cnf.bak
[root@host-10-253-234-32 mysql5.7]# vi /etc/my.cnf
[root@host-10-253-234-32 mysql5.7]# more /etc/my.cnf
[client]
socket=/u01/mysql5.7/mysql.sock
[mysqld]
user=mysql
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
max_allowed_packet=20M
max_connections=1000
lower_case_table_names=1
basedir=/u01/mysql5.7
datadir=/u01/mysql5.7/data
socket=/u01/mysql5.7/mysql.sock
character_set_server=utf8
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

#[mysqld_safe]
log-error=/u01/mysql5.7/log/mysql.err
pid-file=/u01/mysql5.7/mysql.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
[root@host-10-253-234-32 mysql5.7]# 

4、安装,并记住随机生成的密码,首次登陆需要用到

[root@host-10-253-234-32 mysql5.7]# 
[root@host-10-253-234-32 mysql5.7]# bin/mysqld --initialize --user=mysql --basedir=/u01/mysql5.7 --datadir=/u01/mysql5.7/data
2020-07-18T01:35:21.513029Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-07-18T01:35:21.720049Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-07-18T01:35:21.762184Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-07-18T01:35:21.821359Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: f1f4a61a-c896-11ea-8c77-fa163eae1a0e.
2020-07-18T01:35:21.822556Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-07-18T01:35:22.614278Z 0 [Warning] CA certificate ca.pem is self signed.
2020-07-18T01:35:22.924924Z 1 [Note] A temporary password is generated for root@localhost: Dc(hW5-o>JoN
[root@host-10-253-234-32 mysql5.7]# 

5、配置ssl加密bin/mysql_ssl_rsa_setup --datadir=/u01/mysql5.7
6、配置mysql启动服务

[root@host-10-253-234-32 mysql5.7]# ln -s /mysql/bin/mysql /usr/bin
[root@host-10-253-234-32 mysql5.7]# cp support-files/mysql.server /etc/init.d/mysql
[root@host-10-253-234-32 mysql5.7]# chkconfig --add mysql
[root@host-10-253-234-32 mysql5.7]# chkconfig mysql on
[root@host-10-253-234-32 mysql5.7]# chkconfig --list mysql

7、启动服务

/etc/init.d/mysql start
service start mysql  #linux 6启动命令 
systemctl start mysql  #linux 7启动命令 

8、使用自动生成的密码Dc(hW5-o>JoN登陆数据库,然后修改root密码及设置允许远程登陆,要不然会报错ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement

[root@host-10-253-234-32 ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 47966
Server version: 5.7.31-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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.

mysql> 
mysql> set password=password('password@1316');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all privileges on *.* to root@'%' identified by 'password@1316';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> 

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> 

3、配置主从同步

1)主库配置my.cnf,添加如下参数

#Master config
server_id=1
log_bin=mysql-bin
binlog_format=MIXED
binlog_ignore_db=information_schema,mysql,performance_schema,sys

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

#[mysqld_safe]
log-error=/u01/mysql5.7/log/mysql.err
pid-file=/u01/mysql5.7/mysql.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

2)配置主库

[[client]
socket=/u01/mysql5.7/mysql.sock
[mysqld]
user=mysql
sql_mode=STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
max_allowed_packet=20M
max_connections=1000
lower_case_table_names=1
basedir=/u01/mysql5.7
datadir=/u01/mysql5.7/data
socket=/u01/mysql5.7/mysql.sock
character_set_server=utf8

#Master config
server_id=1
log_bin=mysql-bin
binlog_format=MIXED
binlog_ignore_db=information_schema,mysql,performance_schema,sys

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

#[mysqld_safe]
log-error=/u01/mysql5.7/log/mysql.err
pid-file=/u01/mysql5.7/mysql.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

#设置日志超时时间
[root@host-10-253-234-32 ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 46967
Server version: 5.7.31-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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.
mysql> show variables like 'expire_logs_days';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| expire_logs_days | 0     |
+------------------+-------+
1 row in set (0.00 sec)

mysql>
mysql> set global expire_logs_days=15;
Query OK, 0 rows affected (0.00 sec)

mysql>  show variables like 'expire_logs_days';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| expire_logs_days | 15    |
+------------------+-------+
1 row in set (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> 

3)重启,主库创建同步用户slave

[root@host-10-253-234-32 openssh-8.2p1]# systemctl stop mysql
[root@host-10-253-234-32 openssh-8.2p1]# systemctl start mysql
[root@host-10-253-234-32 openssh-8.2p1]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 123
Server version: 5.7.31 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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.

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

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> 
mysql> set password=password('PASSWORD');
mysql> grant all privileges on *.* to root@'%' identified by 'PASSWORD';

4)配置从库my.cnf

[root@host-10-253-234-33 ~]# more /etc/my.cnf
[client]
socket=/u01/mysql5.7/mysql.sock
[mysqld]
user=mysql
basedir=/u01/mysql5.7
datadir=/u01/mysql5.7/data
socket=/u01/mysql5.7/mysql.sock
character_set_server=utf8
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

server_id=2
log_bin=mysql-bin
binlog_format=MIXED
relay_log_recovery=1

#[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
#pid-file=/var/run/mariadb/mariadb.pid
log-error=/u01/mysql5.7/log/mysql.err
pid-file=/u01/mysql5.7/mysql.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
mysql> set global expire_logs_days=15;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

mysql> show variables like 'expire_logs_days';
+------------------+-------+
| Variable_name    | Value |
+------------------+-------+
| expire_logs_days | 15    |
+------------------+-------+
1 row in set (0.00 sec)

mysql> 

5)同步主库数据
导出源库数据

[root@host-10-253-234-32 ~]# /u01/mysql5.7/bin/mysqldump -uroot -p cloudstore>/tmp/cloudstore.sql;
Enter password: 
[root@host-10-253-234-32 ~]# /u01/mysql5.7/bin/mysqldump -uroot -p gaassc>/tmp/gaassc.sql;
Enter password: 
[root@host-10-253-234-32 ~]# /u01/mysql5.7/bin/mysqldump -uroot -p intelligentsuperisiondb>/tmp/intelligentsuperisiondb.sql;
Enter password: 
[root@host-10-253-234-32 ~]# /u01/mysql5.7/bin/mysqldump -uroot -p szyzt>/tmp/szyzt.sql;                                    
Enter password: 
[root@host-10-253-234-32 ~]# /u01/mysql5.7/bin/mysqldump -uroot -p yhucmmg>/tmp/yhucmmg.sql;
Enter password: 
[root@host-10-253-234-32 ~]# /u01/mysql5.7/bin/mysqldump -uroot -p zsk>/tmp/zsk.sql;         
Enter password: 
[root@host-10-253-234-32 ~]#

6)将主库导出的数据文件复制到目标库

[root@host-10-253-234-32 tmp]# scp *.sql root@10.253.234.33:/tmp
root@10.253.234.33's password: 
cloudstore.sql                                                                                                                    100%  498KB  51.0MB/s   00:00    
szmzyl.sql                                                                                                                        100%  682MB 110.9MB/s   00:06    
szyzt.sql                                                                                                                         100%  548MB 109.8MB/s   00:04    
yhucmmg.sql                                                                                                                       100%   96MB 119.5MB/s   00:00    
zsk.sql                                                                                                                           100%  617MB 108.6MB/s   00:05

7、创建数据库,并将数导入目标库

[root@host-10-253-234-33 mysql5.7]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 5.7.31-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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.

mysql> create database cloudstore;
Query OK, 1 row affected (0.00 sec)

mysql> create database szmzyl;
Query OK, 1 row affected (0.00 sec)

mysql> create database szyzt;
Query OK, 1 row affected (0.01 sec)

mysql> create database yhucmmg;
Query OK, 1 row affected (0.00 sec)

mysql> create database zsk;
Query OK, 1 row affected (0.00 sec)

mysql> exit
Bye
[root@host-10-253-234-33 ~]# mysql -u root -p zsk < /tmp/zsk.sql;           
Enter password: 
[root@host-10-253-234-33 ~]# ls -l /tmp
-rw-r--r--. 1 root root    510339 730 22:26 /tmp/cloudstore.sql
-rw-r--r--. 1 root root 714921360 730 21:53 /tmp/szmzyl.sql
-rw-r--r--. 1 root root 574854723 730 21:53 /tmp/szyzt.sql
-rw-r--r--. 1 root root 100624706 730 21:53 /tmp/yhucmmg.sql
-rw-r--r--. 1 root root 647413712 730 21:53 /tmp/zsk.sql
[root@host-10-253-234-33 ~]# mysql -u root -p szyzt < /tmp/szyzt.sql; 
Enter password: 
[root@host-10-253-234-33 ~]# 

7)主库执行:show master status; 记下 Position 和 File 的值

mysql> show master status; 
+------------------+-----------+--------------+-------------------------------------------------+-------------------+
| File             | Position  | Binlog_Do_DB | Binlog_Ignore_DB                                | Executed_Gtid_Set |
+------------------+-----------+--------------+-------------------------------------------------+-------------------+
| mysql-bin.000001 | 833188576 |              | information_schema,mysql,performance_schema,sys |                   |
+------------------+-----------+--------------+-------------------------------------------------+-------------------+
1 row in set (0.00 sec)

8)配置从库同步配置

[root@host-10-253-234-33 ~]# systemctl stop mysql
[root@host-10-253-234-33 ~]# systemctl start mysql
[root@host-10-253-234-33 ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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.

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> change master to
    -> master_host='10.253.234.32',       #主库IP
    -> master_user='slave',               #主库同步用户       
    -> master_password='JY19MZslave',     #主库同步密码
    -> master_port=3306,
    -> master_log_file='mysql-bin.000001',  #7步查出来的log_file
    -> master_log_pos=833188576;            #7步查出来的log_pos
Query OK, 0 rows affected, 2 warnings (0.01 sec)

mysql> SHOW SLAVE STATUS;
+----------------+---------------+-------------+-------------+---------------+------------------+---------------------+-------------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+--------------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Slave_IO_State | Master_Host   | Master_User | Master_Port | Connect_Retry | Master_Log_File  | Read_Master_Log_Pos | Relay_Log_File                      | Relay_Log_Pos | Relay_Master_Log_File | Slave_IO_Running | Slave_SQL_Running | Replicate_Do_DB | Replicate_Ignore_DB | Replicate_Do_Table | Replicate_Ignore_Table | Replicate_Wild_Do_Table | Replicate_Wild_Ignore_Table | Last_Errno | Last_Error | Skip_Counter | Exec_Master_Log_Pos | Relay_Log_Space | Until_Condition | Until_Log_File | Until_Log_Pos | Master_SSL_Allowed | Master_SSL_CA_File | Master_SSL_CA_Path | Master_SSL_Cert | Master_SSL_Cipher | Master_SSL_Key | Seconds_Behind_Master | Master_SSL_Verify_Server_Cert | Last_IO_Errno | Last_IO_Error | Last_SQL_Errno | Last_SQL_Error | Replicate_Ignore_Server_Ids | Master_Server_Id | Master_UUID | Master_Info_File               | SQL_Delay | SQL_Remaining_Delay | Slave_SQL_Running_State | Master_Retry_Count | Master_Bind | Last_IO_Error_Timestamp | Last_SQL_Error_Timestamp | Master_SSL_Crl | Master_SSL_Crlpath | Retrieved_Gtid_Set | Executed_Gtid_Set | Auto_Position | Replicate_Rewrite_DB | Channel_Name | Master_TLS_Version |
+----------------+---------------+-------------+-------------+---------------+------------------+---------------------+-------------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+--------------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
|                | 10.253.234.32 | slave       |        3306 |            60 | mysql-bin.000001 |           833188576 | host-10-253-234-33-relay-bin.000001 |             4 | mysql-bin.000001      | No               | No                |                 |                     |                    |                        |                         |                             |          0 |            |            0 |           833188576 |             154 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                  NULL | No                            |             0 |               |              0 |                |                             |                0 |             | /u01/mysql5.7/data/master.info |         0 |                NULL |                         |              86400 |             |                         |                          |                |                    |                    |                   |             0 |                      |              |                    |
+----------------+---------------+-------------+-------------+---------------+------------------+---------------------+-------------------------------------+---------------+-----------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+-------------+--------------------------------+-----------+---------------------+-------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
1 row in set (0.00 sec)

mysql> stop slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

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

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Connecting to master
                  Master_Host: 10.253.234.32
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 833188576
               Relay_Log_File: host-10-253-234-33-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Connecting
            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: 833188576
              Relay_Log_Space: 154
              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: 1045
                Last_IO_Error: error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60  retries: 1
               Last_SQL_Errno: 0
               Last_SQL_Error: 
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 0
                  Master_UUID: 
             Master_Info_File: /u01/mysql5.7/data/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: 200719 11:52:30
     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: 
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 

9)连接异常检查,检查日志,登陆失败。

[root@host-10-253-234-33 log]# tail -f mysql.err     
2020-07-19T03:57:30.125046Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60  retries: 6, Error_code: 1045
2020-07-19T03:58:30.126947Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60  retries: 7, Error_code: 1045
2020-07-19T03:59:30.128985Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60  retries: 8, Error_code: 1045
2020-07-19T04:00:30.130947Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60  retries: 9, Error_code: 1045
2020-07-19T04:01:30.134093Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60  retries: 10, Error_code: 1045
2020-07-19T04:02:30.135890Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60  retries: 11, Error_code: 1045
2020-07-19T04:03:30.139051Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60  retries: 12, Error_code: 1045
2020-07-19T04:04:30.140825Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60  retries: 13, Error_code: 1045
2020-07-19T04:05:30.142791Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60  retries: 14, Error_code: 1045
2020-07-19T04:06:30.144744Z 3 [ERROR] Slave I/O for channel '': error connecting to master 'slave@10.253.234.32:3306' - retry-time: 60  retries: 15, Error_code: 1045
2020-07-19T04:07:30.146501Z 3 [Note] Slave I/O thread for channel '': connected to master 'slave@10.253.234.32:3306',replication started in log 'mysql-bin.000001' at position 833188576
登陆失败
[root@host-10-253-234-33 log]# mysql -u slave -h 10.253.234.32 -p
Enter password: 
ERROR 1045 (28000): Access denied for user 'slave'@'10.253.234.33' (using password: YES)

10)重新授权主库


mysql> grant replication slave on *.* to 'slave'@'%' identified by 'JY19MZslave';
ERROR 1223 (HY000): Can't execute the query because you have a conflicting read lock
mysql> unlock table;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql> grant replication slave on *.* to 'slave'@'%' identified by 'JY19MZslave';
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

mysql>

mysql> 

11)启动后同步报错,查看是FUNCTION报错,需要将FUNCTION导到目标库。

mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.253.234.32
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000007
          Read_Master_Log_Pos: 65236
               Relay_Log_File: host-10-253-234-33-relay-bin.000004
                Relay_Log_Pos: 137222651
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            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: 1305
                   Last_Error: Error 'FUNCTION yhucmmg.seq does not exist' on query. Default database: 'yhucmmg'. Query: 'SELECT `yhucmmg`.`seq`(_utf8'SEQ_FILEINFO' COLLATE 'utf8_general_ci')'
                 Skip_Counter: 0
          Exec_Master_Log_Pos: 970410907
              Relay_Log_Space: 7093622466
              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: 1305
               Last_SQL_Error: Error 'FUNCTION yhucmmg.seq does not exist' on query. Default database: 'yhucmmg'. Query: 'SELECT `yhucmmg`.`seq`(_utf8'SEQ_FILEINFO' COLLATE 'utf8_general_ci')'
  Replicate_Ignore_Server_Ids: 
             Master_Server_Id: 1
                  Master_UUID: f1f4a61a-c896-11ea-8c77-fa163eae1a0e
             Master_Info_File: /u01/mysql5.7/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: 200726 23:25:54
               Master_SSL_Crl: 
           Master_SSL_Crlpath: 
           Retrieved_Gtid_Set: 
            Executed_Gtid_Set: 
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql>

12)导出和导入函数

#主库导出
[root@host-10-253-234-32 tmp]# /u01/mysql5.7/bin/mysqldump -u root -p --databases szmzyl -ntdR --triggers=false > /tmp/szmzylf.sql
Enter password: 
[root@host-10-253-234-32 tmp]# /u01/mysql5.7/bin/mysqldump -u root -p --databases cloudstore -ntdR --triggers=false > /tmp/cloudstoref.sql
Enter password: 
[root@host-10-253-234-32 tmp]# /u01/mysql5.7/bin/mysqldump -u root -p --databases szyzt -ntdR --triggers=false > /tmp/szyztf.sql
Enter password: 
[root@host-10-253-234-32 tmp]# /u01/mysql5.7/bin/mysqldump -u root -p --databases yhucmmg -ntdR --triggers=false > /tmp/yhucmmgf.sql
Enter password: 
[root@host-10-253-234-32 tmp]# /u01/mysql5.7/bin/mysqldump -u root -p --databases zsk -ntdR --triggers=false > /tmp/zskf.sql
Enter password: 
[root@host-10-253-234-32 tmp]# scp *f.sql root@10.253.234.33:/tmp
root@10.253.234.33's password: 
cloudstoref.sql                                                                                                                   100% 2399     1.3MB/s   00:00    
szmzylf.sql                                                                                                                       100%   22KB  13.9MB/s   00:00    
szyztf.sql                                                                                                                        100% 1358     1.8MB/s   00:00    
yhucmmgf.sql                                                                                                                      100% 2384     3.1MB/s   00:00    
zskf.sql                                                                                                                          100% 2333     2.8MB/s   00:00    
[root@host-10-253-234-32 tmp]# 



#备库导入
[root@host-10-253-234-33 ~]# mysql -u root -p yhucmmg < /tmp/fyhucmmg.log;             
Enter password: 
ERROR 1418 (HY000) at line 37: This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
[root@host-10-253-234-33 ~]#
[root@host-10-253-234-33 ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 242
Server version: 5.7.31-log MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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.
mysql> show variables like 'log_bin_trust_function_creators';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| log_bin_trust_function_creators | OFF   |
+---------------------------------+-------+
1 row in set (0.00 sec)

mysql> 
mysql> set global log_bin_trust_function_creators=TRUE;
Query OK, 0 rows affected (0.00 sec)

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



#再次导入

[root@host-10-253-234-33 ~]# mysql -u root -p szmzyl  < /tmp/szmzylf.sql
Enter password: 
[root@host-10-253-234-33 ~]# mysql -u root -p szyzt  < /tmp/szyztf.sql 
Enter password: 
[root@host-10-253-234-33 ~]# mysql -u root -p yhucmmg  < /tmp/yhucmmgf.sql      
Enter password: 
[root@host-10-253-234-33 ~]# mysql -u root -p cloudstore  < /tmp/cloudstoref.sql           
Enter password: 
[root@host-10-253-234-33 ~]# mysql -u root -p zsk  < /tmp/zskf.sql 
Enter password: 
[root@host-10-253-234-33 ~]# 

11)再检查状态正常

mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.253.234.32
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000012
          Read_Master_Log_Pos: 576500579
               Relay_Log_File: host-10-253-234-33-relay-bin.000002
                Relay_Log_Pos: 9547
        Relay_Master_Log_File: mysql-bin.000012
             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: 576500579
              Relay_Log_Space: 9767
              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: f1f4a61a-c896-11ea-8c77-fa163eae1a0e
             Master_Info_File: /u01/mysql5.7/data/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: 
1 row in set (0.00 sec)

ERROR: 
No query specified

mysql> 

12)主库解锁,开始数据同步。

mysql> unlock table;
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

4、基它参考

START SLAVE; # 启动从库
STOP SLAVE; # 关闭从库

MASTER_LOG_FILE=‘mysql-bin.000001’,#与主库File 保持一致
MASTER_LOG_POS=833188576 , #与主库Position 保持一致

测试同步

主从同步延迟#
在主从同步过程中因为所有的SQL必须都要在从服务器里面执行一遍,但是主服务器如果不断的有更新操作源源不断的写入, 那么一旦有延迟产生, 那么延迟加重的可能性就会原来越大。虽然这个问题又不能完全解决,但是我们可以采取一些措施来缓解。

我们知道因为主服务器要负责更新操作, 他对安全性的要求比从服务器高, 所有有些设置可以修改,比如sync_binlog=1,innodb_flush_log_at_trx_commit = 1 之类的设置,而slave则不需要这么高的数据安全,完全可以讲sync_binlog设置为0或者关闭binlog,innodb_flushlog, innodb_flush_log_at_trx_commit 也 可以设置为0来提高sql的执行效率 这个能很大程度上提高效率。另外就是使用比主库更好的硬件设备作为slave。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值