主从同步介绍、主从同步原理、主从同步结构、构建思路、配置一主一从、配置一主多从、读写分离介绍、工作原理、配置mycat服务、添加数据源、创建集群、指定主机角

news2024/10/7 18:28:33

Top

NSD DBA DAY07

  1. 案例1:MySQL一主一从
  2. 案例2:配置一主多从结构
  3. 案例3:数据读写分离

1 案例1:MySQL一主一从

1.1 问题

  1. 数据库服务器192.168.88.53配置为主数据库服务器
  2. 数据库服务器192.168.88.54配置为从数据库服务器
  3. 客户端192.168.88.50测试配置

1.2 方案

准备3台新的服务器,角色如表-1所示。

表-1

实验拓扑如图-1所示

图-1

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:数据库服务器192.168.88.53配置为主数据库服务器

1)启用binlog日志

 
  1. [root@mysql53 ~]# yum -y install mysql-server mysql
  2. [root@mysql53 ~]# systemctl start mysqld
  3. [root@mysql53 ~]# vim /etc/my.cnf.d/mysql-server.cnf
  4. [mysqld]
  5. server-id=53
  6. log-bin=mysql53
  7. :wq
  8. [root@mysql53 ~]# systemctl restart mysqld

2)用户授权

 
  1. [root@mysql53 ~]# mysql
  2. mysql> create user repluser@"%" identified by "123qqq...A";
  3. Query OK, 0 rows affected (0.11 sec)
  4. mysql> grant replication slave on *.* to repluser@"%";
  5. Query OK, 0 rows affected (0.09 sec)

3)查看日志信息

 
  1. mysql> show master status;
  2. +----------------+----------+--------------+------------------+-------------------+
  3. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  4. +----------------+----------+--------------+------------------+-------------------+
  5. | mysql53.000001 | 667 | | | |
  6. +----------------+----------+--------------+------------------+-------------------+
  7. 1 row in set (0.00 sec)

步骤二:数据库服务器192.168.88.54配置为从数据库服务

1)指定server-id 并重启数据库服务

 
  1. [root@mysql54 ~]# yum -y install mysql-server mysql
  2. [root@mysql54 ~]# systemctl start mysqld
  3. [root@mysql54 ~]# vim /etc/my.cnf.d/mysql-server.cnf
  4. [mysqld]
  5. server-id=54
  6. :wq
  7. [root@mysql54 ~]# systemctl restart mysqld

2)登陆服务指定主服务器信息

 
  1. [root@mysql54 ~]# mysql
  2. mysql> change master to master_host="192.168.88.53" , master_user="repluser" , master_password="123qqq...A" ,master_log_file="mysql53.000001" , master_log_pos=667;
  3. Query OK, 0 rows affected, 8 warnings (0.34 sec)
  4. mysql> start slave ; //启动slave进程
  5. Query OK, 0 rows affected, 1 warning (0.04 sec)
  6. mysql> show slave status \G //查看状态信息
  7. *************************** 1. row ***************************
  8. Slave_IO_State: Waiting for source to send event
  9. Master_Host: 192.168.88.53
  10. Master_User: repluser
  11. Master_Port: 3306
  12. Connect_Retry: 60
  13. Master_Log_File: mysql53.000001
  14. Read_Master_Log_Pos: 667
  15. Relay_Log_File: mysql54-relay-bin.000002
  16. Relay_Log_Pos: 322
  17. Relay_Master_Log_File: mysql53.000001
  18. Slave_IO_Running: Yes //IO线程
  19. Slave_SQL_Running: Yes //SQL线程
  20. Replicate_Do_DB:
  21. Replicate_Ignore_DB:
  22. Replicate_Do_Table:
  23. Replicate_Ignore_Table:
  24. Replicate_Wild_Do_Table:
  25. Replicate_Wild_Ignore_Table:
  26. Last_Errno: 0
  27. Last_Error:
  28. Skip_Counter: 0
  29. Exec_Master_Log_Pos: 667
  30. Relay_Log_Space: 533
  31. Until_Condition: None
  32. Until_Log_File:
  33. Until_Log_Pos: 0
  34. Master_SSL_Allowed: No
  35. Master_SSL_CA_File:
  36. Master_SSL_CA_Path:
  37. Master_SSL_Cert:
  38. Master_SSL_Cipher:
  39. Master_SSL_Key:
  40. Seconds_Behind_Master: 0
  41. Master_SSL_Verify_Server_Cert: No
  42. Last_IO_Errno: 0
  43. Last_IO_Error:
  44. Last_SQL_Errno: 0
  45. Last_SQL_Error:
  46. Replicate_Ignore_Server_Ids:
  47. Master_Server_Id: 53
  48. Master_UUID: 38c02165-005e-11ee-bd2d-525400007271
  49. Master_Info_File: mysql.slave_master_info
  50. SQL_Delay: 0
  51. SQL_Remaining_Delay: NULL
  52. Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
  53. Master_Retry_Count: 86400
  54. Master_Bind:
  55. Last_IO_Error_Timestamp:
  56. Last_SQL_Error_Timestamp:
  57. Master_SSL_Crl:
  58. Master_SSL_Crlpath:
  59. Retrieved_Gtid_Set:
  60. Executed_Gtid_Set:
  61. Auto_Position: 0
  62. Replicate_Rewrite_DB:
  63. Channel_Name:
  64. Master_TLS_Version:
  65. Master_public_key_path:
  66. Get_master_public_key: 0
  67. Network_Namespace:
  68. 1 row in set, 1 warning (0.00 sec)
  69. mysql>
  70. [zhangsan@localhost ~]$

步骤三:客户端192.168.88.50测试配置

1)在主服务器添加用户,给客户端连接使用

 
  1. [root@mysql53 ~]# mysql
  2. mysql> create user plj@"%" identified by "123456";
  3. Query OK, 0 rows affected (0.14 sec)
  4. mysql> grant all on gamedb.* to plj@"%" ;
  5. Query OK, 0 rows affected (0.12 sec)
  6. mysql>

2)客户端连接主服务器存储数据

 
  1. [root@mysql50 ~]# mysql -h192.168.88.53 -uplj -p123456
  2. mysql> create database gamedb;
  3. Query OK, 1 row affected (0.24 sec)
  4. mysql> create table gamedb.user(name char(10) , class char(3));
  5. Query OK, 0 rows affected (1.71 sec)
  6. mysql> insert into gamedb.user values ("yaya","nsd");
  7. Query OK, 1 row affected (0.14 sec)
  8. mysql> select * from gamedb.user;
  9. +------+-------+
  10. | name | class |
  11. +------+-------+
  12. | yaya | nsd |
  13. +------+-------+
  14. 1 row in set (0.01 sec)
  15. mysql>

3)客户端连接从服务器查看数据

-e 命令行下执行数据库命令

 
  1. [root@mysql50 ~]# mysql -h192.168.88.54 -uplj -p123456 –e ‘select * from gamedb.user’
  2. +------+-------+
  3. | name | class |
  4. +------+-------+
  5. | yaya | nsd |
  6. +------+-------+
  7. [root@mysql54 ~]#

2 案例2:配置一主多从结构

2.1 问题

1)基于案例1,把结构配置为一主多从结构

  • 配置192.168.88.55为192.168.88.53主机的从服务器
  • 客户端测试配置。

2.2 方案

准备新的服务器,要求如表-2所示。

表-2

实验拓扑如图-2所示。

图-2

 

2.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:配置192.168.88.55为192.168.88.53主机的从服务器

1)指定MySQL55主机的server-id 并重启数据库服务

 
  1. [root@mysql55 ~]# yum -y install mysql-server mysql
  2. [root@mysql55 ~]# systemctl start mysqld
  3. [root@mysql55 ~]# vim /etc/my.cnf.d/mysql-server.cnf
  4. [mysqld]
  5. server-id=55
  6. :wq
  7. [root@mysql55 ~]# systemctl restart mysqld

2)确保与主服务器数据一致。

 
  1. //在mysql53执行备份命令前查看日志名和偏移量 ,mysql55 在当前查看到的位置同步数据
  2. [root@mysql53 ~]# mysql -e 'show master status'
  3. +----------------+----------+--------------+------------------+-------------------+
  4. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  5. +----------------+----------+--------------+------------------+-------------------+
  6. | mysql53.000002 | 156 | | | |
  7. +----------------+----------+--------------+------------------+-------------------+
  8. [root@mysql53 ~]#
  9. //在主服务器存做完全备份
  10. [root@mysql53 ~]# mysqldump -B gamedb > /root/gamedb.sql
  11. //主服务器把备份文件拷贝给从服务器mysql55
  12. [root@mysql53 ~]# scp /root/gamedb.sql root@192.168.88.55:/root/
  13. [root@mysql55 ~]# mysql < /root/gamedb.sql

3)在MySQL55主机指定主服务器信息

 
  1. [root@mysql55 ~]# mysql
  2. mysql> change master to master_host="192.168.88.53" , master_user="repluser" , master_password="123qqq...A" , master_log_file="mysql53.000002" , master_log_pos=156;
  3. Query OK, 0 rows affected, 8 warnings (0.44 sec)
  4. 注意:日志名和偏移量 要写 在mysql53主机执行完全备份之前查看到的日志名和偏移量
  5. mysql> start slave; //启动slave进程
  6. Query OK, 0 rows affected, 1 warning (0.02 sec)
  7. //查看状态信息
  8. mysql> show slave status \G
  9. *************************** 1. row ***************************
  10. Slave_IO_State: Waiting for source to send event
  11. Master_Host: 192.168.88.53
  12. Master_User: repluser
  13. Master_Port: 3306
  14. Connect_Retry: 60
  15. Master_Log_File: mysql53.000002
  16. Read_Master_Log_Pos: 156
  17. Relay_Log_File: mysql55-relay-bin.000002
  18. Relay_Log_Pos: 322
  19. Relay_Master_Log_File: mysql53.000002
  20. Slave_IO_Running: Yes //正常
  21. Slave_SQL_Running: Yes //正常
  22. Replicate_Do_DB:
  23. Replicate_Ignore_DB:
  24. Replicate_Do_Table:
  25. Replicate_Ignore_Table:
  26. Replicate_Wild_Do_Table:
  27. Replicate_Wild_Ignore_Table:
  28. Last_Errno: 0
  29. Last_Error:
  30. Skip_Counter: 0
  31. Exec_Master_Log_Pos: 156
  32. Relay_Log_Space: 533
  33. Until_Condition: None
  34. Until_Log_File:
  35. Until_Log_Pos: 0
  36. Master_SSL_Allowed: No
  37. Master_SSL_CA_File:
  38. Master_SSL_CA_Path:
  39. Master_SSL_Cert:
  40. Master_SSL_Cipher:
  41. Master_SSL_Key:
  42. Seconds_Behind_Master: 0
  43. Master_SSL_Verify_Server_Cert: No
  44. Last_IO_Errno: 0
  45. Last_IO_Error:
  46. Last_SQL_Errno: 0
  47. Last_SQL_Error:
  48. Replicate_Ignore_Server_Ids:
  49. Master_Server_Id: 53
  50. Master_UUID: 38c02165-005e-11ee-bd2d-525400007271
  51. Master_Info_File: mysql.slave_master_info
  52. SQL_Delay: 0
  53. SQL_Remaining_Delay: NULL
  54. Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
  55. Master_Retry_Count: 86400
  56. Master_Bind:
  57. Last_IO_Error_Timestamp:
  58. Last_SQL_Error_Timestamp:
  59. Master_SSL_Crl:
  60. Master_SSL_Crlpath:
  61. Retrieved_Gtid_Set:
  62. Executed_Gtid_Set:
  63. Auto_Position: 0
  64. Replicate_Rewrite_DB:
  65. Channel_Name:
  66. Master_TLS_Version:
  67. Master_public_key_path:
  68. Get_master_public_key: 0
  69. Network_Namespace:
  70. 1 row in set, 1 warning (0.00 sec)
  71. //添加客户端访问时使用的用户
  72. mysql> create user plj@"%" identified by "123456";
  73. Query OK, 0 rows affected (0.08 sec)
  74. mysql> grant all on gamedb.* to plj@"%" ;
  75. Query OK, 0 rows affected (0.04 sec)
  76. Mysql>

步骤二:客户端测试配置

1)在client50 连接主服务器mysql53 存储数据

 
  1. //连接主服务器存储数据
  2. [root@mysql50 ~]# mysql -h192.168.88.53 -uplj -p123456
  3. mysql> insert into gamedb.user values("tt","aid");
  4. Query OK, 1 row affected (0.14 sec)
  5. mysql> insert into gamedb.user values("mm","uid");
  6. Query OK, 1 row affected (0.13 sec)

2)在client50 分别连接2个从服务器查看数据

 
  1. //连接从服务器54查看数据
  2. [root@mysql50 ~]# mysql -h192.168.88.54 -uplj -p123456 -e 'select * from gamedb.user'
  3. mysql: [Warning] Using a password on the command line interface can be insecure.
  4. +------+-------+
  5. | name | class |
  6. +------+-------+
  7. | yaya | nsd |
  8. | tt | aid |
  9. | mm | uid |
  10. +------+-------+
  11. //连接从服务器55查看数据
  12. [root@mysql50 ~]# mysql -h192.168.88.55 -uplj -p123456 -e 'select * from gamedb.user'
  13. mysql: [Warning] Using a password on the command line interface can be insecure.
  14. +------+-------+
  15. | name | class |
  16. +------+-------+
  17. | yaya | nsd |
  18. | tt | aid |
  19. | mm | uid |
  20. +------+-------+
  21. [root@mysql50 ~]#

3 案例3:数据读写分离

3.1 问题

  1. 搭建一主一从结构
  2. 配置MyCAT服务器
  3. 配置读写分离
  4. 测试配置

3.2 方案

准备新的虚拟机,如表-2所示

表-2

实验拓扑如图-2所示

图-2

 

3.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:搭建一主一从结构

因为数据的查询和存储分别访问不同的数据库服务器,所以要通过主从同步来保证负责读访问的服务与负责写访问的服务器数据一致。

1)配置主数据库服务器

 
  1. [root@mysql56 ~]# yum -y install mysql-server mysql
  2. [root@mysql56 ~]# systemctl start mysqld
  3. //启用binlog日志
  4. [root@mysql56 ~]# vim /etc/my.cnf.d/mysql-server.cnf
  5. [mysqld]
  6. server-id=56
  7. log-bin=mysql56
  8. :wq
  9. [root@mysql56 ~]# systemctl restart mysqld
  10. //用户授权
  11. [root@mysql56 ~]# mysql
  12. mysql> create user repluser@"%" identified by "123qqq...A";
  13. Query OK, 0 rows affected (0.11 sec)
  14. mysql> grant replication slave on *.* to repluser@"%" ;
  15. Query OK, 0 rows affected (0.08 sec)
  16. //查看日志信息
  17. mysql> show master status;
  18. +----------------+----------+--------------+------------------+-------------------+
  19. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  20. +----------------+----------+--------------+------------------+-------------------+
  21. | mysql56.000001 | 667 | | | |
  22. +----------------+----------+--------------+------------------+-------------------+
  23. 1 row in set (0.00 sec)

2)配置从数据库服务器

 
  1. //指定server-id 并重启数据库服务
  2. [root@mysql57 ~]# yum -y install mysql-server mysql
  3. [root@mysql57 ~]# systemctl start mysqld
  4. [root@mysql57 ~]# vim /etc/my.cnf.d/mysql-server.cnf
  5. [mysqld]
  6. server-id=57
  7. :wq
  8. [root@mysql57 ~]# systemctl restart mysqld
  9. //管理员登陆,指定主服务器信息
  10. [root@mysql57 ~]# mysql
  11. mysql> change master to master_host="192.168.88.56" , master_user="repluser" , master_password="123qqq...A" , master_log_file="mysql56.000001",master_log_pos=667;
  12. Query OK, 0 rows affected, 8 warnings (0.54 sec)
  13. //启动slave进程
  14. mysql> start slave;
  15. Query OK, 0 rows affected, 1 warning (0.03 sec)
  16. //查看状态信息
  17. mysql> show slave status \G
  18. *************************** 1. row ***************************
  19. Slave_IO_State: Waiting for source to send event
  20. Master_Host: 192.168.88.56
  21. Master_User: repluser
  22. Master_Port: 3306
  23. Connect_Retry: 60
  24. Master_Log_File: mysql56.000001
  25. Read_Master_Log_Pos: 667
  26. Relay_Log_File: mysql57-relay-bin.000002
  27. Relay_Log_Pos: 322
  28. Relay_Master_Log_File: mysql56.000001
  29. Slave_IO_Running: Yes //IO线程
  30. Slave_SQL_Running: Yes //SQL线程
  31. Replicate_Do_DB:
  32. Replicate_Ignore_DB:
  33. Replicate_Do_Table:
  34. Replicate_Ignore_Table:
  35. Replicate_Wild_Do_Table:
  36. Replicate_Wild_Ignore_Table:
  37. Last_Errno: 0
  38. Last_Error:
  39. Skip_Counter: 0
  40. Exec_Master_Log_Pos: 667
  41. Relay_Log_Space: 533
  42. Until_Condition: None
  43. Until_Log_File:
  44. Until_Log_Pos: 0
  45. Master_SSL_Allowed: No
  46. Master_SSL_CA_File:
  47. Master_SSL_CA_Path:
  48. Master_SSL_Cert:
  49. Master_SSL_Cipher:
  50. Master_SSL_Key:
  51. Seconds_Behind_Master: 0
  52. Master_SSL_Verify_Server_Cert: No
  53. Last_IO_Errno: 0
  54. Last_IO_Error:
  55. Last_SQL_Errno: 0
  56. Last_SQL_Error:
  57. Replicate_Ignore_Server_Ids:
  58. Master_Server_Id: 56
  59. Master_UUID: e0ab8dc4-0109-11ee-87e7-525400ad7ed3
  60. Master_Info_File: mysql.slave_master_info
  61. SQL_Delay: 0
  62. SQL_Remaining_Delay: NULL
  63. Slave_SQL_Running_State: Replica has read all relay log; waiting for more updates
  64. Master_Retry_Count: 86400
  65. Master_Bind:
  66. Last_IO_Error_Timestamp:
  67. Last_SQL_Error_Timestamp:
  68. Master_SSL_Crl:
  69. Master_SSL_Crlpath:
  70. Retrieved_Gtid_Set:
  71. Executed_Gtid_Set:
  72. Auto_Position: 0
  73. Replicate_Rewrite_DB:
  74. Channel_Name:
  75. Master_TLS_Version:
  76. Master_public_key_path:
  77. Get_master_public_key: 0
  78. Network_Namespace:
  79. 1 row in set, 1 warning (0.00 sec)

步骤二:配置mycat服务器

1)拷贝软件到mycat58主机

 
  1. [root@server1 ~]# scp /linux-soft/s3/mycat2-1.21-release-jar-with-dependencies.jar root@192.168.88.58:/root/
  2. [root@server1 ~]# scp /linux-soft/s3/mycat2-install-template-1.21.zip root@192.168.88.58:/root/

2)安装mycat软件

 
  1. //安装jdk
  2. [root@mycat58 upload]# yum -y install java-1.8.0-openjdk.x86_64
  3. //安装解压命令
  4. [root@mycat58 upload]# which unzip || yum -y install unzip
  5. //安装mycat
  6. [root@mycat58 upload]# unzip mycat2-install-template-1.21.zip
  7. [root@mycat58 upload]# mv mycat /usr/local/
  8. //安装依赖
  9. [root@mycat58 upload]# cp mycat2-1.21-release-jar-with-dependencies.jar /usr/local/mycat/lib/
  10. //修改权限
  11. [root@mycat58 upload]# chmod -R 777 /usr/local/mycat/

3)定义客户端连接mycat服务使用用户及密码:

 
  1. [root@mycat58 ~]# vim /usr/local/mycat/conf/users/root.user.json
  2. {
  3. "dialect":"mysql",
  4. "ip":null,
  5. "password":"654321", 密码
  6. "transactionType":"proxy",
  7. "username":"mycat" 用户名
  8. }
  9. :wq
  1. 定义连接的数据库服务器
 
  1. [root@mycat58 ~]# vim /usr/local/mycat/conf/datasources/prototypeDs.data
  2. {
  3. "dbType":"mysql",
  4. "idleTimeout":60000,
  5. "initSqls":[],
  6. "initSqlsGetConnection":true,
  7. "instanceType":"READ_WRITE",
  8. "maxCon":1000,
  9. "maxConnectTimeout":3000,
  10. "maxRetryCount":5,
  11. "minCon":1,
  12. "name":"prototypeDs",
  13. "password":"123456", 密码
  14. "type":"JDBC",
  15. "url":"jdbc:mysql://localhost:3306/mysql?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8", 连接本机的数据库服务
  16. "user":"plj", 用户名
  17. "weight":0
  18. }
  19. :wq

5)在mycat58主机运行数据库服务

 
  1. [root@mycat58 ~]# yum -y install mysql-server mysql 安装软件
  2. [root@mycat58 ~]# systemctl start mysqld 启动服务
  3. [root@mycat58 ~]# mysql 连接服务
  4. mysql> create user plj@"%" identified by "123456"; 创建plj用户
  5. Query OK, 0 rows affected (0.05 sec)
  6. mysql> grant all on *.* to plj@"%" ; 授予权限
  7. Query OK, 0 rows affected (0.39 sec)
  8. mysql> exit 断开连接
  9. Bye
  10. [root@mycat58 ~]#

6)启动mycat服务

 
  1. [root@mycat58 ~]# /usr/local/mycat/bin/mycat help
  2. Usage: /usr/local/mycat/bin/mycat { console | start | stop | restart | status | dump }
  3. [root@mycat58 ~]# /usr/local/mycat/bin/mycat start
  4. Starting mycat2...
  5. //半分钟左右 能看到端口
  6. [root@mycat58 ~]# netstat -utnlp | grep 8066
  7. tcp6 0 0 :::8066 :::* LISTEN 57015/java
  8. [root@mycat58 ~]#

7) 连接mycat服务

 
  1. [root@mycat58 ~]# mysql -h127.0.0.1 -P8066 -umycat -p654321
  2. mysql> show databases;
  3. +--------------------+
  4. | `Database` |
  5. +--------------------+
  6. | information_schema |
  7. | mysql |
  8. | performance_schema |
  9. +--------------------+
  10. 3 rows in set (0.11 sec)
  11. Mysql>

步骤三:配置读写分离

1)添加数据源:连接mycat服务后做如下操作

 
  1. //连接mycat服务
  2. [root@mycat58 ~]# mysql -h127.0.0.1 -P8066 -umycat -p654321
  3. //添加mysql56数据库服务器
  4. MySQL> /*+ mycat:createdatasource{
  5. "name":"whost56", "url":"jdbc:mysql://192.168.88.56:3306","user":"plja","password":"123456"}*/;
  6. Query OK, 0 rows affected (0.25 sec)
  7. //添加mysql57数据库服务器
  8. Mysql>/*+ mycat:createdatasource{
  9. "name":"rhost57", "url":"jdbc:mysql://192.168.88.57:3306","user":"plja","password":"123456"}*/;
  10. //查看数据源
  11. mysql> /*+mycat:showDataSources{}*/ \G
  12. *************************** 1. row ***************************
  13. NAME: whost56
  14. USERNAME: plja
  15. PASSWORD: 123456
  16. MAX_CON: 1000
  17. MIN_CON: 1
  18. EXIST_CON: 0
  19. USE_CON: 0
  20. MAX_RETRY_COUNT: 5
  21. MAX_CONNECT_TIMEOUT: 30000
  22. DB_TYPE: mysql
  23. URL: jdbc:mysql://192.168.88.56:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
  24. WEIGHT: 0
  25. INIT_SQL:
  26. INIT_SQL_GET_CONNECTION: true
  27. INSTANCE_TYPE: READ_WRITE
  28. IDLE_TIMEOUT: 60000
  29. DRIVER: {
  30. CreateTime:"2023-06-02 17:01:14",
  31. ActiveCount:0,
  32. PoolingCount:0,
  33. CreateCount:0,
  34. DestroyCount:0,
  35. CloseCount:0,
  36. ConnectCount:0,
  37. Connections:[
  38. ]
  39. }
  40. TYPE: JDBC
  41. IS_MYSQL: true
  42. *************************** 2. row ***************************
  43. NAME: rhost57
  44. USERNAME: plja
  45. PASSWORD: 123456
  46. MAX_CON: 1000
  47. MIN_CON: 1
  48. EXIST_CON: 0
  49. USE_CON: 0
  50. MAX_RETRY_COUNT: 5
  51. MAX_CONNECT_TIMEOUT: 30000
  52. DB_TYPE: mysql
  53. URL: jdbc:mysql://192.168.88.57:3306?useUnicode=true&serverTimezone=Asia/Shanghai&characterEncoding=UTF-8&autoReconnect=true
  54. WEIGHT: 0
  55. INIT_SQL:
  56. INIT_SQL_GET_CONNECTION: true
  57. INSTANCE_TYPE: READ_WRITE
  58. IDLE_TIMEOUT: 60000
  59. DRIVER: {
  60. CreateTime:"2023-06-02 17:01:14",
  61. ActiveCount:0,
  62. PoolingCount:0,
  63. CreateCount:0,
  64. DestroyCount:0,
  65. CloseCount:0,
  66. ConnectCount:0,
  67. Connections:[
  68. ]
  69. }
  70. TYPE: JDBC
  71. IS_MYSQL: true
  72. *************************** 3. row ***************************
  73. NAME: prototypeDs
  74. USERNAME: plj
  75. PASSWORD: 123456
  76. MAX_CON: 1000
  77. MIN_CON: 1
  78. EXIST_CON: 0
  79. USE_CON: 0
  80. MAX_RETRY_COUNT: 5
  81. MAX_CONNECT_TIMEOUT: 3000
  82. DB_TYPE: mysql
  83. URL: jdbc:mysql://localhost:3306/mysql?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
  84. WEIGHT: 0
  85. INIT_SQL:
  86. INIT_SQL_GET_CONNECTION: true
  87. INSTANCE_TYPE: READ_WRITE
  88. IDLE_TIMEOUT: 60000
  89. DRIVER: {
  90. CreateTime:"2023-06-02 17:01:14",
  91. ActiveCount:0,
  92. PoolingCount:0,
  93. CreateCount:0,
  94. DestroyCount:0,
  95. CloseCount:0,
  96. ConnectCount:0,
  97. Connections:[
  98. ]
  99. }
  100. TYPE: JDBC
  101. IS_MYSQL: true
  102. 3 rows in set (0.00 sec)
  103. mysql>
  104. //添加的数据源以文件的形式保存在安装目录下
  105. [root@mycat58 conf]# ls /usr/local/mycat/conf/datasources/
  106. prototypeDs.datasource.json rhost57.datasource.json whost56.datasource.json
  107. [root@mycat58 conf]#

2)配置数据库服务器添加plja用户

 
  1. //在master服务器添加
  2. [root@mysql56 ~]# mysql
  3. mysql> create user plja@"%" identified by "123456";
  4. Query OK, 0 rows affected (0.06 sec)
  5. mysql> grant all on *.* to plja@"%";
  6. Query OK, 0 rows affected (0.03 sec)
  7. mysql>exit
  8. [root@mysql56 ~]#
  9. //在slave服务器查看是否同步成功
  10. [root@mysql57 ~]# mysql -e 'select user , host from mysql.user where user="plja"'
  11. +------+------+
  12. | user | host |
  13. +------+------+
  14. | plja | % |
  15. +------+------+
  16. [root@mysql57 ~]#

3)创建集群,连接mycat服务后做如下配置:

 
  1. [root@mycat58 ~]# mysql -h127.0.0.1 -P8066 -umycat -p654321
  2. //创建集群
  3. mysql>/*!mycat:createcluster{
  4. "name":"rwcluster",
  5. "masters":["whost56"],
  6. "replicas":["rhost57"]
  7. }*/ ;
  8. Mysql>
  9. //查看集群信息
  10. mysql> /*+ mycat:showClusters{}*/ \G
  11. *************************** 1. row ***************************
  12. NAME: rwcluster
  13. SWITCH_TYPE: SWITCH
  14. MAX_REQUEST_COUNT: 2000
  15. TYPE: BALANCE_ALL
  16. WRITE_DS: whost56
  17. READ_DS: whost56,rhost57
  18. WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
  19. READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
  20. AVAILABLE: true
  21. *************************** 2. row ***************************
  22. NAME: prototype
  23. SWITCH_TYPE: SWITCH
  24. MAX_REQUEST_COUNT: 200
  25. TYPE: BALANCE_ALL
  26. WRITE_DS: prototypeDs
  27. READ_DS: prototypeDs
  28. WRITE_L: io.mycat.plug.loadBalance.BalanceRandom$1
  29. READ_L: io.mycat.plug.loadBalance.BalanceRandom$1
  30. AVAILABLE: true
  31. 2 rows in set (0.00 sec)
  32. mysql>
  33. //创建的集群以文件的形式保存在目录下
  34. [root@mycat58 conf]# ls /usr/local/mycat/conf/clusters/
  35. prototype.cluster.json rwcluster.cluster.json
  36. [root@mycat58 conf]#

4)指定主机角色

 
  1. //修改master角色主机仅负责写访问
  2. [root@mycat58 ~]# vim /usr/local/mycat/conf/datasources/whost56.datasource.json
  3. {
  4. "dbType":"mysql",
  5. "idleTimeout":60000,
  6. "initSqls":[],
  7. "initSqlsGetConnection":true,
  8. "instanceType":"WRITE", 仅负责写访问
  9. "logAbandoned":true,
  10. "maxCon":1000,
  11. "maxConnectTimeout":30000,
  12. "maxRetryCount":5,
  13. "minCon":1,
  14. "name":"whost56",
  15. "password":"123456",
  16. "queryTimeout":0,
  17. "removeAbandoned":false,
  18. "removeAbandonedTimeoutSecond":180,
  19. "type":"JDBC",
  20. "url":"jdbc:mysql://192.168.88.56:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true",
  21. "user":"plja",
  22. "weight":0
  23. }
  24. :wq
  25. //修改slave角色主机仅负责读访问
  26. [root@mycat58 ~]# vim /usr/local/mycat/conf/datasources/rhost57.datasource.json
  27. {
  28. "dbType":"mysql",
  29. "idleTimeout":60000,
  30. "initSqls":[],
  31. "initSqlsGetConnection":true,
  32. "instanceType":"READ",仅负责读访问
  33. "logAbandoned":true,
  34. "maxCon":1000,
  35. "maxConnectTimeout":30000,
  36. "maxRetryCount":5,
  37. "minCon":1,
  38. "name":"rhost57",
  39. "password":"123456",
  40. "queryTimeout":0,
  41. "removeAbandoned":false,
  42. "removeAbandonedTimeoutSecond":180,
  43. "type":"JDBC",
  44. "url":"jdbc:mysql://192.168.88.57:3306?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&autoReconnect=true",
  45. "user":"plja",
  46. "weight":0
  47. }
  48. :wq

5)修改读策略

 
  1. [root@mycat58 ~]# vim /usr/local/mycat/conf/clusters/rwcluster.cluster.json
  2. {
  3. "clusterType":"MASTER_SLAVE",
  4. "heartbeat":{
  5. "heartbeatTimeout":1000,
  6. "maxRetryCount":3,
  7. "minSwitchTimeInterval":300,
  8. "showLog":false,
  9. "slaveThreshold":0.0
  10. },
  11. "masters":[
  12. "whost56"
  13. ],
  14. "maxCon":2000,
  15. "name":"rwcluster",
  16. "readBalanceType":"BALANCE_ALL_READ",
  17. "replicas":[
  18. "rhost57"
  19. ],
  20. "switchType":"SWITCH"
  21. }
  22. :wq
  23. //重启mycat服务
  24. [root@mycat58 ~]# /usr/local/mycat/bin/mycat restart
  25. Stopping mycat2...
  26. Stopped mycat2.
  27. Starting mycat2...
  28. [root@mycat58 ~]#

步骤四:测试配置

思路如下:

  1. 连接mycat服务建库
  2. 指定存储数据使用的集群
  3. 连接mycat服务建表
  4. 客户端连接mycat服务执行select 或 insert

具体操作如下:

 
  1. [root@mycat58 ~]# mysql -h127.0.0.1 -P8066 -umycat -p654321
  2. mysql> create database testdb;
  3. Query OK, 0 rows affected (0.30 sec)
  4. mysql> exit
  5. Bye
  6. //指定testdb库存储数据使用的集群
  7. [root@mycat58 ~]# vim /usr/local/mycat/conf/schemas/testdb.schema.json
  8. {
  9. "customTables":{},
  10. "globalTables":{},
  11. "normalProcedures":{},
  12. "normalTables":{},
  13. "schemaName":"testdb",
  14. "targetName":"rwcluster", 添加此行,之前创建的集群名rwcluster
  15. "shardingTables":{},
  16. "views":{}
  17. }
  18. :wq
  19. [root@mycat58 ~]# /usr/local/mycat/bin/mycat restart
  20. Stopping mycat2...
  21. Stopped mycat2.
  22. Starting mycat2...
  23. [root@mycat58 ~]#
  24. //连接mycat服务建表插入记录
  25. [root@client50 ~]# mysql -h192.168.88.58 -P8066 -umycat -p654321
  26. mysql> create table testdb.user (name varchar(10) , password varchar(10));
  27. Query OK, 0 rows affected (0.45 sec)
  28. mysql> insert into testdb.user values("yaya","123456");
  29. Query OK, 1 row affected (0.20 sec)
  30. mysql> select * from testdb.user;
  31. +------+----------+
  32. | name | password |
  33. +------+----------+
  34. | yaya | 123456 |
  35. +------+----------+
  36. 1 row in set (0.01 sec)
  37. mysql>

测试读写分离

 
  1. //在从服务器本机插入记录,数据仅在从服务器有,主服务器没有
  2. [root@mysql57 ~]# mysql -e 'insert into testdb.user values ("yayaA","654321")'
  3. [root@mysql57 ~]# mysql -e 'select * from testdb.user'
  4. +-------+----------+
  5. | name | password |
  6. +-------+----------+
  7. | yaya | 123456 |
  8. | yayaA | 654321 |
  9. +-------+----------+
  10. [root@mysql57 ~]#
  11. //主服务器数据不变,日志偏移量不不变
  12. [root@mysql56 ~]# mysql -e 'select * from testdb.user'
  13. +------+----------+
  14. | name | password |
  15. +------+----------+
  16. | yaya | 123456 |
  17. +------+----------+
  18. [root@mysql56 ~]# mysql -e 'show master status'
  19. +----------------+----------+--------------+------------------+-------------------+
  20. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  21. +----------------+----------+--------------+------------------+-------------------+
  22. | mysql56.000002 | 4514 | | | |
  23. +----------------+----------+--------------+------------------+-------------------+
  24. [root@mysql56 ~]#
  25. //客户端连接mycat服务读/写数据
  26. [root@client50 ~]# mysql -h192.168.88.58 -P8066 -umycat -p654321
  27. mysql> select * from testdb.user; 查看到的是2条记录的行
  28. +-------+----------+
  29. | name | password |
  30. +-------+----------+
  31. | yaya | 123456 |
  32. | yayaA | 654321 |
  33. +-------+----------+
  34. 2 rows in set (0.04 sec)
  35. mysql> insert into testdb.user values("yayaB","123456"); 插入记录
  36. Query OK, 1 row affected (0.06 sec)
  37. mysql> select * from testdb.user;
  38. +-------+----------+
  39. | name | password |
  40. +-------+----------+
  41. | yaya | 123456 |
  42. | yayaB | 123456 |
  43. +-------+----------+
  44. 2 rows in set (0.01 sec)
  45. mysql>
  46. //在主服务器查看数据和日志偏移量
  47. [root@mysql56 ~]# mysql -e 'select * from testdb.user'
  48. +-------+----------+
  49. | name | password |
  50. +-------+----------+
  51. | yaya | 123456 |
  52. | yayaB | 123456 |
  53. +-------+----------+
  54. [root@mysql56 ~]# mysql -e 'show master status'
  55. +----------------+----------+--------------+------------------+-------------------+
  56. | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
  57. +----------------+----------+--------------+------------------+-------------------+
  58. | mysql56.000002 | 4807 | | | |
  59. +----------------+----------+--------------+------------------+-------------------+
  60. [root@mysql56 ~]#
  61. //客户端连接mycat服务查看到的是3条记录
  62. [root@client50 ~]# mysql -h192.168.88.58 -P8066 -umycat -p654321 -e 'select * from testdb.user'
  63. mysql: [Warning] Using a password on the command line interface can be insecure.
  64. +-------+----------+
  65. | name | password |
  66. +-------+----------+
  67. | yaya | 123456 |
  68. | yayaA | 654321 |
  69. | yayaB | 123456 |
  70. +-------+----------+
  71. [root@client50 ~]#

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/882272.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

世微AP2813 平均电流双路降压恒流驱动器 LED储能电源驱动指示灯IC 可恒流可爆闪 可双路恒流

产品描述 AP2813 是一款双路降压恒流驱动器,高效率、外围简单、内置功率管&#xff0c;适用于 5-80V 输入的高精度降压 LED 恒流驱动芯片。内置功率管输出最大功率可达12W&#xff0c;最大电流 1.2A。AP2813 一路直亮&#xff0c;另外一路通过 MODE1 切换全亮&#xff0c;爆闪…

西门子以太网PLC的跨网段无线通讯和仪表的数据采集

产品介绍 产品型号&#xff1a;NET50-PN-W4 使用范围&#xff1a;用于西门子以太网PLC的跨网段无线通讯和仪表的数据采集 一、产品介绍 工业通讯桥接器&#xff08;NET50-PN-W4&#xff09;用于西门子以太网PLC的通讯扩展&#xff0c;以太网设备的跨网段通讯和Modbus仪表的无…

C语言 ——函数指针变量、函数指针数组、回调函数

目录 一、函数指针变量 1、概念 2、函数调用 3、复杂的函数指针变量 分析&#xff1a; 结论&#xff1a; 二、typdef 普通类型重命名&#xff1a; 指针类型重命名&#xff1a; 三、函数指针数组 1、概念 2、转移表—函数指针数组的用途 模拟计算器 四、回调函数 …

突破大模型 | Alluxio助力AI大模型训练-成功案例(一)

更多详细内容可见《Alluxio助力AI大模型训练制胜宝典》 【案例一&#xff1a;知乎】多云缓存在知乎的探索:从UnionStore到Alluxio 作者&#xff1a;胡梦宇-知乎大数据基础架构开发工程师&#xff08;内容转载自InfoQ&#xff09; 一、背景 随着云原生技术的飞速发展&#xff…

时序预测 | MATLAB实现WOA-CNN-GRU鲸鱼算法优化卷积门控循环单元时间序列预测

时序预测 | MATLAB实现WOA-CNN-GRU鲸鱼算法优化卷积门控循环单元时间序列预测 目录 时序预测 | MATLAB实现WOA-CNN-GRU鲸鱼算法优化卷积门控循环单元时间序列预测预测效果基本介绍模型描述程序设计参考资料 预测效果 基本介绍 时序预测 | MATLAB实现WOA-CNN-GRU鲸鱼算法优化卷积…

金融助贷客户数据资源有哪些模式?

目前助贷行业百花齐放&#xff0c;各家都在竞新创造更优化的获客方式。那么从传统方式发展至今&#xff0c;助贷行业的获客主要模式都更新了那些呢&#xff1f; 无论做什么产品&#xff0c; 什么新项目&#xff0c; 拓展客户都成了最重要的问题。 特别是随着互联网技术的不…

【Linux命令详解 | top命令】 top命令用于动态显示系统中进程的活动情况,类似于任务管理器

文章标题 简介一&#xff0c;参数列表二&#xff0c;使用介绍1. 安装与基本使用2. 实时监控与交互操作3. 进程排序和筛选4. 杀死进程5. 查看系统总体性能6. 退出top 总结参考资料 简介 top命令是一个强大的终端工具&#xff0c;用于实时显示系统中运行的进程活动情况。类似于任…

OCR相关模块——版面分析技术、表格文本识别

OCR相关模块——版面分析技术、表格文本识别 版面分析技术表格识别技术 版面分析技术 版面分析模型&#xff1a;飞桨用到了yolov2检测模型&#xff0c;对文档图片中的文本、表格、图片、标题与列表区域进行检测。当前主流是用分割做。 表格识别技术 参考博文

【C语言】字符串函数的介绍一(strlen、strcpy、stract)

前言 这篇文章是对于字符串操作函数、内存函数的比较详细的介绍。 我们都知道&#xff0c;字符串在C语言中使用的特别频繁&#xff0c;但类型里&#xff0c;却没有字符串这种类型&#xff0c;这时&#xff0c;众多的库函数就可以帮助我们灵活地使用字符串了 这篇文章同样适合…

派克Parker伺服驱动器 高性能电机控制系统的应用详解

派克Parker伺服驱动器及电机是一种高性能的电机控制系统&#xff0c;广泛应用于机器人、医疗设备、工业自动化和航空航天等领域。具有高精度、高可靠性、高动态性能、低噪音、低振动、低能耗等优点&#xff0c;采用了先进的数字信号处理技术&#xff0c;能够实现高精度的位置控…

【es6】函数柯里化(Currying)

柯里化&#xff08;Currying&#xff09;&#xff1a;把接受多个参数的函数变换成接受一个单一参数(最初函数的第一个参数)的函数&#xff0c;并且返回接受余下的参数且返回结果的新函数。 柯里化由 Christopher Strachey 以逻辑学家 Haskell Curry 命名的&#xff0c;它是 Mos…

第四章:前端框架Vue基础入门

文章目录 一、Vue框架概述1.1 声明响应式的数据 二、Vue内置指令2.1、条件渲染指令v-if/v-show2.2 v-for: 列表渲染2.3、v-text/v-html 模板指令2.4 v-on:事件监听器2.6 动态绑定v-bind2.7 v-model表单元素值绑定 三、计算属性与监视3.1 计算属性computed3.2 watch侦听器3.3 wa…

摆脱焦虑,释放技术人的潜能!

引言 在这个瞬息万变的时代&#xff0c;每个人都或多或少会面临职场生涯中的焦虑与迷茫。这种焦虑具有时代性特质&#xff0c;既源于对自我的疑惑&#xff0c;也对这个变化太快的世界感到不安。 对于技术从业者来说&#xff0c;科技变革加速带来的冲击尤为强烈。面对日新月异的…

【Visual Studio Code】--- Win11 C盘爆满 修改 Code 插件数据和缓存的保存路径

Win11 C盘爆满 修改 Code 插件数据和缓存的保存路径 一、概述二、修改 Code 插件数据和缓存的保存路径 一、概述 一个好的文章能够帮助开发者完成更便捷、更快速的开发。书山有路勤为径&#xff0c;学海无涯苦作舟。我是秋知叶i、期望每一个阅读了我的文章的开发者都能够有所成…

天津和则百顺国际贸易有限公司:连接中国和印度汽车品牌的国际间贸易桥梁

全球汽车产业正迎来前所未有的变革,而中国和印度作为两大新兴市场,其汽车品牌也在逐步崭露头角。在这个背景下,天津和则百顺国际贸易有限公司(以下简称和则百顺)以其出色的贸易服务,成为了连接中国和印度汽车品牌的重要国际间贸易桥梁。 中国和印度汽车市场的崛起 中国和印度分…

STM32CubeMx驱动SG90(360度)

SG90 360度是一直转 而不是给定角度转的 pwm周期必须为20ms 0.5ms占空比 反转速度最大 1.5ms 不转 2.5ms正转速度最大

如何实现安全上网

l 场景描述 政府、军工、科研等涉密单位或企业往往要比其他组织更早接触高精尖的技术与产品&#xff0c;相对应的数据保密性要求更高。常规的内外网物理隔离手段&#xff0c;已经满足不了这些涉密单位的保密需求&#xff0c;发展到现在&#xff0c;需求已经演变成既要保证网络…

学习笔记十四:K8S最小调度单元POD概述

K8S最小调度单元POD概述 k8s核心资源Pod介绍Pod是什么Pod如何管理多个容器Pod网络Pod存储代码自动发版更新收集业务日志 Pod工作方式自主式Pod控制器管理的Pod(防误删除) 如何基于Pod运行应用 k8s核心资源Pod介绍 K8s官方文档&#xff1a;https://kubernetes.io/ K8s中文官方文…

STM32--TIM定时器(3)

文章目录 输入捕获简介频率测量输入捕获通道输入捕获基本结构PWMI的基本结构输入捕获模式测量PWM频率和占空比代码 编码器接口正交编码器工作模式接口基本结构TIM编码接口器测速代码&#xff1a; 输入捕获简介 输入捕获IC(Input Capture)&#xff0c;是处理器捕获外部输入信号…

DDPM: Denoising Diffusion Probabilistic Models

DDPM: Denoising Diffusion Probabilistic Models 去噪扩散模型前向过程-加噪声反向过程-去噪声 去噪扩散模型 论文题目&#xff1a;Denoising Diffusion Probabilistic Models (DDPM) 论文来源&#xff1a;NIPS, 2020 论文地址&#xff1a;https://arxiv.org/abs/2006.11239 论…