主從同步介紹、主從同步原理、主從同步結構、構建思路、配置一主一從、配置一主多從、讀寫分離介紹、工作原理、配置mycat服務、添加數據源、創建集群、指定主機角

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.pswp.cn/news/38129.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/38129.shtml
英文地址,請注明出處:http://en.pswp.cn/news/38129.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

網絡編程(8.14)TCP并發服務器模型

作業&#xff1a; 1. 多線程中的newfd&#xff0c;能否修改成全局&#xff0c;不行&#xff0c;為什么&#xff1f; 2. 多線程中分支線程的newfd能否不另存&#xff0c;直接用指針間接訪問主線程中的newfd,不行&#xff0c;為什么&#xff1f; 多線程并發服務器模型原代碼&…

排查docker無法啟動問題

查看Linux系統操作日志(最后200行就可以排查)&#xff1a; tail -200f /var/log/messages

數據分析--帆軟報表--大數據大屏

進入國企公司學習有一段時間了&#xff0c;崗位是數據分析方向------ 母前使用的是帆軟工具進行的開發。 可以進行大數據大屏 也可使嵌入到手機端。 下面是例子

Python-OpenCV中的圖像處理-GrabCut算法交互式前景提取

Python-OpenCV中的圖像處理-GrabCut算法交互式前景提取 Python-OpenCV中的圖像處理-GrabCut算法交互式前景提取 Python-OpenCV中的圖像處理-GrabCut算法交互式前景提取 cv2.grabCut(img: Mat, mask: typing.Optional[Mat], rect, bgdModel, fgdModel, iterCount, mode…) img…

數據庫連接池

什么是數據庫連接池 使用數據庫連接池的好處是減少了連接的創建和關閉的開銷&#xff0c;提高了數據庫訪問的性能和效率。 為什么我們要使用數據庫連接池 我們使用數據庫連接池的主要原因是為了提高應用程序訪問數據庫的性能和效率。使用數據庫連接池的好處: 連接重用&…

【Apple】Logic Pro導入7.1.4.wav并自動分析多聲道

Step1: 創建空項目 Step2: 選中下圖“使用麥克風或...”這一項&#xff0c;底下要創建的軌道數填1就行。 點擊創建之后&#xff1a; Step3: 拖動文件、拖動文件、拖動文件到項目中&#xff0c;并選中復選框“所有所選文件都源自一個項目&#xff08;將創建一個智能速度多軌道集…

[NLP]LLM 訓練時GPU顯存耗用量估計

以LLM中最常見的Adam fp16混合精度訓練為例&#xff0c;分析其顯存占用有以下四個部分&#xff1a; GPT-2含有1.5B個參數&#xff0c;如果用fp16格式&#xff0c;只需要1.5G*2Byte3GB顯存, 但是模型狀態實際上需要耗費1.5B*1624GB. 比如說有一個模型參數量是1M&#xff0c;在…

什么是前端框架?怎么學習? - 易智編譯EaseEditing

前端框架是一種用于開發Web應用程序界面的工具集合&#xff0c;它提供了一系列預定義的代碼和結構&#xff0c;以簡化開發過程并提高效率。 前端框架通常包括HTML、CSS和JavaScript的庫和工具&#xff0c;用于構建交互式、動態和響應式的用戶界面。 學習前端框架可以讓您更高效…

nginx的負載均衡

nginx的負載均衡 文章目錄 nginx的負載均衡1.以多臺虛擬機作服務器1.1 在不同的虛擬機上安裝httpd服務1.2 在不同虛擬機所構建的服務端的默認路徑下創建不同標識的文件1.3 使用windows本機的瀏覽器分別訪問3臺服務器的地址 2.在新的一臺虛擬機上配置nginx實現反向代理以及負載均…

使用element UI 的el-upload上傳圖片并攜帶參數的用法

直接看代碼&#xff1a;前端實現 <div class"upload"><el-uploadclass"upload-demo"name"upload_name":data"{user_name:user_name}"action"http://localhost:8000/api/deal_pest_Image":show-file-list"fal…

vb+sql汽車配件管理系統設計與實現

摘 要 目前汽車配件銷售企業大多數在其連鎖店的管理還是手工進行,隨著汽車配件行業的迅速發展,手工管理的種種弊端暴露無疑,給銷售企業的發展帶來了不必要的麻煩。為了規范企業內部管理,提高企業業務管理水平,更好的為客戶服務,應采用計算機來管理汽車配件的進銷存業務。…

【Sklearn】基于樸素貝葉斯算法的數據分類預測(Excel可直接替換數據)

【Sklearn】基于樸素貝葉斯算法的數據分類預測(Excel可直接替換數據) 1.模型原理2.模型參數3.文件結構4.Excel數據5.下載地址6.完整代碼7.運行結果1.模型原理 模型原理: 樸素貝葉斯分類是基于貝葉斯定理的一種分類方法。它假設特征之間相互獨立(樸素性),從而簡化計算過…

01|Java中常見錯誤或不清楚

補充&#xff1a;length vs length() vs size() 1 java中的length屬性是針對數組說的,比如說你聲明了一個數組,想知道這個數組的長度則用到了length這個屬性. 2 java中的length()方法是針對字符串String說的,如果想看這個字符串的長度則用到length()這個方法. 3.java中的siz…

【Vue-Router】命名視圖

命名視圖 同時 (同級) 展示多個視圖&#xff0c;而不是嵌套展示&#xff0c;例如創建一個布局&#xff0c;有 sidebar (側導航) 和 main (主內容) 兩個視圖&#xff0c;這個時候命名視圖就派上用場了。 可以在界面中擁有多個單獨命名的視圖&#xff0c;而不是只有一個單獨的出…

Python獲取、修改主機名稱和IP地址實踐

Python獲取、修改主機名稱和IP地址的方法有多種&#xff0c;內置socket模塊、執行系統命令、第三方模塊等等&#xff0c;本文只是完成功能的一次成功的實踐。 1. 獲取、修改主機名稱 本案例使用python的socket模塊獲取、修改主機名稱&#xff0c;socket模塊是一個用于實現網絡…

UML-A 卷-知識考卷

UML-A 卷-知識考卷 UML有多少種圖&#xff0c;請列出每種圖的名字&#xff1a; 常用的幾種UML圖&#xff1a; 類圖&#xff08;Class Diagram&#xff09;&#xff1a;類圖是描述類、接口、關聯關系和繼承關系的圖形化表示。它展示了系統中各個類之間的靜態結構和關系。時序…

TFRecords詳解

內容目錄 TFRecords 是什么序列化(Serialization)tf.data 圖像序列化&#xff08;Serializing Images)tf.Example函數封裝 小結 TFRecords 是什么 TPU擁有八個核心&#xff0c;充當八個獨立的工作單元。我們可以通過將數據集分成多個文件或分片&#xff08;shards&#xff09;…

2023年7月京東洗衣機行業品牌銷售排行榜(京東數據分析軟件)

2023年上半年&#xff0c;洗衣機市場表現平淡&#xff0c;同環比來看出貨量都有一定程度的下滑。7月份&#xff0c;洗衣機市場仍未改變這一下滑態勢。 根據鯨參謀電商數據分析平臺的相關數據顯示&#xff0c;7月份&#xff0c;京東平臺洗衣機的銷量為109萬&#xff0c;環比下降…

web圖書管理系統Servlet+JSP+javabean+MySQL圖書商城圖書館 源代碼

本項目為前幾天收費幫學妹做的一個項目&#xff0c;Java EE JSP項目&#xff0c;在工作環境中基本使用不到&#xff0c;但是很多學校把這個當作編程入門的項目來做&#xff0c;故分享出本項目供初學者參考。 一、項目描述 web圖書管理系統ServletJSPjavabeanMySQL 系統有1權限…

Golang 程序性能優化利器 PGO 詳解(二):收集樣本數據和編譯

在軟件開發過程中&#xff0c;性能優化是不可或缺的一部分。無論是在Web服務、數據處理系統還是實時通信中&#xff0c;良好的性能都是至關重要的。Golang 從1.20版版本開始引入的 Profile Guided Optimization&#xff08;PGO&#xff09;機制能夠幫助更好地優化 Go 程序的性能…