1. 程式人生 > 其它 >基於Vue的任務清單(To Do List)

基於Vue的任務清單(To Do List)

技術標籤:中介軟體mysql

1. mysql實現讀寫分離的方式

mysql 實現讀寫分離的方式有以下幾種:

  • 程式修改mysql操作,直接和資料庫通訊,簡單快捷的讀寫分離和隨機的方式實現的負載均衡,許可權獨立分配,需要開發人員協助。
  • amoeba,直接實現讀寫分離和負載均衡,不用修改程式碼,有很靈活的資料解決方案,自己分配賬戶,和後端資料庫許可權管理獨立,許可權處理不夠靈活。
  • mysql-proxy,直接實現讀寫分離和負載均衡,不用修改程式碼,master和slave用一樣的帳號,效率低
  • mycat中介軟體
  • proxysql中介軟體(推薦使用)

2. ProxySQL簡介

ProxySQL 是一款可以實際用於生產環境的 MySQL 中介軟體,它有官方版和 percona 版兩種。percona版是在官方版的基礎上修改的,添加了幾個比較實用的工具。生產環境建議用官方版。

ProxySQL 是用 C++ 語言開發的,雖然也是一個輕量級產品,但效能很好(據測試,能處理千億級的資料),功能也足夠,能滿足中介軟體所需的絕大多數功能,包括:

  • 最基本的讀/寫分離,且方式有多種
  • 可定製基於使用者、基於schema、基於語句的規則對SQL語句進行路由。換句話說,規則很靈活。基於schema和與語句級的規則,可以實現簡單的sharding(分庫分表)
  • 可快取查詢結果。雖然ProxySQL的快取策略比較簡陋,但實現了基本的快取功能,絕大多數時候也夠用了。此外,作者已經打算實現更豐富的快取策略
  • 監控後端節點。ProxySQL可以監控後端節點的多個指標,包括:ProxySQL和後端的心跳資訊,後端節點的read-only/read-write,slave和master的資料同步延遲性(replication lag)

3. ProxySQL安裝

環境說明:

主機版本主機名IP要安裝的服務
CentOS8proxysql192.168.50.153proxysql
mariadb
CentOS8master192.168.50.154mariadb
CentOS8slave192.168.50.155mariadb
1. 配置yum源
[[email protected] ~]# cd /etc/yum.repos.d/
[[email protected] yum.repos.d]# ls                 保留CentOS8版本預設的源
CentOS-AppStream.repo   CentOS-Media.repo
CentOS-Base.repo        CentOS-PowerTools.repo
CentOS-centosplus.repo  CentOS-Sources.repo
CentOS-CR.repo          CentOS-Vault.repo
CentOS-Debuginfo.repo   epel-modular.repo
CentOS-Devel.repo       epel-playground.repo
CentOS-Extras.repo      epel.repo
CentOS-fasttrack.repo   epel-testing-modular.repo
CentOS-HA.repo          epel-testing.repo


2. 寫一個配置檔案
[
[email protected]
~]# vim /etc/yum.repos.d/proxysql.repo [proxysql_repo] name= ProxySQL baseurl=http://repo.proxysql.com/ProxySQL/proxysql-2.0.x/centos/8 gpgcheck=1 gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key 3. 安裝proxysql [[email protected] ~]# yum -y install proxysql [[email protected] ~]# rpm -qa|grep proxysql proxysql-2.0.15-1.x86_64

4. ProxySQL的Admin管理介面

[[email protected] ~]# systemctl enable --now proxysql
[[email protected] ~]# ss -antl
State  Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0      128          0.0.0.0:6032      0.0.0.0:*   
LISTEN 0      128          0.0.0.0:6033      0.0.0.0:*   
LISTEN 0      128          0.0.0.0:6033      0.0.0.0:*   
LISTEN 0      128          0.0.0.0:6033      0.0.0.0:*   
LISTEN 0      128          0.0.0.0:6033      0.0.0.0:*   
LISTEN 0      128          0.0.0.0:22        0.0.0.0:*   
LISTEN 0      128             [::]:22           [::]:*   

當 ProxySQL 啟動後,將監聽兩個埠:

  • admin管理介面,預設埠為6032。該埠用於檢視、配置ProxySQL
  • 接收SQL語句的介面,預設埠為6033,這個介面類似於MySQL的3306埠
    在這裡插入圖片描述
    ProxySQL 的 admin 管理介面是一個使用 MySQL 協議的介面,所以,可以直接使用 mysql 客戶端、navicat 等工具去連線這個管理介面,其預設的使用者名稱和密碼均為 admin

例如,使用 mysql 客戶端去連線 ProxySQL 的管理介面:

1. 安裝mysql客戶端,不用安裝服務端,所以不用安裝mariadb*
[[email protected] ~]# yum -y install mariadb
[[email protected] ~]# rpm -qa|grep mariadb
mariadb-connector-c-config-3.0.7-1.el8.noarch
mariadb-common-10.3.17-1.module_el8.1.0+257+48736ea6.x86_64
mariadb-10.3.17-1.module_el8.1.0+257+48736ea6.x86_64
mariadb-connector-c-3.0.7-1.el8.x86_64



2. 登陸mysql
[[email protected] ~]# mysql -uadmin -padmin -P6032 -h127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (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;
+-----+---------------+-------------------------------------+
| seq | name          | file                                |
+-----+---------------+-------------------------------------+
| 0   | main          |                                     |
| 2   | disk          | /var/lib/proxysql/proxysql.db       |
| 3   | stats         |                                     |
| 4   | monitor       |                                     |
| 5   | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
5 rows in set (0.000 sec)

5. 和admin管理介面相關的變數

5.1 admin-admin_credentials

admin-admin_credentials 變數控制的是admin管理介面的管理員賬戶。預設的管理員賬戶和密碼為admin:admin,但是這個預設的使用者只能在本地使用。如果想要遠端連線到ProxySQL,例如用windows上的navicat連線Linux上的ProxySQL管理介面,必須自定義一個管理員賬戶。

新增管理員帳戶

[[email protected] ~]# mysql -uadmin -padmin -P6032 -h127.0.0.1      //登陸進去
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (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)]> select @@admin-admin_credentials;          //檢視當前使用者名稱和密碼
+---------------------------+
| @@admin-admin_credentials |
+---------------------------+
| admin:admin               |
+---------------------------+
1 row in set (0.001 sec)




1. 設定新管理員賬號proadmin,密碼123456
MySQL [(none)]> set admin-admin_credentials='admin:admin;proadmin:123456';
Query OK, 1 row affected (0.001 sec)

MySQL [(none)]> select @@admin-admin_credentials;
+------------------------------+
| @@admin-admin_credentials    |
+------------------------------+
| admin:admin;proadmin:123456 |
+------------------------------+
1 row in set (0.002 sec)


2. 使修改立即生效
MySQL [(none)]> load admin variables to runtime;         
Query OK, 0 rows affected (0.001 sec)


3.  使修改永久儲存到磁碟
MySQL [(none)]> save admin variables to disk;          
Query OK, 35 rows affected (0.003 sec)



4. 關閉防火牆
[[email protected] ~]# systemctl stop firewalld
[[email protected] ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[[email protected] ~]# vim /etc/selinux/config 

修改後,就可以使用該使用者名稱和密碼連線管理介面

[[email protected] ~]# mysql -uproadmin -p123456 -P6032 -h 192.168.50.153
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (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 tables from main;       這些表都是臨時的
+----------------------------------------------------+
| tables                                             |
+----------------------------------------------------+
| global_variables                                   |
| mysql_aws_aurora_hostgroups                        |
| mysql_collations                                   |
| mysql_firewall_whitelist_rules                     |
| mysql_firewall_whitelist_sqli_fingerprints         |
| mysql_firewall_whitelist_users                     |
| mysql_galera_hostgroups                            |
| mysql_group_replication_hostgroups                 |
| mysql_query_rules                                  |
| mysql_query_rules_fast_routing                     |
| mysql_replication_hostgroups                       |
| mysql_servers                                      |
| mysql_users                                        |
| proxysql_servers                                   |
| restapi_routes                                     |
| runtime_checksums_values                           |
| runtime_global_variables                           |
| runtime_mysql_aws_aurora_hostgroups                |
| runtime_mysql_firewall_whitelist_rules             |
| runtime_mysql_firewall_whitelist_sqli_fingerprints |
| runtime_mysql_firewall_whitelist_users             |
| runtime_mysql_galera_hostgroups                    |
| runtime_mysql_group_replication_hostgroups         |
| runtime_mysql_query_rules                          |
| runtime_mysql_query_rules_fast_routing             |
| runtime_mysql_replication_hostgroups               |
| runtime_mysql_servers                              |
| runtime_mysql_users                                |
| runtime_proxysql_servers                           |
| runtime_restapi_routes                             |
| runtime_scheduler                                  |
| scheduler                                          |
+----------------------------------------------------+
32 rows in set (0.001 sec)

所有的配置操作都是在修改main庫中對應的表

以下兩種查尋方式的結果是一樣的
MySQL [(none)]> select * from global_variables where variable_name='admin-admin_credentials';    
+-------------------------+------------------------------+
| variable_name           | variable_value               |
+-------------------------+------------------------------+
| admin-admin_credentials | admins:admin;proadmin:123456 |
+-------------------------+------------------------------+
1 row in set (0.002 sec)


MySQL [(none)]> select @@admin-admin_credentials;
+------------------------------+
| @@admin-admin_credentials    |
+------------------------------+
| admins:admin;proadmin:123456 |
+------------------------------+
1 row in set (0.001 sec)

必須要區分admin管理介面的使用者名稱和mysql_users中的使用者名稱

  • admin管理介面的使用者是連線到管理介面(預設埠6032)上用來管理、配置ProxySQL的
  • mysql_users表中的使用者名稱是應用程式連線ProxySQL(預設埠6033),以及ProxySQL連線後端MySQL Servers使用的使用者。它的作用是傳送、路由SQL語句,類似於MySQL Server的3306埠。所以,這個表中的使用者必須已經在後端MySQL Server上存在且授權了

admin管理介面的使用者必須不能存在於mysql_users中,這是出於安全的考慮,防止通過admin管理介面使用者猜出mysql_users中的使用者

5.2 admin-stats_credentials

admin-stats_credentials 變數控制admin管理介面的普通使用者,這個變數中的使用者沒有超級管理員許可權,只能檢視monitor庫和main庫中關於統計的資料,其它庫都是不可見的,且沒有任何寫許可權

預設的普通使用者名稱和密碼均為 stats ,與admin一樣,它預設也只能用於本地登入,若想讓人遠端檢視則要新增檢視的專有使用者

[[email protected] ~]# mysql -ustats -pstats -P6032 -h127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (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;
+-----+---------------+-------------------------------------+
| seq | name          | file                                |
+-----+---------------+-------------------------------------+
| 0   | main          |                                     |
| 2   | monitor       |                                     |
| 3   | stats_history | /var/lib/proxysql/proxysql_stats.db |
+-----+---------------+-------------------------------------+
3 rows in set (0.001 sec)

MySQL [(none)]>  show tables from main;
+--------------------------------------+
| tables                               |
+--------------------------------------+
| global_variables                     |
| stats_memory_metrics                 |
| stats_mysql_commands_counters        |
| stats_mysql_connection_pool          |
| stats_mysql_connection_pool_reset    |
| stats_mysql_errors                   |
| stats_mysql_errors_reset             |
| stats_mysql_free_connections         |
| stats_mysql_global                   |
| stats_mysql_gtid_executed            |
| stats_mysql_prepared_statements_info |
| stats_mysql_processlist              |
| stats_mysql_query_digest             |
| stats_mysql_query_digest_reset       |
| stats_mysql_query_rules              |
| stats_mysql_users                    |
| stats_proxysql_servers_checksums     |
| stats_proxysql_servers_metrics       |
| stats_proxysql_servers_status        |
+--------------------------------------+
19 rows in set (0.000 sec)



1. 使用admin登陸進去
[[email protected] ~]# mysql -uadmin -padmin -P6032 -h127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 14
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (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)]> select @@admin-stats_credentials;
+---------------------------+
| @@admin-stats_credentials |
+---------------------------+
| stats:stats               |
+---------------------------+
1 row in set (0.001 sec)

MySQL [(none)]> set admin-stats_credentials='stats:stats;prostats:123456';            //新增專有的檢視使用者
Query OK, 1 row affected (0.000 sec)

MySQL [(none)]> select @@admin-stats_credentials;
+-----------------------------+
| @@admin-stats_credentials   |
+-----------------------------+
| stats:stats;prostats:123456 |
+-----------------------------+
1 row in set (0.001 sec)


MySQL [(none)]> load admin variables to runtime;
Query OK, 0 rows affected (0.000 sec)

MySQL [(none)]> save admin variables to disk;
Query OK, 35 rows affected (0.003 sec)

同樣,這個變數中的使用者必須不能存在於mysql_users表中
使用mystats使用者遠端連線檢視

[[email protected] ~]# mysql -uprostats -p123456 -h192.168.50.153 -P6032
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (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 tables from main;
+--------------------------------------+
| tables                               |
+--------------------------------------+
| global_variables                     |
| stats_memory_metrics                 |
| stats_mysql_commands_counters        |
| stats_mysql_connection_pool          |
| stats_mysql_connection_pool_reset    |
| stats_mysql_errors                   |
| stats_mysql_errors_reset             |
| stats_mysql_free_connections         |
| stats_mysql_global                   |
| stats_mysql_gtid_executed            |
| stats_mysql_prepared_statements_info |
| stats_mysql_processlist              |
| stats_mysql_query_digest             |
| stats_mysql_query_digest_reset       |
| stats_mysql_query_rules              |
| stats_mysql_users                    |
| stats_proxysql_servers_checksums     |
| stats_proxysql_servers_metrics       |
| stats_proxysql_servers_status        |
+--------------------------------------+
19 rows in set (0.001 sec)

5.3 admin-mysql_ifaces

admin-mysql_ifaces 變數指定admin介面的監聽地址,格式為冒號分隔的hostname:port列表。預設監聽在 0.0.0.0:6032

MySQL [(none)]> select @@admin-mysql_ifaces;
+----------------------+
| @@admin-mysql_ifaces |
+----------------------+
| 0.0.0.0:6032         |
+----------------------+
1 row in set (0.001 sec)

MySQL [(none)]> set admin-mysql_ifaces='0.0.0.0:8888';
Query OK, 1 row affected (0.001 sec)

MySQL [(none)]> select @@admin-mysql_ifaces;
+----------------------+
| @@admin-mysql_ifaces |
+----------------------+
| 0.0.0.0:8888         |
+----------------------+
1 row in set (0.002 sec)


MySQL [(none)]> load admin variables to runtime;
Query OK, 0 rows affected (0.000 sec)

MySQL [(none)]> save admin variables to disk;
Query OK, 35 rows affected (0.004 sec)

MySQL [(none)]> quit
Bye
[[email protected] ~]# ss -antl
State  Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0      128          0.0.0.0:6033      0.0.0.0:*   
LISTEN 0      128          0.0.0.0:6033      0.0.0.0:*   
LISTEN 0      128          0.0.0.0:6033      0.0.0.0:*   
LISTEN 0      128          0.0.0.0:6033      0.0.0.0:*   
LISTEN 0      128          0.0.0.0:22        0.0.0.0:*   
LISTEN 0      128          0.0.0.0:8888      0.0.0.0:*      此時埠變成0.0.0.0:8888
LISTEN 0      128             [::]:22           [::]:*   


此時就要用8888來連線
[[email protected] ~]# mysql -uadmin -padmin -P6032 -h127.0.0.1       用6032連不進去
ERROR 2002 (HY000): Can't connect to MySQL server on '127.0.0.1' (115)

[[email protected] ~]# mysql -uadmin -padmin -P8888 -h127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (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)]> 

注意,允許使用UNIX的domain socket進行監聽,這樣本主機內的應用程式就可以直接被處理。
例如:

此時不加-h連不進去
[[email protected] ~]# mysql -uadmin -padmin -P8888
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)

1. 先連進去
[[email protected] ~]# mysql -uadmin -padmin -P8888 -h127.0.0.1
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (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)]> set admin-mysql_ifaces='0.0.0.0:8888;/tmp/proxysql.sock';     新增套接字檔案
Query OK, 1 row affected (0.000 sec)

MySQL [(none)]> load admin variables to runtime;
Query OK, 0 rows affected (0.000 sec)

MySQL [(none)]> save admin variables to disk;
Query OK, 35 rows affected (0.003 sec)

MySQL [(none)]> quit
Bye
[[email protected] ~]# ls /tmp/
proxysql.sock               vmware-root_871-3980298366
vmware-root_845-4021653450  vmware-root_885-4021784556
 

2. 此時能加-S連進去
[[email protected] ~]# mysql -uadmin -padmin -P8888 -S /tmp/proxysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 18
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (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)]> quit
Bye


3. 建立一個目錄並修改許可權
[[email protected] ~]# mkdir /var/lib/mysql/
[[email protected] ~]# chown -R proxysql.proxysql /var/lib/mysql/
[[email protected] ~]# mysql -uadmin -padmin -P8888 -S /tmp/proxysql.sock 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 19
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (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)]> 
MySQL [(none)]> select @@admin-mysql_ifaces;
+---------------------------------+
| @@admin-mysql_ifaces            |
+---------------------------------+
| 0.0.0.0:8888;/tmp/proxysql.sock |
+---------------------------------+
1 row in set (0.001 sec)

MySQL [(none)]> set admin-mysql_ifaces='0.0.0.0:8888;/var/lib/mysql/mysql.sock';  把路徑改成這個路徑就可以不使用-h來連線了
Query OK, 1 row affected (0.000 sec)

MySQL [(none)]> select @@admin-mysql_ifaces;
+----------------------------------------+
| @@admin-mysql_ifaces                   |
+----------------------------------------+
| 0.0.0.0:8888;/var/lib/mysql/mysql.sock |
+----------------------------------------+
1 row in set (0.001 sec)


MySQL [(none)]> load admin variables to runtime;
Query OK, 0 rows affected (0.001 sec)

MySQL [(none)]> save admin variables to disk;
Query OK, 35 rows affected (0.004 sec)

MySQL [(none)]> quit
Bye
[[email protected] ~]# ls /var/lib/mysql/
mysql.sock


4. 此時不需要使用-h就可以連線進來
[[email protected] ~]# mysql -uadmin -padmin -P8888
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 5.5.30 (ProxySQL Admin Module)

Copyright (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)]>