MYSQL读写分离解决方案:MariaDB MaxScale部署实录
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MYSQL读写分离解决方案:MariaDB MaxScale部署实录相关的知识,希望对你有一定的参考价值。
MASTER(KING01)
[[email protected] ~]# mysql -uroot -pabcd.1234 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 173 Server version: 5.5.5-10.2.12-MariaDB-log Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show master status; +------------------+-----------+--------------+------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | +------------------+-----------+--------------+------------------+ | mysql-bin.000003 | 251166915 | | | +------------------+-----------+--------------+------------------+ 1 row in set (0.00 sec)
SLAVE(KING02)
[[email protected] ~]# mysql -uroot -pabcd.1234 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 26 Server version: 5.5.5-10.2.12-MariaDB-log Source distribution Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.201 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 251188037 Relay_Log_File: relay-bin.000002 Relay_Log_Pos: 174038 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 251188037 Relay_Log_Space: 174341 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_SSL_Crl: Master_SSL_Crlpath: Using_Gtid: No Gtid_IO_Pos: Replicate_Do_Domain_Ids: Replicate_Ignore_Domain_Ids: Parallel_Mode: conservative SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it 1 row in set (0.00 sec)
MAXSCALE(KING04)
[[email protected] ~]# curl -sS https://downloads.mariadb.com/MariaDB/mariadb_repo_setup | bash [warning] Found existing file at /etc/yum.repos.d/mariadb.repo. Moving to /etc/yum.repos.d/mariadb.repo.old_1. [info] Repository file successfully written to /etc/yum.repos.d/mariadb.repo. [info] Adding trusted package signing keys... [info] Succeessfully added trusted package signing keys.
[[email protected] ~]# yum install -y maxscale latest
[[email protected] ~]# maxkeys Generating .secrets file in /var/lib/maxscale. [[email protected] ~]# maxpasswd /var/lib/maxscale maxscale 5A23832649A7C09A5011B99813491542
[[email protected] ~]# vi /etc/maxscale.cnf # MaxScale documentation: # https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-22/ # Global parameters # # Complete list of configuration options: # https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-22-mariadb-maxscale-configuration-usage-scenarios/ [maxscale] threads=auto log_info=1 log_warning=1 # Server definitions # # Set the address of the server to the network # address of a MariaDB server. # [server1] type=server address=192.168.1.201 port=3306 protocol=MariaDBBackend [server2] type=server address=192.168.1.202 port=3306 protocol=MariaDBBackend # Monitor for the servers # # This will keep MaxScale aware of the state of the servers. # MariaDB Monitor documentation: # https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-22-mariadb-monitor/ [MariaDB-Monitor] type=monitor module=mariadbmon servers=server1,server2 user=monitor passwd=5A23832649A7C09A5011B99813491542 monitor_interval=2000 detect_replication_lag=true detect_stale_master=true # Service definitions # # Service Definition for a read-only service and # a read/write splitting service. # # ReadConnRoute documentation: # https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-22-readconnroute/ # [Read-Only-Service] # type=service # router=readconnroute # servers=server1 # user=myuser # passwd=mypwd # router_options=slave # ReadWriteSplit documentation: # https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-22-readwritesplit/ [Read-Write-Service] type=service router=readwritesplit servers=server1,server2 user=router passwd=5A23832649A7C09A5011B99813491542 max_slave_connections=100% max_slave_replication_lag=5 use_sql_variables_in=all enable_root_user=1 log_auth_warnings=1 # This service enables the use of the MaxAdmin interface # MaxScale administration guide: # https://mariadb.com/kb/en/mariadb-enterprise/mariadb-maxscale-22-maxadmin-admin-interface/ [MaxAdmin-Service] type=service router=cli # Listener definitions for the services # # These listeners represent the ports the # services will listen on. # # [Read-Only-Listener] # type=listener # service=Read-Only-Service # protocol=MariaDBClient # port=4008 [Read-Write-Listener] type=listener service=Read-Write-Service address=192.168.1.204 protocol=MariaDBClient port=4006 [MaxAdmin-Listener] type=listener service=MaxAdmin-Service protocol=maxscaled socket=default
[[email protected] ~]# /etc/init.d/maxscale start Starting MaxScale: maxscale (pid 1500) is running... [ OK ] [[email protected] ~]# /etc/init.d/maxscale status Checking MaxScale status: MaxScale (pid 1500) is running..[ OK ] [[email protected] ~]# cat /var/log/maxscale/maxscale.log
MariaDB MaxScale /var/log/maxscale/maxscale.log Fri Mar 9 15:00:40 2018 ---------------------------------------------------------------------------- 2018-03-09 15:00:40 notice : Working directory: /var/log/maxscale 2018-03-09 15:00:40 notice : The collection of SQLite memory allocation statistics turned off. 2018-03-09 15:00:40 notice : Threading mode of SQLite set to Multi-thread. 2018-03-09 15:00:40 notice : MariaDB MaxScale 2.2.2 started 2018-03-09 15:00:40 notice : MaxScale is running in process 1432 2018-03-09 15:00:40 notice : Configuration file: /etc/maxscale.cnf 2018-03-09 15:00:40 notice : Log directory: /var/log/maxscale 2018-03-09 15:00:40 notice : Data directory: /var/lib/maxscale 2018-03-09 15:00:40 notice : Module directory: /usr/lib64/maxscale 2018-03-09 15:00:40 notice : Service cache: /var/cache/maxscale 2018-03-09 15:00:40 notice : Loading /etc/maxscale.cnf. 2018-03-09 15:00:40 notice : The logging of informational messages has been enabled. 2018-03-09 15:00:40 notice : The logging of warning messages has been enabled. 2018-03-09 15:00:40 notice : /etc/maxscale.cnf.d does not exist, not reading. 2018-03-09 15:00:40 notice : [cli] Initialise CLI router module 2018-03-09 15:00:40 notice : Loaded module cli: V1.0.0 from /usr/lib64/maxscale/libcli.so 2018-03-09 15:00:40 notice : [readwritesplit] Initializing statement-based read/write split router module. 2018-03-09 15:00:40 notice : Loaded module readwritesplit: V1.1.0 from /usr/lib64/maxscale/libreadwritesplit.so 2018-03-09 15:00:40 notice : [mariadbmon] Initialise the MariaDB Monitor module. 2018-03-09 15:00:40 notice : Loaded module mariadbmon: V1.5.0 from /usr/lib64/maxscale/libmariadbmon.so 2018-03-09 15:00:40 notice : Loaded module MariaDBBackend: V2.0.0 from /usr/lib64/maxscale/libmariadbbackend.so 2018-03-09 15:00:40 notice : Loaded module MySQLBackendAuth: V1.0.0 from /usr/lib64/maxscale/libmysqlbackendauth.so 2018-03-09 15:00:40 info : [maxscaled] Initialise MaxScaled Protocol module. 2018-03-09 15:00:40 notice : Loaded module maxscaled: V2.0.0 from /usr/lib64/maxscale/libmaxscaled.so 2018-03-09 15:00:40 notice : Loaded module MaxAdminAuth: V2.1.0 from /usr/lib64/maxscale/libmaxadminauth.so 2018-03-09 15:00:40 notice : Loaded module MariaDBClient: V1.1.0 from /usr/lib64/maxscale/libmariadbclient.so 2018-03-09 15:00:40 notice : Loaded module MySQLAuth: V1.1.0 from /usr/lib64/maxscale/libmysqlauth.so 2018-03-09 15:00:40 notice : Monitor 'MariaDB-Monitor' is missing the 'journal_max_age' parameter, using default value of 28800 seconds. 2018-03-09 15:00:40 notice : Monitor 'MariaDB-Monitor' is missing the 'script_timeout' parameter, using default value of 90 seconds. 2018-03-09 15:00:40 notice : No query classifier specified, using default 'qc_sqlite'. 2018-03-09 15:00:40 notice : Loaded module qc_sqlite: V1.0.0 from /usr/lib64/maxscale/libqc_sqlite.so 2018-03-09 15:00:40 info : qc_sqlite loaded. 2018-03-09 15:00:40 notice : O_DIRECT is not supported for pipes on Linux kernel 2 (supported from version 3.4 onwards), NOT using it. 2018-03-09 15:00:40 info : Epoll instance for listening sockets added to worker epoll instance. 2018-03-09 15:00:40 warning: Removing stale journal file for monitor 'MariaDB-Monitor'. 2018-03-09 15:00:40 notice : Using encrypted passwords. Encryption key: '/var/lib/maxscale/.secrets'. 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('root', 'localhost', NULL, 1, '8E329B15E3C0FF9DDF7597B748CCE9473593BF60') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('root', '127.0.0.1', NULL, 1, '8E329B15E3C0FF9DDF7597B748CCE9473593BF60') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('root', '::1', NULL, 1, '8E329B15E3C0FF9DDF7597B748CCE9473593BF60') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('repl', '192.168.1.%', NULL, 0, 'A424E797037BF97C19A2E88CF7891C5C2038C039') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('tpcc', '192.168.1.%', 'tpcc1000', 0, 'D2B995B159B5840C27690FC435C6ACEE7B9E1348') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('monitor', '192.168.1.%', 'maxscale_schema', 0, '53E5D0C7885BD540911663B04133F20C94AD4306') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('router', '192.168.1.%', NULL, 0, '53E5D0C7885BD540911663B04133F20C94AD4306') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('tpcc', '192.168.1.%', NULL, 0, 'D2B995B159B5840C27690FC435C6ACEE7B9E1348') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('monitor', '192.168.1.%', NULL, 0, '53E5D0C7885BD540911663B04133F20C94AD4306') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('router', '192.168.1.%', 'mysql', 0, '53E5D0C7885BD540911663B04133F20C94AD4306') 2018-03-09 15:00:40 notice : [MySQLAuth] [Read-Write-Service] Loaded 10 MySQL users for listener Read-Write-Listener. 2018-03-09 15:00:40 notice : Listening for connections at [192.168.1.204]:4006 with protocol MySQL 2018-03-09 15:00:40 info : Started session [1] for Read-Write-Service service 2018-03-09 15:00:40 notice : Listening for connections at [/tmp/maxadmin.sock]:0 with protocol MaxScale Admin 2018-03-09 15:00:40 info : Started session [2] for MaxAdmin-Service service 2018-03-09 15:00:40 info : [qc_sqlite] In-memory sqlite database successfully opened for thread 140223790401280. 2018-03-09 15:00:40 notice : Started REST API on [127.0.0.1]:8989 2018-03-09 15:00:40 notice : MaxScale started with 1 worker threads, each with a stack size of 10485760 bytes. 2018-03-09 15:00:40 info : [qc_sqlite] In-memory sqlite database successfully opened for thread 140223848254464. 2018-03-09 15:00:40 notice : Started MaxScale log flusher. 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('root', 'localhost', NULL, 1, '8E329B15E3C0FF9DDF7597B748CCE9473593BF60') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('root', '127.0.0.1', NULL, 1, '8E329B15E3C0FF9DDF7597B748CCE9473593BF60') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('root', '::1', NULL, 1, '8E329B15E3C0FF9DDF7597B748CCE9473593BF60') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('repl', '192.168.1.%', NULL, 0, 'A424E797037BF97C19A2E88CF7891C5C2038C039') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('tpcc', '192.168.1.%', 'tpcc1000', 0, 'D2B995B159B5840C27690FC435C6ACEE7B9E1348') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('monitor', '192.168.1.%', 'maxscale_schema', 0, '53E5D0C7885BD540911663B04133F20C94AD4306') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('router', '192.168.1.%', NULL, 0, '53E5D0C7885BD540911663B04133F20C94AD4306') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('tpcc', '192.168.1.%', NULL, 0, 'D2B995B159B5840C27690FC435C6ACEE7B9E1348') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('monitor', '192.168.1.%', NULL, 0, '53E5D0C7885BD540911663B04133F20C94AD4306') 2018-03-09 15:00:40 info : [MySQLAuth] Added user: INSERT OR REPLACE INTO mysqlauth_users VALUES ('router', '192.168.1.%', 'mysql', 0, '53E5D0C7885BD540911663B04133F20C94AD4306') 2018-03-09 15:00:40 notice : Server changed state: server1[192.168.1.201:3306]: new_master. [Running] -> [Master, Running] 2018-03-09 15:00:40 notice : Server changed state: server2[192.168.1.202:3306]: new_slave. [Running] -> [Slave, Running] 2018-03-09 15:00:40 notice : [mariadbmon] A Master Server is now available: 192.168.1.201:3306 2018-03-09 15:05:31 info : (7) [readwritesplit] > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, type: QUERY_TYPE_GSYSVAR_WRITE, stmt: SET NAMES utf8 2018-03-09 15:05:31 info : (7) [readwritesplit] Session write, routing to all servers. 2018-03-09 15:05:31 info : (7) [readwritesplit] Route query to master [192.168.1.201]:3306 2018-03-09 15:05:31 info : (7) [readwritesplit] Route query to slave [192.168.1.202]:3306 2018-03-09 15:05:31 info : (7) [readwritesplit] > Autocommit: [enabled], trx is [not open], cmd: (0x02) COM_INIT_DB, type: QUERY_TYPE_SESSION_WRITE, stmt: 2018-03-09 15:05:31 info : (7) [readwritesplit] Session write, routing to all servers. 2018-03-09 15:05:31 info : (7) [readwritesplit] Route query to master [192.168.1.201]:3306 2018-03-09 15:05:31 info : (7) [readwritesplit] Route query to slave [192.168.1.202]:3306 2018-03-09 15:05:31 info : (7) [readwritesplit] > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, type: QUERY_TYPE_WRITE, stmt: SHOW TABLE STATUS LIKE 'order_line' 2018-03-09 15:05:31 info : (7) [readwritesplit] Route query to master [192.168.1.201]:3306 < 2018-03-09 15:05:31 info : (7) [readwritesplit] > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, type: QUERY_TYPE_READ, stmt: SHOW ENGINES 2018-03-09 15:05:31 info : (7) [readwritesplit] Route query to slave [192.168.1.202]:3306 < 2018-03-09 15:05:31 info : (5) [readwritesplit] > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, type: QUERY_TYPE_READ, stmt: SHOW CREATE TABLE `order_line` 2018-03-09 15:05:31 info : (5) [readwritesplit] Route query to slave [192.168.1.202]:3306 < 2018-03-09 15:05:31 info : (7) [readwritesplit] > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, type: QUERY_TYPE_READ, stmt: SELECT * FROM `order_line` LIMIT 0, 1000 2018-03-09 15:05:31 info : (7) [readwritesplit] Route query to slave [192.168.1.202]:3306 < 2018-03-09 15:05:31 info : (7) [readwritesplit] > Autocommit: [enabled], trx is [not open], cmd: (0x03) COM_QUERY, type: QUERY_TYPE_READ, stmt: SHOW COLUMNS FROM `tpcc1000`.`order_line` 2018-03-09 15:05:31 info : (7) [readwritesplit] Route query to slave [192.168.1.202]:3306 < 2018-03-09 15:05:35 info : [readwritesplit] Servers and router connection counts:
以上是关于MYSQL读写分离解决方案:MariaDB MaxScale部署实录的主要内容,如果未能解决你的问题,请参考以下文章