MySQL数据库的主从同步和读写分离

文章目录

  • MySQL数据库的主从同步和读写分离
    • 1.案例
    • 2.主从同步
      • 1.主从同步的类型
      • 2.主从复制的工作过程
      • 3.案例实施
    • 3.读写分离
      • 1.读写分离的原理
      • 2.案例实施

1.案例

企业拓扑图

1.在上述拓扑图中,后端MySQL数据库只有一台时,会有以下问题:

单点故障,导致服务不可用

无法处理大量的并发数据请求

数据丢失是大灾难

改造过后企业的拓扑图

2.改造的办法

增加MySQL数据库的服务器,对数据进行备份,形成主备

确保主备MySQL数据库服务器数据是一样的

主服务器宕机了,备份服务器继续工作,数据有保障

更高级的解决方案的拓扑图

重点:

1.我们需要主从同步账号

2.节点被调度的权限或者账号

3.代理账号

2.主从同步

1.主从同步的类型

1.基于语句的同步(默认)

在主服务器上执行的语句,从服务器执行同样的语句

2.基于行的同步

把改变的内容复制到从服务器

3.混合类型的复制

一旦发现基于语句无法精确复制时,就会采用基于行的复制

2.主从复制的工作过程

3.案例实施

1.配置MySQL master服务器

[root@localhost ~]# hostnamectl set-hostname master
[root@localhost ~]# su
//安装ntp,同步时间
[root@master ~]# yum -y install ntp
//配置ntp的主配置文件
[root@master ~]# vim /etc/ntp.conf
//本地时钟源
server 127.127.195.0
//设置时间层级为8
fudge 127.127.195.0 stratum 8
[root@master ~]# systemctl start ntpd
//关闭防火墙
[root@master ~]# systemctl stop firewalld
[root@master ~]# setenforce 0
//安装MySQL5.7
//安装MySQL的源码编译包
[root@master ~]# yum -y install ncurses ncurses-devel bison cmake gcc gcc-c++
//创建MySQL程序用户
[root@master ~]# useradd -s /sbin/nologin mysql
//将压缩包解压到/opt目录下面
[root@master ~]# tar -zxvf mysql-boost-5.7.20.tar.gz -C /opt
[root@master ~]# cd /opt/mysql-5.7.20/
//cmake 编译安装MySQL5.7
[root@master mysql-5.7.20]# cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1
[root@master mysql-5.7.20]# make
[root@master mysql-5.7.20]# make install
//将/usr/local/mysql目录下面的所有文件的属主和属组都给mysql
[root@master mysql-5.7.20]# chown -R mysql:mysql /usr/local/mysql/
//配置MySQL的my.cnf文件
[root@master mysql-5.7.20]# vi /etc/my.cnf
[client]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock[mysql]
port = 3306
default-character-set=utf8
socket = /usr/local/mysql/mysql.sock[mysqld]
user = mysql
basedir = /usr/local/mysql
datadir = /usr/local/mysql/data
port = 3306
character_set_server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket = /usr/local/mysql/mysql.sock
server-id = 1sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES//修改my.cnf的属主和属组
[root@master mysql-5.7.20]# chown mysql:mysql /etc/my.cnf
//配置mysql的环境变量
[root@master mysql-5.7.20]# echo 'PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH' >> /etc/profile
[root@master mysql-5.7.20]# echo 'export PATH' >> /etc/profile
//加载环境变量
[root@master mysql-5.7.20]# source /etc/profile
//初始化mysql数据库
[root@master mysql-5.7.20]# cd /usr/local/mysql/
[root@master mysql]# bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
[root@master mysql]# cp usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
[root@master mysql]# systemctl enable mysqld
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service to /usr/lib/systemd/system/mysqld.service.
[root@master mysql]# netstat -ntap | grep 3306
tcp6       0      0 :::3306                 :::*                    LISTEN      22547/mysqld
[root@master mysql]# vim /etc/my.cnf
server-id  = 11
log-bin=master-bin
log-slave-updates=true
[root@master mysql]# systemctl restart mysqld
[root@master ~]# mysql -uroot -pabc123
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 3
Server version: 5.7.20-log Source distributionCopyright (c) 2000, 2017, 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 'myslave'@'192.168.73.%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.01 sec)mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)mysql> show master status;
+-------------------+----------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+------------------+-------------------+
| master-bin.000001 |      604 |              |                  |                   |
+-------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)mysql> grant all on *.* to test@'192.168.73.%' identified by '123.com';
Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

2.配置MySQL从服务器slave1

//MySQL5.7的安装参照master的安装
[root@localhost mysql]# hostnamectl set-hostname slave1
[root@localhost mysql]# su
[root@slave1 mysql]# yum -y install ntp ntpdate
[root@slave1 mysql]# systemctl stop firewalld
[root@slave1 mysql]# setenforce 0
[root@slave1 mysql]# /usr/sbin/ntpdate 192.168.73.1419 Jan 11:16:04 ntpdate[5477]: the NTP socket is in use, exiting
[root@slave1 mysql]# vim /etc/my.cnf
server-id = 22
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
[root@slave1 mysql]# systemctl restart mysqld
[root@slave1 mysql]# mysql -u root -pabc123
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 3
Server version: 5.7.20 Source distributionCopyright (c) 2000, 2017, 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> change master to master_host='192.168.73.141',master_user='myslave',master_password='123456'',master_log_file='master-bin.000001',master_log_pos=604;
Query OK, 0 rows affected, 2 warnings (0.04 sec)mysql> start slave;
Query OK, 0 rows affected (0.00 sec)mysql> grant all on *.* to test@'192.168.73.%' identified by '123.com';
Query OK, 0 rows affected, 1 warning (0.01 sec)mysql> show slave status\G;
*************************** 1. row ***************************Slave_IO_State: Connecting to masterMaster_Host: 192.168.73.141Master_User: myslaveMaster_Port: 3306Connect_Retry: 60Master_Log_File: master-bin.000001Read_Master_Log_Pos: 604Relay_Log_File: relay-log-bin.000001Relay_Log_Pos: 4Relay_Master_Log_File: master-bin.000001Slave_IO_Running: ConnectingSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 604Relay_Log_Space: 154Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 1130Last_IO_Error: error connecting to master 'myslave@192.168.73.141:3306' - retry-time: 60  retries: 9Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0Master_UUID: Master_Info_File: /usr/local/mysql/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: 200109 11:29:56Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row in set (0.00 sec)mysql> grant all on *.* to test@'192.168.73.%' identified by '123.com';
Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

3.配置MySQL从服务器slave2

[root@localhost mysql]# hostnamectl set-hostname slave2
[root@localhost mysql]# su
[root@slave2 mysql]# yum -y install ntp ntpdate
[root@slave2 mysql]# systemctl start ntpd
[root@slave2 mysql]# systemctl stop firewalld
[root@slave2 mysql]# setenforce 0
[root@slave2 mysql]# /usr/sbin/ntpdate 192.168.73.1419 Jan 11:27:05 ntpdate[23734]: the NTP socket is in use, exiting
[root@slave2 mysql]# vim /etc/my.cnf
server-id = 23
relay-log=relay-log-bin
relay-log-index=slave-relay-bin.index
[root@slave2 mysql]# systemctl restart mysqld
[root@slave2 mysql]# mysql -uroot -pabc123
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 3
Server version: 5.7.20 Source distributionCopyright (c) 2000, 2017, 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> change master to master_host='192.168.73.141',master_user='myslave',master_password='123456',master_log_file='master-bin.000001',master_log_pos=604;
Query OK, 0 rows affected, 2 warnings (0.12 sec)mysql> start slave;
Query OK, 0 rows affected (0.01 sec)mysql> show slave status\G;
*************************** 1. row ***************************Slave_IO_State: Connecting to masterMaster_Host: 192.168.73.141Master_User: myslaveMaster_Port: 3306Connect_Retry: 60Master_Log_File: master-bin.000001Read_Master_Log_Pos: 604Relay_Log_File: relay-log-bin.000001Relay_Log_Pos: 4Relay_Master_Log_File: master-bin.000001Slave_IO_Running: ConnectingSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 604Relay_Log_Space: 154Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 1130Last_IO_Error: error connecting to master 'myslave@192.168.73.141:3306' - retry-time: 60  retries: 1Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 0Master_UUID: Master_Info_File: /usr/local/mysql/data/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: 200109 11:33:22Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row in set (0.00 sec)mysql> grant all on *.* to test@'192.168.73.%' identified by '123.com';
Query OK, 0 rows affected, 1 warning (0.00 sec)mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

4.测试主从同步

在master主机上

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.11 sec)mysql> create database school;
Query OK, 1 row affected (0.10 sec)mysql> use school;
Database changed
mysql> create table info (id int,name char(20));
Query OK, 0 rows affected (0.01 sec)mysql> insert into info (id,name) values(1,'1hao');
Query OK, 1 row affected (0.01 sec)mysql> insert into info (id,name) values(2,'2hao');
Query OK, 1 row affected (0.01 sec)mysql> select * from info;
+------+------+
| id   | name |
+------+------+
|    1 | 1hao |
|    2 | 2hao |
+------+------+
2 rows in set (0.00 sec)

在slave1主机上

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)mysql> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> select * from info;
+------+------+
| id   | name |
+------+------+
|    1 | 1hao |
|    2 | 2hao |
+------+------+
2 rows in set (0.00 sec)

在slave2主机上

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)mysql> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> select * from info;
+------+------+
| id   | name |
+------+------+
|    1 | 1hao |
|    2 | 2hao |
+------+------+
2 rows in set (0.00 sec)

3.读写分离

1.读写分离的原理

1.读写分离就是只在主服务器上写,只在从服务器上读

2.主数据库处理事务性查询,而从数据库处理select查询

3.数据库复制被用来把事务性查询导致的变更同步到集群中的从数据库

2.案例实施

1.搭建amoeba代理服务器

[root@localhost ~]# hostnamectl set-hostname amoeba
[root@localhost ~]# su
[root@amoeba ~]# ls
amoeba-mysql-binary-2.2.0.tar.gz  jdk-6u14-linux-x64.bin     模板  文档  桌面
anaconda-ks.cfg                   mysql-boost-5.7.20.tar.gz  视频  下载
initial-setup-ks.cfg              公共                       图片  音乐
[root@amoeba ~]# systemctl stop firewalld
[root@amoeba ~]# setenforce 0
[root@amoeba ~]# cp jdk-6u14-linux-x64.bin /usr/local
[root@amoeba ~]# cd /usr/local
[root@amoeba local]# ls
bin  etc  games  include  jdk-6u14-linux-x64.bin  lib  lib64  libexec  mysql  sbin  share  src
[root@amoeba local]# chmod +x jdk-6u14-linux-x64.bin
[root@amoeba local]# ./jdk-6u14-linux-x64.bin
Please enter "yes" or "no".
Do you agree to the above license terms? [yes or no]
yes
[root@amoeba local]# ls
bin  games    jdk1.6.0_14             lib    libexec  sbin   src
etc  include  jdk-6u14-linux-x64.bin  lib64  mysql    share
[root@amoeba local]# mv jdk1.6.0_14/ jdk1.6
[root@amoeba local]# ls
bin  games    jdk1.6                  lib    libexec  sbin   src
etc  include  jdk-6u14-linux-x64.bin  lib64  mysql    share
[root@amoeba local]# echo '
> export JAVA_HOME=/usr/local/jdk1.6
> export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
> export PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/bin:$PATH:$HOME/bin
> export AMOEBA_HOME=/usr/local/amoeba
> export PATH=$PATH:$AMOEBA_HOME/bin' >> /etc/profile
[root@amoeba local]# source /etc/profile
[root@amoeba local]# java -version
java version "1.6.0_14"
Java(TM) SE Runtime Environment (build 1.6.0_14-b08)
Java HotSpot(TM) 64-Bit Server VM (build 14.0-b16, mixed mode)
[root@amoeba local]# mkdir /usr/local/amoeba
[root@amoeba local]# cd
[root@amoeba ~]# tar -zxvf amoeba-mysql-binary-2.2.0.tar.gz -C /usr/local/amoeba
[root@amoeba ~]# chmod -R 755 /usr/local/amoeba
[root@amoeba ~]# /usr/local/amoeba/bin/amoeba
amoeba start|stop
[root@amoeba ~]# cd /usr/local/amoeba
[root@amoeba amoeba]# vim conf/amoeba.xml30                                         <property name="user">amoeba</property>31 32                                         <property name="password">123456</property>
117                 <property name="defaultPool">master</property>
118                 <property name="writePool">master</property>
119                 <property name="readPool">slaves</property>
[root@amoeba amoeba]# vim conf/amoeba.xml26                         <property name="user">test</property>27                         28                         29                         <property name="password">123.com</property>45         <dbServer name="user"  parent="abstractServer">46                 <factoryConfig>47                         <!-- mysql ip -->48                         <property name="ipAddress">192.168.73.141</property>49                 </factoryConfig>50         </dbServer>45         <dbServer name="master"  parent="abstractServer">46                 <factoryConfig>47                         <!-- mysql ip -->48                         <property name="ipAddress">192.168.73.141</property>49                 </factoryConfig>50         </dbServer>51 52         <dbServer name="slave1"  parent="abstractServer">53                 <factoryConfig>54                         <!-- mysql ip -->55                         <property name="ipAddress">192.168.73.138</property>56                 </factoryConfig>57         </dbServer>58         59         <dbServer name="slave2"  parent="abstractServer">60                 <factoryConfig>61                         <!-- mysql ip -->62                         <property name="ipAddress">192.168.73.142</property>63                 </factoryConfig>64         </dbServer>6566         <dbServer name="slaves" virtual="true">67                 <poolConfig class="com.meidusa.amoeba.server.MultipleServerPool">68                         <!-- Load balancing strategy: 1=ROUNDROBIN , 2=WEIGHTBASED , 3=HA-->69                         <property name="loadbalance">1</property>70 71                         <!-- Separated by commas,such as: server1,server2,server1 -->72                         <property name="poolNames">slave1,slave2</property>73                 </poolConfig>74         </dbServer>
[root@amoeba ~]# netstat -ntap | grep java
tcp6       0      0 :::8066                 :::*                    LISTEN      6097/java
tcp6       0      0 127.0.0.1:24038         :::*                    LISTEN      6097/java
tcp6      37      0 192.168.73.140:45008    192.168.73.141:3306     CLOSE_WAIT  5838/java
tcp6       0      0 192.168.73.140:42792    192.168.73.141:3306     ESTABLISHED 5975/java
tcp6       0      0 192.168.73.140:54100    192.168.73.142:3306     ESTABLISHED 6097/java
tcp6       0      0 192.168.73.140:35558    192.168.73.138:3306     ESTABLISHED 5975/java
tcp6       0      0 192.168.73.140:44924    192.168.73.141:3306     ESTABLISHED 6097/java
tcp6       0      0 192.168.73.140:51968    192.168.73.142:3306     ESTABLISHED 5975/java
tcp6       0      0 192.168.73.140:37690    192.168.73.138:3306     ESTABLISHED 6097/java

2.在客户机上面

[root@localhost ~]# hostnamectl set-hostname client
[root@localhost ~]# su
[root@client ~]# yum -y install mysql
[root@client ~]# systemctl stop firewalld
[root@client ~]# setenforce 0
[root@client ~]# mysql -u amoeba -p 123456 -h 192.168.73.140 -P8066
Enter password: \\密码123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1218932998
Server version: 5.1.45-mysql-amoeba-proxy-2.2.0Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.MySQL [(none)]> show databases;
ERROR 2006 (HY000): MySQL server has gone away
No connection. Trying to reconnect...
Connection id:    1218932998
Current database: *** NONE ***+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.01 sec)MySQL [(none)]> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
MySQL [school]> select * from info;
+------+------+
| id   | name |
+------+------+
|    1 | 1hao |
|    2 | 2hao |
+------+------+
2 rows in set (0.01 sec)

3.关闭slave的slave

//slave2
[root@slave1 ~]# mysql -uroot -pabc123
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 1534
Server version: 5.7.20 Source distributionCopyright (c) 2000, 2017, 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 (0.01 sec)mysql> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changedmysql> insert into info (id,name) values(4,'4hao');
Query OK, 1 row affected (0.01 sec)
//slave1
[root@slave1 ~]# mysql -uroot -pabc123
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 1534
Server version: 5.7.20 Source distributionCopyright (c) 2000, 2017, 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 (0.01 sec)mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.00 sec)mysql> use school;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> insert into info (id,name) values (3,'3hao');
Query OK, 1 row affected (0.00 sec)

4.在client里面测试

MySQL [school]> select * from info;
+------+------+
| id   | name |
+------+------+
|    1 | 1hao |
|    2 | 2hao |
|    4 | 4hao |
+------+------+
3 rows in set (0.02 sec)MySQL [school]> select * from info;
+------+------+
| id   | name |
+------+------+
|    1 | 1hao |
|    2 | 2hao |
|    3 | 3hao |
+------+------+
3 rows in set (0.01 sec)MySQL [school]> select * from info;
+------+------+
| id   | name |
+------+------+
|    1 | 1hao |
|    2 | 2hao |
|    4 | 4hao |
+------+------+
3 rows in set (0.00 sec)MySQL [school]> select * from info;
+------+------+
| id   | name |
+------+------+
|    1 | 1hao |
|    2 | 2hao |
|    3 | 3hao |
+------+------+
3 rows in set (0.00 sec)

从这个测试中,我们可以看出: MySQL对读请求做了负载均衡,对多次请求采用了轮询算法,所有的用于处理读请求的MySQL会轮流(或者按权重)来响应 。

MySQL数据库的主从同步和读写分离相关推荐

  1. mysql数据库的主从同步,实现读写分离

    目录 前言 1 分别在两台centos 7系统上安装mysql 5.7 2 master主服务器的配置 2.1 配置文件my.cnf的修改 2.2 创建从服务器的用户和权限 2.3 重启mysql服务 ...

  2. 【纯干货】Amoeba实现MySQL主从同步与读写分离

    [纯干货]Amoeba实现MySQL主从同步与读写分离 一.简介 amoeba简介 Amoeba(变形虫)项目,该开源框架于2008年开始发布一款 Amoeba for Mysql软件.这个软件致力于 ...

  3. 高性能高可用MySQL(主从同步,读写分离,分库分表,去中心化,虚拟IP,心跳机制)

    高性能高可用MySQL(主从同步,读写分离,分库分表,去中心化,虚拟IP,心跳机制) 视频地址:https://www.bilibili.com/video/BV1ry4y1v7Tr?p=8& ...

  4. mysql数据库的主从同步(主服务器存在内网IP)

    1 分别在两台服务器上安装系统和mysql数据库 主服务器WIN2008R2,主服务器虚拟机和从服务器上是centos 7系统,并在centos 7系统安装mysql 5.7 本文中的两台服务器的IP ...

  5. mysql读写分离错_MySQL主从同步、读写分离配置步骤、问题解决

    根据要求配置MySQL主从备份.读写分离,结合网上的文档,对搭建的步骤和出现的问题以及解决的过程做了如下笔记: 现在使用的两台服务器已经安装了MySQL,全是rpm包装的,能正常使用. 为了避免不必要 ...

  6. 基于Mysql主从同步的读写分离

    Mysql读写分离 mysql读写分离技术又称为mysql代理服务,通过在mysql主从同步的基础上增加一台代理服务器来实现将访问数据库时的读和写操作分配到不同服务器上来实现负载均衡,从而提高数据库访 ...

  7. Django中实现MySQL主从同步实现读写分离

    1 MySQL主从同步1. 主从同步机制 1.主从同步介绍和优点 在多台数据服务器中,分为主服务器和从服务器.一台主服务器对应多台从服务器. 主服务器只负责写入数据,从服务器只负责同步主服务器的数据, ...

  8. php解决mysql主从同步_Mysql读写分离,主从同步实现

    随着用户量的增多,数据库操作往往会成为一个系统的瓶颈所在,因此我们可以通过实现数据库的读写分离来提高系统的性能. 通过设置主从数据库实现读写分离,主库负责"写"操作,从库负责&qu ...

  9. mysql5.7主从同步与读写分离

    读写分离 Mysql主从复制的过程 读写分离原理 配置主从同步 主服务编译 部署第一台从服务器 部署第2台从服务器配置 配置读写分离关键服务 定义具体的读写分离对象 配置amoeba的主配置文件amo ...

  10. MySQL数据库设置主从同步

    2019独角兽企业重金招聘Python工程师标准>>> MySQL主从同步的机制: MySQL同步的流程大致如下:  1.主服务器(master)将变更事件(更新.删除.表结构改变等 ...

最新文章

  1. 如何设计日志采集系统?不妨看看这篇文章
  2. 麻省理工最新报告:完全无人驾驶仍需10年
  3. 正确设置语言,加速WP应用提交
  4. python一个月能学成嘛-python自我学习1--class 面向对象变成
  5. 《FPGA全程进阶---实战演练》第一章之如何学习FPGA
  6. 这才是实现分布式锁的正确姿势!
  7. boost::mp11::tuple_for_each相关用法的测试程序
  8. 白鹭 修改底层 egret.js 库后再次编译 成 新的库
  9. 华为oj----iNOC产品部-杨辉三角的变形 .
  10. oracle data guard方案,Oracle Data Guard 概念篇
  11. P3-weixin-2.0.1 版本发布,JAVA微信插件框架
  12. 华为手机安装GMS框架
  13. python谷歌翻译 频率限制_利用Google进行无长度限制的文本翻译(无需API,无需Money)...
  14. Windows和ubuntu互传文件
  15. 尺度不变特征转换(Scale-invariant feature transform 或 SIFT)
  16. 不评价别人的生活,是一个人最基本的修养 // 转
  17. 论文精读-基于双目图像的视差估计方法研究以及实现
  18. ChatGPT是什么?可以用来做什么?
  19. cordova for android hybrid 程序专题页面宽度大小显示异常问题
  20. 基于SSH开发校园失物招领网

热门文章

  1. 自动化鼠标和键盘操作pyautogui
  2. CH552-HID 键盘 鼠标
  3. Android Bmob后端云—数据库、服务器!
  4. How to learn a new technology
  5. c语言bcd错误数字还原,Windows10开机出现恢复界面且提示错误0xc0000034怎么办
  6. 中国城市资本流动问题探索
  7. SpringBoot笔记系列:(十)数据持久化Spring Data JPA
  8. JAVA计算机毕业设计政府会议管理系统Mybatis+系统+数据库+调试部署
  9. Openssl 编译
  10. C++primer——形参、局部变量和静态局部变量的差别