MySQL数据库的主从复制

MySQL数据库

MySQL是一种开放源代码的关系型数据库管理系统(RDBMS),使用最常用的数据库管理语言–结构化查询语言(SQL)进行数据库管理。
MySQL是开放源代码的,因此任何人都可以在General Public License的许可下下载并根据个性化的需要对其进行修改。
MySQL因为其速度、可靠性和适应性而备受关注。大多数人都认为在不需要事务化处理的情况下,MySQL是管理内容最好的选择。

MySQL的主从复制

环境

两台CentOS 7.4主机,安装MySQL-5.7.14
一台用户做Mysql主服务器,一台用于做Mysql从服务器,都在同一个网段中,配置好yum源、防火墙关闭、各节点时钟服务同步、各节点之间可以通过主机名互相通信

1.清空防火墙策略,关闭SELinux

[root@localhost ~]# iptables -F && setenforce 0
[root@localhost ~]# getenforce
Permissive

2.安装MySQL并启动
https://blog.csdn.net/QQ1006207580/article/details/89100508

配置

配置master主服务器

1.在/etc/my.cnf文件的 [mysql] 中添加两行代码,然后重启mysql

[root@localhost ~]# vi /etc/my.cnf
[mysqld]
......
log_bin=mysql-bin						#打开Mysql日志,日志格式为二进制
server-id=101							#配置server-id,让主服务器有唯一ID号
......

[root@localhost ~]# systemctl restart mysqld

2.创建复制帐号,具有replication slave 权限

[root@localhost ~]# mysql -uroot -pAdmin@123
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.14-log MySQL Community Server (GPL)

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

#创建复制用户,让其具有replication slave权限
mysql> grant replication slave on *.* to 'root'@'192.168.130.%' identified by 'Admin@123';
Query OK, 0 rows affected, 1 warning (0.00 sec)

# 设置读锁
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.01 sec)

# 得到binlog日志文件名和偏移量
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000001 |      603 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

# 备份要同步的数据库
[root@localhost ~]# mysqldump -uroot -p -A -B |gzip > /server/backup/mysql_bak.$(date +%F).sql.gz
Enter password:
# 主库备份数据长传到从库
[root@localhost ~]# scp /server/backup/mysql_bak.2019-04-10.sql.gz 192.168.130.103:/server/backup/
root@192.168.130.103's password:
mysql_bak.2019-04-10.sql.gz                          100%  202KB  38.0MB/s   00:00

mysql> unlock tables;
Query OK, 0 rows affected (0.01 sec)

还原主库备份数据

[root@localhost ~]# cd /server/backup/
[root@localhost backup]# ls
mysql_bak.2019-04-10.sql.gz
[root@localhost backup]# gzip -d mysql_bak.2019-04-10.sql.gz
[root@localhost backup]# mysql -uroot -p < mysql_bak.2019-04-10.sql
Enter password:
[root@localhost backup]# mysql -uroot -p -e 'show databases;'
Enter password:
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

配置slave从服务器

1.编辑my.cnf文件,添加server-id=109,重启数据库

[root@localhost ~]# vi /etc/my.cnf
[mysqld]
......
server-id=103
......

[root@localhost ~]# systemctl restart mysqld

2.配置从数据库

[root@localhost ~]# mysql -uroot -pAdmin@123                                           
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.14 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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='192.168.130.101',
    -> master_user='root',
    -> master_password='Admin@123',
    -> master_log_file='mysql-bin.000001',
    -> master_log_pos=603;
Query OK, 0 rows affected, 2 warnings (0.03 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: 192.168.130.101
                  Master_User: root
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 603
               Relay_Log_File: localhost-relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-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: 603
              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: 1130
                Last_IO_Error: error connecting to master 'root@192.168.130.101: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: /var/lib/mysql/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: 190410 17:17:05
     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

在从库检查slave时,Slave_IO_Running: Connecting,最后发现为防火墙的问题。

检查能否主动同步

1.主库创建一个数据库

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

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
+--------------------+
5 rows in set (0.00 sec)

2.从库检查

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
+--------------------+
5 rows in set (0.00 sec)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值