本机windows下MySQL配置主从复制及MyCat实现读写分离测试


参考地址:
https://blog.csdn.net/u011325787/article/details/51422204
https://blog.csdn.net/u011325787/article/details/51423424
上面博文中的大大是linux下的,写的很好,可以参考哦。

主从复制部分

一、master配置

1.master的my.ini配置

由于本人下载的MySQL版本是5.7的zip包,所以初始化安装的时候,开始是没有下面的配置的,my.ini也是自己新建的。于是有了这步配置。

在[mysqld]下加入以下配置,开启主从配置初始化:

# replication
server_id=6
binlog-ignore-db=mysql
log-bin=master-mysql-bin
binlog_cache_size=1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062

参数说明:

  1. serverid 全局唯一的
  2. binlog-ignore-db=mysql 复制过滤,我们不同步mysql系统自带的数据库
  3. log-bin=master-mysql-bin 开启logbin功能并设置logbin文件的名称
  4. binlog_format=mixed 混合型复制模式,默认采用基于语句的复制,一旦发现基于语句的无法精确的复制时,就会采用基于行的复制。

修改后重启mysql:

net stop MySQL
net start MySQL

2.给root授予slave权限

打开navicat,使用命令行
执行:

grant replication slave on *.* to 'root'@'%' identified by 'root';

然后,刷新权限

flush privileges;

3.查看master状态

show master status;

如下图:
在这里插入图片描述
这两个,在配置slave时会用到。

4.查看同步状态

show processlist;

5.master全部命令执行过程

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

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

mysql> show master status;
+-------------------------+----------+--------------+------------------+-------------------+
| File                    | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------------+----------+--------------+------------------+-------------------+
| master-mysql-bin.000001 |     3649 |              | mysql            |                   |
+-------------------------+----------+--------------+------------------+-------------------+
1 row in set (0.05 sec)

mysql> show processlist;
+----+--------+-----------------+-------+-------------+------+---------------------------------------------------------------+------------------+
| Id | User   | Host            | db    | Command     | Time | State                                                         | Info             |
+----+--------+-----------------+-------+-------------+------+---------------------------------------------------------------+------------------+
|  2 | root   | localhost:62622 | han   | Sleep       |   16 |                                                               | NULL             |
|  3 | root   | localhost:62633 | han   | Sleep       |  185 |                                                               | NULL             |
|  4 | root   | localhost:62636 | NULL  | Query       |    0 | starting                                                      | show processlist |
|  5 | root   | localhost:62652 | han   | Sleep       |    6 |                                                               | NULL             |
|  6 | root   | localhost:62653 | han   | Sleep       |   36 |                                                               | NULL             |
|  7 | root   | localhost:62654 | han   | Sleep       |   46 |                                                               | NULL             |
|  8 | root   | localhost:62737 | han   | Sleep       |   66 |                                                               | NULL             |
|  9 | root   | localhost:62738 | han   | Sleep       |   86 |                                                               | NULL             |
| 10 | root   | localhost:62827 | han   | Sleep       |   56 |                                                               | NULL             |
| 11 | root   | localhost:62912 | han   | Sleep       |   26 |                                                               | NULL             |
| 13 | root   | localhost:62977 | han   | Sleep       | 3399 |                                                               | NULL             |
| 14 | root   | localhost:62984 | han   | Sleep       |   76 |                                                               | NULL             |
| 18 | root   | localhost:63356 | mysql | Sleep       | 2513 |                                                               | NULL             |
| 21 | master | localhost:63370 | han   | Sleep       | 2261 |                                                               | NULL             |
| 22 | master | localhost:63371 | NULL  | Sleep       | 2030 |                                                               | NULL             |
| 23 | master | localhost:63379 | han   | Sleep       | 2257 |                                                               | NULL             |
| 24 | root   | localhost:63923 | NULL  | Binlog Dump |  310 | Master has sent all binlog to slave; waiting for more updates | NULL             |
+----+--------+-----------------+-------+-------------+------+---------------------------------------------------------------+------------------+
17 rows in set (0.07 sec)

二、slave配置

1.slave的my.ini配置

在[mysqld]下加入以下配置,开启主从配置初始化:

# replication
server_id=5
binlog-ignore-db=mysql
log-bin=mysql-slave-bin
binlog_cache_size = 1M
binlog_format=mixed
expire_logs_days=7
slave_skip_errors=1062
relay_log=mysql-relay-bin
log_slave_updates=1
read_only=1

参数说明:

  1. log-bin=mysql-slave-bin 开启了二进制日志,实际上不开也没关系,因为我们这个slave只是做slave,如果你这台slave还有可能要做别人的master的话那么必须开启
  2. relay_log=mysql-relay-bin 配置中继日志,用来存放从master的bin-log那边同步来的数据

修改后重启mysql

net start MySQL2

2.连接master

mysql> change master to master_host='127.0.0.1',master_user='root',master_password='root',master_port=3306,master_log_file='master-mysql-bin.000001',master_log_pos=3649;
Query OK, 0 rows affected (0.53 sec)

参数说明:

  1. master_host=‘127.0.0.1’ master主机的ip地址
  2. master_user=‘root’
  3. master_password=‘root’ 我们刚刚在master有执行过授权的账号密码就是这个
  4. master_port=3306,master 数据库的端口号
  5. master_log_file=‘master-mysql-bin.000001’ 这个是我们通过show master status看到的file
  6. master_log_pos=3649 这个是我们通过show master status看到的position
  7. master_connect_retry=30 重试

3.查看状态

show slave status;

4.执行同步

start slave;

5.查看列表

show processlist;

6.停止主从复制

开始的时候,我看网上多数说可以新建个用户,于是我也尝试在master库新建了个master用户。结果,反正在windows系统下各种各样的问题,不推荐。。。

mysql> stop slave;
Query OK, 0 rows affected (0.05 sec)

mysql> reset slave all;
Query OK, 0 rows affected (0.21 sec)

mysql> show slave status;
Empty set

mysql>

7.slave全部命令执行过程

mysql> change master to master_host='127.0.0.1',master_user='root',master_password='root',master_port=3306,master_log_file='master-mysql-bin.000001',master_log_pos=3649;
Query OK, 0 rows affected (0.53 sec)
mysql> start slave;
Query OK, 0 rows affected (0.04 sec)

mysql> show processlist;
+----+-------------+-----------------+--------------------+---------+------+--------------------------------------------------------+------------------+
| Id | User        | Host            | db                 | Command | Time | State                                                  | Info             |
+----+-------------+-----------------+--------------------+---------+------+--------------------------------------------------------+------------------+
| 31 | root        | localhost:62892 | NULL               | Query   |    0 | starting                                               | show processlist |
| 58 | root        | localhost:62954 | NULL               | Sleep   | 1762 |                                                        | NULL             |
| 73 | root        | localhost:62990 | information_schema | Sleep   | 3284 |                                                        | NULL             |
| 87 | root        | localhost:63024 | han                | Sleep   |    9 |                                                        | NULL             |
| 88 | system user |                 | NULL               | Connect |   13 | Waiting for master to send event                       | NULL             |
| 89 | system user |                 | NULL               | Connect |   13 | Slave has read all relay log; waiting for more updates | NULL             |
+----+-------------+-----------------+--------------------+---------+------+--------------------------------------------------------+------------------+
6 rows in set (0.06 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 |
+----------------------------------+-------------+-------------+-------------+---------------+-------------------------+---------------------+------------------------+---------------+-------------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+------------------------------------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
| Waiting for master to send event | 127.0.0.1   | root        |        3306 |            60 | master-mysql-bin.000001 |                3649 | mysql-relay-bin.000002 |           327 | master-mysql-bin.000001 | Yes              | Yes               |                 |                     |                    |                        |                         |                             |          0 |            |            0 |                3649 |             534 | None            |                |             0 | No                 |                    |                    |                 |                   |                |                     0 | No                            |             0 |               |              0 |                |                             |                6 | af211e1e-3da4-11ea-86b1-04d9f536dc3f | F:\mysql\mysql2\mysql-5.7.29-winx64\data\master.info |         0 | NULL                | Slave has read all relay log; waiting for more updates |              86400 |             |                         |                          |                |                    |                    |                   |             0 |                      |              |                    |
+----------------------------------+-------------+-------------+-------------+---------------+-------------------------+---------------------+------------------------+---------------+-------------------------+------------------+-------------------+-----------------+---------------------+--------------------+------------------------+-------------------------+-----------------------------+------------+------------+--------------+---------------------+-----------------+-----------------+----------------+---------------+--------------------+--------------------+--------------------+-----------------+-------------------+----------------+-----------------------+-------------------------------+---------------+---------------+----------------+----------------+-----------------------------+------------------+--------------------------------------+------------------------------------------------------+-----------+---------------------+--------------------------------------------------------+--------------------+-------------+-------------------------+--------------------------+----------------+--------------------+--------------------+-------------------+---------------+----------------------+--------------+--------------------+
1 row in set (0.07 sec)

mysql> 

三、测试

通过navicat测试,在mysql1中新建数据库,mysql2也会新建数据库,mysql1中新建表,mysql2中也会新建表。
在这里插入图片描述

Mycat部分

一、下载

官网:http://www.mycat.io/
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、配置文件

conf/rule.xml
conf/schema.xml
conf/server.xml

1.配置server.xml

原始文件中,我们其他部分不看,只看user部分,只配置它。

<user name="administrator" defaultAccount="true">
		<property name="password">123456</property>
		<property name="schemas">TESTDB</property>
		<property name="defaultSchema">TESTDB</property>
		<!--No MyCAT Database selected 错误前会尝试使用该schema作为schema,不设置则为null,报错 -->
		
		<!-- 表级 DML 权限设置 -->
		<!-- 		
		<privileges check="false">
			<schema name="TESTDB" dml="0110" >
				<table name="tb01" dml="0000"></table>
				<table name="tb02" dml="1111"></table>
			</schema>
		</privileges>		
		 -->
</user>

2.配置schema.xml

原文件东西太多,于是我给删除简化了些。
我们要配置的位置有三部分:schema,dataNode,dataHost。

<?xml version="1.0"?>
<!DOCTYPE mycat:schema SYSTEM "schema.dtd">
<mycat:schema xmlns:mycat="http://io.mycat/">

	<schema name="TESTDB" checkSQLschema="true" sqlMaxLimit="100" dataNode="dn1">
		
	</schema>

	<dataNode name="dn1" dataHost="localhost1" database="han" />

	

	<dataHost name="localhost1" maxCon="1000" minCon="10" balance="1"
			  writeType="0" dbType="mysql" dbDriver="native" switchType="2"  slaveThreshold="100">
		<heartbeat>show slave status</heartbeat>
		
		<writeHost host="hostM1" url="localhost:3306" user="root"
				   password="root">
			<readHost host="hostS1" url="localhost:3307" user="root"
                   password="root" />
		</writeHost><!-- 注意这里master把slave库的配置包裹进去哦 -->
	
		
	</dataHost>

</mycat:schema>

重启mycat:

F:\mycat\mycat\bin>mycat stop
wrapper  | Stopping the Mycat-server service...
wrapper  | Mycat-server stopped.

F:\mycat\mycat\bin>mycat start
wrapper  | Starting the Mycat-server service...
wrapper  | Mycat-server started.

3.navicat连接逻辑库

在这里插入图片描述

4.效果

在这里插入图片描述

5.测试

对不起,太晚了,明天还得上班,有参数意思也都没写,抽空再完善吧。如果有需要的,下方留言,互相探讨,互相学习。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mycat 是一个开源的分布式数据库中间件,可以实现 MySQL读写分离、分库分表、数据分片等功能,也可以管理 MySQL主从复制。 具体来说,Mycat 可以通过以下几个步骤来管理 MySQL主从复制: 1. 在 Mycat配置文件中,配置 MySQL 数据源的主从关系。可以通过配置 `dataHost` 和 `dataNode` 来实现主从复制的管理。 2. 在 Mycat配置文件中,配置读写分离规则,以确保读操作访问从库,写操作访问主库。 3. 在 MySQL 的主库上创建一个用于复制的用户,并且给予该用户复制权限。例如,可以使用以下命令创建一个名为 `repl` 的用户: ``` CREATE USER 'repl'@'%' IDENTIFIED BY 'password'; GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%'; ``` 4. 在 MySQL 的主库上启用二进制日志,并且设置一个唯一的服务器 ID。例如,可以在 `my.cnf` 文件中添加以下配置: ``` server-id = 1 log-bin = mysql-bin ``` 5. 在 MySQL 的从库上配置复制参数,包括主库的连接信息、从库的服务器 ID、以及需要复制的二进制日志文件和位置。例如,可以使用以下命令配置从库: ``` CHANGE MASTER TO MASTER_HOST='master_host', MASTER_USER='repl', MASTER_PASSWORD='password', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=107; ``` 6. 在 Mycat 中启动主从复制功能,并且监控复制状态。可以使用 Mycat 的命令行工具或者 Web 界面来启动复制,以及查看复制状态。 以上是 Mycat 管理 MySQL 主从复制的基本步骤,具体实现方式可能因版本和配置不同而有所差异。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值