【MySQL】 MySQL主从复制

简介

MySQL作为世界上使用最为广泛的数据库之一,免费是其原因之一。但不可忽略的是它本身的功能的确很强大。随着技术的发展,在实际的生产环境中,由单台MySQL数据库服务器不能满足实际的需求。此时数据库集群就很好的解决了这个问题了。采用MySQL分布式集群,能够搭建一个高并发、负载均衡的集群服务器(这篇博客暂时不涉及)。在此之前我们必须要保证每台MySQL服务器里的数据同步。数据同步我们可以通过MySQL内部配置就可以轻松完成,主要有主从复制和主主复制。

环境

(1)主库(192.168.56.1):Win10 CMD Server version: 5.7.10-log MySQL Community Server (GPL) (绿色版)

(2)从库(192.168.56.101):Centos7 Server version: 5.7.17-log MySQL Community Server (GPL)

一、准备工作

已经安装好,并且清空两边数据

二、主库(win10)配置文件

server-id=167

log-bin=C:\Program Files\mysql-5.7.10-winx64\logs

log-bin-index=C:\Program Files\mysql-5.7.10-winx64\logs

三、从库(Linux)配置文件

server-id=168

log-bin=mysql-bin

auto_increment_increment=2

auto_increment_offset=2

四、在主库上创建一个账户,提供给从库连接(主库操作)

#主库: 192.168.56.1

#从库: 192.168.56.101

#用户:eddie

#密码:eddie

#授权:192.168.56.101

mysql> GRANT REPLICATION SLAVE ON *.* TO 'eddie'@'192.168.56.101' IDENTIFIED BY 'eddie';

Query OK, 0 rows affected, 1 warning (0.53 sec)

mysql> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.06 sec)

五、查询主库信息 (主库操作)

mysql> SHOW MASTER STATUS;

+-------------+----------+--------------+------------------+-------------------+

| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |

+-------------+----------+--------------+------------------+-------------------+

| logs.000007 | 154 | | | |

+-------------+----------+--------------+------------------+-------------------+

六、在从库配置主库配置的用户名密码和二进制文件位置

mysql> stop slave;

mysql> CHANGE MASTER TO MASTER_HOST='192.168.56.1', MASTER_USER='mysql101', MASTER_PASSWORD='mysql101',MASTER_LOG_FILE='logs.000007', MASTER_LOG_POS=154;

七、测试主从复制是否成功

mysql>SLAVE START;

mysql>SHOW SLAVE STATUS\G

# 当看到Slave_IO_Running: YES、Slave_SQL_Running: YES才表明状态正常!

八、对表操作

【=主库=】
#创建库
CREATE DATABASE abc;

#使用库
USE abc;

#创建表
CREATE TABLE tab1(id INT AUTO_INCREMENT,NAME VARCHAR(10),PRIMARY KEY(id));

#插入数据
mysql> insert into tab1 (name) value('11'),('11'),('11');
Query OK, 3 rows affected (0.10 sec)
Records: 3  Duplicates: 0  Warnings: 0

#查看库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| aa                 |
| abc                |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

#查看表
SHOW TABLES;
mysql> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| tab1          |
+---------------+
1 row in set (0.00 sec)

#表数据
mysql> select * from tab1;
+----+------+
| id | name |
+----+------+
|  1 | 12   |
+----+------+
1 row in set (0.00 sec)


【=从库=】

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

mysql> use abc;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> show tables;
+---------------+
| Tables_in_abc |
+---------------+
| tab1          |
+---------------+
1 row in set (0.00 sec)

mysql> select * from tab1;
+----+------+
| id | name |
+----+------+
|  1 | 12   |
+----+------+
1 row in set (0.00 sec)

备注: 本人在做这个的时候出现几个问题,就不一一贴出来,希望到动手查一下,或者留言!

相关推荐