mysql-router的安裝與使用


mysql-router是mysql官方的輕量級的中間件主要有如下工能:

功能 & 特性:

  1、流量分發(只向一個實例導入流量、如果當前的實例宕機了、就會向集群的下一個實例導入流量)

  2、一定的高可用能力

  3、比之前的官方中間件mysql-proxy性能上有好一點

  4、可以與mysql-fabric整合

1、下載

 https://dev.mysql.com/get/Downloads/MySQL-Router/mysql-router-2.0.4-linux-glibc2.12-x86-64bit.tar.gz

 

2、安裝

tar -xzvf mysql-router-2.0.4-linux-glibc2.12-x86-64bit.tar.gz -C /usr/local/
ln -s /usr/local/mysql-router-2.0.4-linux-glibc2.12-x86-64bit /usr/local/mysqlrouter
export /usr/local/mysqlrouter/bin/:$PATH

 

3、測試安裝是否成功

mysqlrouter --help
MySQL Router v2.0.4 on Linux (64-bit) (GPL community edition)
Copyright (c) 2015, 2016, 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.

Start MySQL Router.

Configuration read from the following files in the given order (enclosed
in parentheses means not available for reading):
  (./mysqlrouter.ini)
  (/root/.mysqlrouter.ini)

Usage: mysqlrouter [-v|--version] [-h|--help]
                   [-c|--config=<path>]
                   [-a|--extra-config=<path>]

Options:
  -v, --version
        Display version information and exit.
  -h, --help
        Display this help and exit.
  -c <path>, --config <path>
        Only read configuration from given file.
  -a <path>, --extra-config <path>
        Read this file after configuration files are read from either
        default locations or from files specified by the --config
        option.

 

4、配置mysql-router

/usr/local/mysqlrouter/etc/mysqlrouter.cnf

[DEFAULT]
logging_folder                  =/usr/local/mysqlrouter/run/
plugin_folder                   =/usr/local/mysqlrouter/lib/mysqlrouter/
runtime_folder                  =/usr/local/mysqlrouter/run/
config_folder                   =/usr/local/mysqlrouter/etc/

[logger]
level                           =debug

[routing:7001]
bind_address                    =0.0.0.0:7001
connect_timeout                 =2
mode                            =read-write
# [read-write | read-only]
max_connections                 =256
max_connect_errors              =1000
client_connect_timeout          =2
destinations                    =127.0.0.1:3307,127.0.0.1:3308,127.0.0.1:3309

 

5、為了測試mysql-router的功能(讀寫分離+高可用)、於是初始化三個實例(3307,3308,3309)各個實例的配置文件如下

3307

[mysql]
auto-rehash



[mysqld]
####: for global
user                    =mysql                    #    mysql
basedir                    =/usr/local/mysql                #    /usr/local/mysql/
datadir                    =/usr/local/datas/3307                #    /usr/local/mysql/data
server_id                =1                        #    0
port                    =3307                        #    3306
character_set_server            =utf8                        #    latin1
log_timestamps                =system                        #    utc
socket                    =mysql.sock                #    /tmp/mysql.sock
read_only                =0                        #    off
skip-slave-start            =1                        #    
auto_increment_increment        =1                        #    1
auto_increment_offset            =1                        #    1
lower_case_table_names            =1                        #    0
secure_file_priv            =                        #    null


####: for binlog
binlog_format                =row                        #    row
log_bin                    =mysql-bin                    #    off
binlog_rows_query_log_events        =on                        #    off
log_slave_updates            =on                        #    off
expire_logs_days            =4                        #    0
binlog_cache_size            =32768                        #    32768(32k)
binlog_checksum                =none                        #    CRC32
sync_binlog                =1                        #    1


####: for error-log
log_error                =error.log                    #    /usr/local/mysql/data/localhost.localdomain.err


####: for slow query log


####: for gtid
gtid_executed_compression_period    =1000                        #    1000
gtid_mode                =on                        #    off
enforce_gtid_consistency        =on                        #    off


####: for replication
master_info_repository            =table                        #    file
relay_log_info_repository        =table                        #    file


####: for group replication
transaction_write_set_extraction    =XXHASH64                    #    off
loose-group_replication_group_name    ="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"        #    
loose-group_replication_start_on_boot    =off                        #    off
loose-group_replication_local_address    ="127.0.0.1:24901"                #
loose-group_replication_group_seeds    ="127.0.0.1:24901,127.0.0.1:24902,127.0.0.1:24903"
loose-group_replication_bootstrap_group    =off                        #    off


####: for innodb
default_storage_engine            =innodb                        #    innodb
default_tmp_storage_engine        =innodb                        #    innodb
innodb_data_file_path            =ibdata1:12M:autoextend                #    ibdata1:12M:autoextend
innodb_temp_data_file_path        =ibtmp1:12M:autoextend                #    ibtmp1:12M:autoextend
innodb_buffer_pool_filename        =ib_buffer_pool                    #    ib_buffer_pool
innodb_log_group_home_dir        =./                        #    ./
innodb_log_files_in_group        =2                        #    2
innodb_log_file_size            =48M                        #    50331648(48M)
innodb_file_format            =Barracuda                    #    Barracuda
innodb_file_per_table            =on                         #    on
innodb_page_size            =16k                        #    16384(16k)
innodb_thread_concurrency        =0                        #    0
innodb_read_io_threads            =4                        #    4
innodb_write_io_threads            =4                        #    4
innodb_purge_threads            =4                        #    4
innodb_print_all_deadlocks        =on                        #    off
innodb_deadlock_detect            =on                        #    on
innodb_lock_wait_timeout        =50                        #    50
innodb_spin_wait_delay            =6                        #    6
innodb_autoinc_lock_mode        =2                        #    1
innodb_stats_persistent            =on                        #    on
innodb_stats_persistent_sample_pages    =20                        #    20
innodb_buffer_pool_instances        =1                        #    1
innodb_adaptive_hash_index        =on                        #    on
innodb_change_buffering            =all                        #    all
innodb_change_buffer_max_size        =25                        #    25
innodb_flush_neighbors            =1                        #    1
innodb_flush_method            =O_DIRECT                    #    
innodb_doublewrite            =on                        #    on
innodb_log_buffer_size            =16M                        #    16777216(16M)
innodb_flush_log_at_timeout        =1                        #    1
innodb_flush_log_at_trx_commit        =1                        #    1
innodb_buffer_pool_size            =134217728                    #    134217728(128M)
autocommit                =1                        #    1



####  for performance_schema
performance_schema                            =on        #    on
performance_schema_consumer_events_stages_current            =on        #    off
performance_schema_consumer_events_stages_history            =on        #    off
performance_schema_consumer_events_stages_history_long            =off        #    off
performance_schema_consumer_statements_digest                =on        #    on
performance_schema_consumer_events_statements_current            =on        #    on
performance_schema_consumer_events_statements_history            =on        #    on
performance_schema_consumer_events_statements_history_long        =off        #    off
performance_schema_consumer_events_waits_current            =on        #    off
performance_schema_consumer_events_waits_history            =on        #    off
performance_schema_consumer_events_waits_history_long            =off        #    off
performance_schema_consumer_global_instrumentation            =on        #    on
performance_schema_consumer_thread_instrumentation            =on        #    on
View Code

3308

[mysql]
auto-rehash



[mysqld]
####: for global
user                    =mysql                    #    mysql
basedir                    =/usr/local/mysql                #    /usr/local/mysql/
datadir                    =/usr/local/datas/3308                #    /usr/local/mysql/data
server_id                =2                        #    0
port                    =3308                        #    3306
character_set_server            =utf8                        #    latin1
log_timestamps                =system                        #    utc
socket                    =mysql.sock                #    /tmp/mysql.sock
read_only                =0                        #    off
skip-slave-start            =1                        #    
auto_increment_increment        =1                        #    1
auto_increment_offset            =1                        #    1
lower_case_table_names            =1                        #    0
secure_file_priv            =                        #    null


####: for binlog
binlog_format                =row                        #    row
log_bin                    =mysql-bin                    #    off
binlog_rows_query_log_events        =on                        #    off
log_slave_updates            =on                        #    off
expire_logs_days            =4                        #    0
binlog_cache_size            =32768                        #    32768(32k)
binlog_checksum                =none                        #    CRC32
sync_binlog                =1                        #    1


####: for error-log
log_error                =error.log                    #    /usr/local/mysql/data/localhost.localdomain.err


####: for slow query log


####: for gtid
gtid_executed_compression_period    =1000                        #    1000
gtid_mode                =on                        #    off
enforce_gtid_consistency        =on                        #    off


####: for replication
master_info_repository            =table                        #    file
relay_log_info_repository        =table                        #    file


####: for group replication
transaction_write_set_extraction    =XXHASH64                    #    off
loose-group_replication_group_name    ="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"        #    
loose-group_replication_start_on_boot    =off                        #    off
loose-group_replication_local_address    ="127.0.0.1:24901"                #
loose-group_replication_group_seeds    ="127.0.0.1:24901,127.0.0.1:24902,127.0.0.1:24903"
loose-group_replication_bootstrap_group    =off                        #    off


####: for innodb
default_storage_engine            =innodb                        #    innodb
default_tmp_storage_engine        =innodb                        #    innodb
innodb_data_file_path            =ibdata1:12M:autoextend                #    ibdata1:12M:autoextend
innodb_temp_data_file_path        =ibtmp1:12M:autoextend                #    ibtmp1:12M:autoextend
innodb_buffer_pool_filename        =ib_buffer_pool                    #    ib_buffer_pool
innodb_log_group_home_dir        =./                        #    ./
innodb_log_files_in_group        =2                        #    2
innodb_log_file_size            =48M                        #    50331648(48M)
innodb_file_format            =Barracuda                    #    Barracuda
innodb_file_per_table            =on                         #    on
innodb_page_size            =16k                        #    16384(16k)
innodb_thread_concurrency        =0                        #    0
innodb_read_io_threads            =4                        #    4
innodb_write_io_threads            =4                        #    4
innodb_purge_threads            =4                        #    4
innodb_print_all_deadlocks        =on                        #    off
innodb_deadlock_detect            =on                        #    on
innodb_lock_wait_timeout        =50                        #    50
innodb_spin_wait_delay            =6                        #    6
innodb_autoinc_lock_mode        =2                        #    1
innodb_stats_persistent            =on                        #    on
innodb_stats_persistent_sample_pages    =20                        #    20
innodb_buffer_pool_instances        =1                        #    1
innodb_adaptive_hash_index        =on                        #    on
innodb_change_buffering            =all                        #    all
innodb_change_buffer_max_size        =25                        #    25
innodb_flush_neighbors            =1                        #    1
innodb_flush_method            =O_DIRECT                    #    
innodb_doublewrite            =on                        #    on
innodb_log_buffer_size            =16M                        #    16777216(16M)
innodb_flush_log_at_timeout        =1                        #    1
innodb_flush_log_at_trx_commit        =1                        #    1
innodb_buffer_pool_size            =134217728                    #    134217728(128M)
autocommit                =1                        #    1



####  for performance_schema
performance_schema                            =on        #    on
performance_schema_consumer_events_stages_current            =on        #    off
performance_schema_consumer_events_stages_history            =on        #    off
performance_schema_consumer_events_stages_history_long            =off        #    off
performance_schema_consumer_statements_digest                =on        #    on
performance_schema_consumer_events_statements_current            =on        #    on
performance_schema_consumer_events_statements_history            =on        #    on
performance_schema_consumer_events_statements_history_long        =off        #    off
performance_schema_consumer_events_waits_current            =on        #    off
performance_schema_consumer_events_waits_history            =on        #    off
performance_schema_consumer_events_waits_history_long            =off        #    off
performance_schema_consumer_global_instrumentation            =on        #    on
performance_schema_consumer_thread_instrumentation            =on        #    on
View Code

3309

[mysql]
auto-rehash



[mysqld]
####: for global
user                    =mysql                    #    mysql
basedir                    =/usr/local/mysql                #    /usr/local/mysql/
datadir                    =/usr/local/datas/3309                #    /usr/local/mysql/data
server_id                =3                        #    0
port                    =3309                        #    3306
character_set_server            =utf8                        #    latin1
log_timestamps                =system                        #    utc
socket                    =mysql.sock                #    /tmp/mysql.sock
read_only                =0                        #    off
skip-slave-start            =1                        #    
auto_increment_increment        =1                        #    1
auto_increment_offset            =1                        #    1
lower_case_table_names            =1                        #    0
secure_file_priv            =                        #    null


####: for binlog
binlog_format                =row                        #    row
log_bin                    =mysql-bin                    #    off
binlog_rows_query_log_events        =on                        #    off
log_slave_updates            =on                        #    off
expire_logs_days            =4                        #    0
binlog_cache_size            =32768                        #    32768(32k)
binlog_checksum                =none                        #    CRC32
sync_binlog                =1                        #    1


####: for error-log
log_error                =error.log                    #    /usr/local/mysql/data/localhost.localdomain.err


####: for slow query log


####: for gtid
gtid_executed_compression_period    =1000                        #    1000
gtid_mode                =on                        #    off
enforce_gtid_consistency        =on                        #    off


####: for replication
master_info_repository            =table                        #    file
relay_log_info_repository        =table                        #    file


####: for group replication
transaction_write_set_extraction    =XXHASH64                    #    off
loose-group_replication_group_name    ="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"        #    
loose-group_replication_start_on_boot    =off                        #    off
loose-group_replication_local_address    ="127.0.0.1:24901"                #
loose-group_replication_group_seeds    ="127.0.0.1:24901,127.0.0.1:24902,127.0.0.1:24903"
loose-group_replication_bootstrap_group    =off                        #    off


####: for innodb
default_storage_engine            =innodb                        #    innodb
default_tmp_storage_engine        =innodb                        #    innodb
innodb_data_file_path            =ibdata1:12M:autoextend                #    ibdata1:12M:autoextend
innodb_temp_data_file_path        =ibtmp1:12M:autoextend                #    ibtmp1:12M:autoextend
innodb_buffer_pool_filename        =ib_buffer_pool                    #    ib_buffer_pool
innodb_log_group_home_dir        =./                        #    ./
innodb_log_files_in_group        =2                        #    2
innodb_log_file_size            =48M                        #    50331648(48M)
innodb_file_format            =Barracuda                    #    Barracuda
innodb_file_per_table            =on                         #    on
innodb_page_size            =16k                        #    16384(16k)
innodb_thread_concurrency        =0                        #    0
innodb_read_io_threads            =4                        #    4
innodb_write_io_threads            =4                        #    4
innodb_purge_threads            =4                        #    4
innodb_print_all_deadlocks        =on                        #    off
innodb_deadlock_detect            =on                        #    on
innodb_lock_wait_timeout        =50                        #    50
innodb_spin_wait_delay            =6                        #    6
innodb_autoinc_lock_mode        =2                        #    1
innodb_stats_persistent            =on                        #    on
innodb_stats_persistent_sample_pages    =20                        #    20
innodb_buffer_pool_instances        =1                        #    1
innodb_adaptive_hash_index        =on                        #    on
innodb_change_buffering            =all                        #    all
innodb_change_buffer_max_size        =25                        #    25
innodb_flush_neighbors            =1                        #    1
innodb_flush_method            =O_DIRECT                    #    
innodb_doublewrite            =on                        #    on
innodb_log_buffer_size            =16M                        #    16777216(16M)
innodb_flush_log_at_timeout        =1                        #    1
innodb_flush_log_at_trx_commit        =1                        #    1
innodb_buffer_pool_size            =134217728                    #    134217728(128M)
autocommit                =1                        #    1



####  for performance_schema
performance_schema                            =on        #    on
performance_schema_consumer_events_stages_current            =on        #    off
performance_schema_consumer_events_stages_history            =on        #    off
performance_schema_consumer_events_stages_history_long            =off        #    off
performance_schema_consumer_statements_digest                =on        #    on
performance_schema_consumer_events_statements_current            =on        #    on
performance_schema_consumer_events_statements_history            =on        #    on
performance_schema_consumer_events_statements_history_long        =off        #    off
performance_schema_consumer_events_waits_current            =on        #    off
performance_schema_consumer_events_waits_history            =on        #    off
performance_schema_consumer_events_waits_history_long            =off        #    off
performance_schema_consumer_global_instrumentation            =on        #    on
performance_schema_consumer_thread_instrumentation            =on        #    on
View Code

 

6、分別啟動這三個實例

mysqld --defaults-file=/usr/local/datas/my.cnf.3307 &
mysqld --defaults-file=/usr/local/datas/my.cnf.3308 &
mysqld --defaults-file=/usr/local/datas/my.cnf.3309 &

 

7、分別對這三個實例增加用戶和庫

3307

create user appuser@'%' identified by '123456';
create user appuser@'127.0.0.1' identified by '123456';

grant all on *.* to appuser@'%';
grant all on *.* to appuser@'127.0.0.1';

create database db3307;

create database tempdb;

create table tempdb.t(x int);
insert into tempdb.t(x) values(3307);

3308

create user appuser@'%' identified by '123456';
create user appuser@'127.0.0.1' identified by '123456';

grant all on *.* to appuser@'%';
grant all on *.* to appuser@'127.0.0.1';

create database db3308;

create database tempdb;

create table tempdb.t(x int);
insert into tempdb.t(x) values(3308);

3309

create user appuser@'%' identified by '123456';
create user appuser@'127.0.0.1' identified by '123456';

grant all on *.* to appuser@'%';
grant all on *.* to appuser@'127.0.0.1';

create database db3309;

create database tempdb;

create table tempdb.t(x int);
insert into tempdb.t(x) values(3309);

 

8、啟動mysqlrouter

mysqlrouter --config /usr/local/mysqlrouter/etc/mysqlrouter.cnf &

 

9、測試通過mysqlrouter這個中間件來連接mysql庫

mysql -h127.0.0.1 -uappuser -p123456 -e"show databases;" -P7001
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db3307             |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
[jianglexing@workspace Desktop]$ mysql -h127.0.0.1 -uappuser -p123456 -e"select * from tempdb.t;" -P7001
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| x    |
+------+
| 3307 |
+------+
[jianglexing@workspace Desktop]$ mysql -h127.0.0.1 -uappuser -p123456 -e"select * from tempdb.t;" -P7001
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| x    |
+------+
| 3307 |
+------+
[jianglexing@workspace Desktop]$ mysql -h127.0.0.1 -uappuser -p123456 -e"select * from tempdb.t;" -P7001
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| x    |
+------+
| 3307 |
+------+
[jianglexing@workspace Desktop]$ mysql -h127.0.0.1 -uappuser -p123456 -e"select * from tempdb.t;" -P7001
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| x    |
+------+
| 3307 |
+------+

由上面可以看出mysqlrouter總是把流量導向了3307這個實例好像沒有看到什么讀寫分離的能力,難道只有高可用(只是一個分發器,一個mysql掛了,然后把

流量導到另一個上面去,如果是這樣的話,還不如lvs呢!)

 

10、測試它的高可用能力(kill 掉3307)這個實例、然后再測試mysqlrouter把流量導到了哪里去

kill -9 pid_of_mysql_3307

 

mysql -h127.0.0.1 -uappuser -p123456 -e"select * from tempdb.t;" -P7001
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| x    |
+------+
| 3308 |
+------+
[jianglexing@workspace Desktop]$ mysql -h127.0.0.1 -uappuser -p123456 -e"select * from tempdb.t;" -P7001
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| x    |
+------+
| 3308 |
+------+
[jianglexing@workspace Desktop]$ mysql -h127.0.0.1 -uappuser -p123456 -e"select * from tempdb.t;" -P7001
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| x    |
+------+
| 3308 |
+------+
[jianglexing@workspace Desktop]$ mysql -h127.0.0.1 -uappuser -p123456 -e"select * from tempdb.t;" -P7001
mysql: [Warning] Using a password on the command line interface can be insecure.
+------+
| x    |
+------+
| 3308 |
+------+

 

 

結論

  由測試的結果可以看出mysqlrouter總是把流量導入到其配置文件中destinations項中最左邊的那個活着的的實例上去;

  建議后端的mysql集群使用mysql-group-replication來保存數據的一致性。

 

 

 ----


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM