mysql主从复制,读写分离

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/daily886/article/details/85124227

目录

安装mysql的教程 https://blog.csdn.net/daily886/article/details/84522981

在主服务器10.10.87.243上添加mysql用户

查看主服务器10.10.87.243状态

从服务器配置

回到主服务器10.10.87.243上,创建数据库 game

再来看看从服务器上,自动复制了game数据库,主从配置完毕

读写分离的逻辑


安装mysql的教程 https://blog.csdn.net/daily886/article/details/84522981

首先主服务器10.10.87.243上安装mysql , 配置 server-id=1

# 主服务器配置
server-id = 1

再到从服务器10.10.87.244上安装mysql , 配置 server-id=2

# 从服务器配置
server-id = 2

在主服务器10.10.87.243上添加mysql用户

# 登录mysql
[root@localhost ~]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.24-log MySQL Community Server (GPL)

#创建新用户
mysql>create user rep; 

#rep用户必须具有REPLICATION SLAVE权限,除此之外没有必要添加不必要的权限,密码为mysql。说明一下10.10.87.%,这个配置是指明repl用户所在服务器,这里%是通配符,表示10.10.87.0-10.10.87.255的Server都可以以repl用户登陆主服务器。当然你也可以指定固定Ip。

mysql> GRANT REPLICATION SLAVE ON *.* TO 'rep'@'10.10.87.%' IDENTIFIED BY 'mysql';

查看主服务器10.10.87.243状态

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000008 |      656 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

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

从服务器配置

# 登录mysql
[root@localhost ~]# mysql -h 10.10.87.243 -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3

# 连接Master
mysql> change master to master_host='10.10.87.243',
    -> master_port=3306,
    -> master_user='rep1',
    -> master_password='mysql',
    -> master_log_file='mysql-bin.000008',
    -> master_log_pos=0;

# 启动Slave
mysql> start slave;

# 查看数据库
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

回到主服务器10.10.87.243上,创建数据库 game

# 创建数据库
mysql> create database game charset=utf8;
Query OK, 1 row affected (0.00 sec)

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

再来看看从服务器上,自动复制了game数据库,主从配置完毕

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

读写分离的逻辑

读取操作,连接从服务器,进行读取操作

扫描二维码关注公众号,回复: 5019252 查看本文章

写入或者更新删除操作,连接主服务器,进行写入或者更新删除操作

猜你喜欢

转载自blog.csdn.net/daily886/article/details/85124227