proxysql安装和使用小记

MySQL环境信息:

操作系统:CentOS release 6.9 

主库: 192.168.140.51

从库: 192.168.140.52 

从库: 192.168.16.150 

proxysql中间件: 192.168.140.52

备注:两个从库都需要开启read_only=on,命令为

mysql> set global read_only=on


编辑proxysql.repo文件

#vi /etc/yum.repos.d/proxysql.repo 

[proxysql_repo]

name= ProxySQL YUM repository

baseurl=http://repo.proxysql.com/ProxySQL/proxysql-1.4.x/centos/$releasever

gpgcheck=1

gpgkey=http://repo.proxysql.com/ProxySQL/repo_pub_key



#yum install proxysql -y

Loaded plugins: fastestmirror

Setting up Install Process

Determining fastest mirrors

epel/metalink                                                                                                          | 8.9 kB     00:00     

 * base: mirrors.huaweicloud.com

 * epel: mirrors.ustc.edu.cn

 * extras: mirrors.huaweicloud.com

 * updates: mirrors.163.com

base                                                                                                                   | 3.7 kB     00:00     

epel                                                                                                                   | 3.2 kB     00:00     

epel/primary                                                                                                           | 3.2 MB     00:00     

epel                                                                                                                              12515/12515

extras                                                                                                                 | 3.4 kB     00:00     

percona                                                                                                                | 2.9 kB     00:00     

percona/primary_db                                                                                                     | 346 kB     00:09     

percona-release-noarch                                                                                                 | 2.9 kB     00:00     

percona-release-x86_64                                                                                                 | 2.9 kB     00:00     

percona-release-x86_64/primary_db                                                                                      | 346 kB     00:09     

proxysql_repo                                                                                                          | 2.9 kB     00:00     

proxysql_repo/primary_db                                                                                               |  12 kB     00:00     

updates                                                                                                                | 3.4 kB     00:00     

updates/primary_db                                                                                                     | 1.3 MB     00:00     

Resolving Dependencies

--> Running transaction check

---> Package proxysql.x86_64 0:1.4.12-1 will be installed

--> Finished Dependency Resolution


Dependencies Resolved


==============================================================================================================================================

 Package                          Arch                           Version                          Repository                             Size

==============================================================================================================================================

Installing:

 proxysql                         x86_64                         1.4.12-1                         proxysql_repo                         5.9 M


Transaction Summary

==============================================================================================================================================

Install       1 Package(s)


Total download size: 5.9 M

Installed size: 22 M

Downloading Packages:

proxysql-1.4.12-1-centos67.x86_64.rpm                                                                                  | 5.9 MB     00:09     

warning: rpmts_HdrFromFdno: Header V4 RSA/SHA256 Signature, key ID 79953b49: NOKEY

Retrieving key from http://repo.proxysql.com/ProxySQL/repo_pub_key

Importing GPG key 0x79953B49:

 Userid: "rene cannnao (Proxysql Repository) <rene.cannao@gmail.com>"

 From  : http://repo.proxysql.com/ProxySQL/repo_pub_key

Running rpm_check_debug

Running Transaction Test

Transaction Test Succeeded

Running Transaction

  Installing : proxysql-1.4.12-1.x86_64                                                                                                   1/1 

  Verifying  : proxysql-1.4.12-1.x86_64                                                                                                   1/1 


Installed:

  proxysql.x86_64 0:1.4.12-1                                                                                                                  


Complete!


启动proxysql:

#service proxysql start

Starting ProxySQL: 2018-10-09 09:05:58 [INFO] Using config file /etc/proxysql.cnf

DONE!


查看版本信息:

#proxysql --version

ProxySQL version 1.4.12-9-g216b872, codename Truls



登陆proxysql:

设置prompt:

export MYSQL_PS1="\\u@\\h [\\d] \\r:\\m:\\s>>>"


进入ProxySQL:

#mysql -uadmin -padmin -h127.0.0.1 -P6032


admin@127.0.0.1 [(none)] 05:27:35>>>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.01 sec)


备注:6032是proxysql的管理端口号.


数据库信息介绍:

main 内存配置数据库,表里存放后端db实例、用户验证、路由规则等信息。

disk 是持久化到硬盘的配置。

stats是统计信息的汇总,是proxysql运行抓取的统计信息,包括到后端各命令的执行次数、流量、processlist、查询种类汇总/执行时间等。

monitor是一些监控的收集信息,主要是对后端db的健康/延迟检查等。



在master上添加proxysql监控账号和对外访问账号:


mysql> create user monitor@'192.168.140.%' identified by '123456';

Query OK, 0 rows affected (0.29 sec)


mysql> 

mysql> create user monitor@'192.168.16.%' identified by '123456';

Query OK, 0 rows affected (0.01 sec)


mysql> grant all privileges on *.* to monitor@'192.168.140.%';

Query OK, 0 rows affected (0.04 sec)


mysql> grant all privileges on *.* to monitor@'192.168.16.%';

Query OK, 0 rows affected (0.00 sec)



mysql> 

mysql> create user dsf@'%' identified by 'dsf';

Query OK, 0 rows affected (0.09 sec)


mysql> grant all privileges on *.* to dsf@'%' with grant option;

Query OK, 0 rows affected (0.06 sec)


mysql> flush privileges;

Query OK, 0 rows affected (0.12 sec)



mysql> use main

Reading table information for completion of table and column names

You can turn off this feature to get a quicker startup with -A


Database changed

mysql> show tables;

+--------------------------------------------+

| tables                                     |

+--------------------------------------------+

| global_variables                           |

| mysql_collations                           |

| mysql_group_replication_hostgroups         |

| mysql_query_rules                          |

| mysql_query_rules_fast_routing             |

| mysql_replication_hostgroups               |

| mysql_servers                              |

| mysql_users                                |

| proxysql_servers                           |

| runtime_checksums_values                   |

| runtime_global_variables                   |

| 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_scheduler                          |

| scheduler                                  |

+--------------------------------------------+

20 rows in set (0.00 sec)


mysql> show create table mysql_servers \G

*************************** 1. row ***************************

       table: mysql_servers

Create Table: CREATE TABLE mysql_servers (

    hostgroup_id INT CHECK (hostgroup_id>=0) NOT NULL DEFAULT 0,

    hostname VARCHAR NOT NULL,

    port INT NOT NULL DEFAULT 3306,

    status VARCHAR CHECK (UPPER(status) IN ('ONLINE','SHUNNED','OFFLINE_SOFT', 'OFFLINE_HARD')) NOT NULL DEFAULT 'ONLINE',

    weight INT CHECK (weight >= 0) NOT NULL DEFAULT 1,

    compression INT CHECK (compression >=0 AND compression <= 102400) NOT NULL DEFAULT 0,

    max_connections INT CHECK (max_connections >=0) NOT NULL DEFAULT 1000,

    max_replication_lag INT CHECK (max_replication_lag >= 0 AND max_replication_lag <= 126144000) NOT NULL DEFAULT 0,

    use_ssl INT CHECK (use_ssl IN(0,1)) NOT NULL DEFAULT 0,

    max_latency_ms INT UNSIGNED CHECK (max_latency_ms>=0) NOT NULL DEFAULT 0,

    comment VARCHAR NOT NULL DEFAULT '',

    PRIMARY KEY (hostgroup_id, hostname, port) )

1 row in set (0.00 sec)


添加主从服务器信息列表:

mysql> insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.140.51',3306);

Query OK, 1 row affected (0.00 sec)


mysql> insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.140.52',3306);

Query OK, 1 row affected (0.00 sec)


mysql> insert into mysql_servers(hostgroup_id,hostname,port) values(10,'192.168.16.150',3306);

Query OK, 1 row affected (0.00 sec)


从memory加载到runtime:

mysql> load mysql servers to runtime;

Query OK, 0 rows affected (0.05 sec)


持久化到磁盘:

mysql> save mysql servers to disk;

Query OK, 0 rows affected (0.56 sec)


查看server状态信息,三台应该都是online:

mysql>  select * from mysql_servers;

+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+

| hostgroup_id | hostname       | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |

+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+

| 10           | 192.168.140.51 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |

| 10           | 192.168.140.52 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |

| 10           | 192.168.16.150 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |

+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+

3 rows in set (0.00 sec)


为proxysql配置监控账号:

mysql> set mysql-monitor_username='monitor';

Query OK, 1 row affected (0.00 sec)


mysql> set mysql-monitor_password='123456';

Query OK, 1 row affected (0.00 sec)


mysql> load mysql variables to runtime;

Query OK, 0 rows affected (0.00 sec)


mysql> save mysql variables to disk;

Query OK, 97 rows affected (0.29 sec)


查看监控信息,监控正常,没有任何报错:

mysql> select * from monitor.mysql_server_connect_log order by time_start_us desc limit 6;

+----------------+------+------------------+-------------------------+---------------+

| hostname       | port | time_start_us    | connect_success_time_us | connect_error |

+----------------+------+------------------+-------------------------+---------------+

| 192.168.140.51 | 3306 | 1539064061326592 | 3513                    | NULL          |

| 192.168.140.52 | 3306 | 1539064060571978 | 2129                    | NULL          |

| 192.168.16.150 | 3306 | 1539064059817210 | 3859                    | NULL          |

| 192.168.140.52 | 3306 | 1539064000942524 | 1271                    | NULL          |

| 192.168.140.51 | 3306 | 1539064000379889 | 3259                    | NULL          |

| 192.168.16.150 | 3306 | 1539063999817183 | 2875                    | NULL          |

+----------------+------+------------------+-------------------------+---------------+

6 rows in set (0.01 sec)


配置读写分离:

设置proxysql主从分组信息:

mysql> show create table mysql_replication_hostgroups \G

*************************** 1. row ***************************

       table: mysql_replication_hostgroups

Create Table: CREATE TABLE mysql_replication_hostgroups (

    writer_hostgroup INT CHECK (writer_hostgroup>=0) NOT NULL PRIMARY KEY,

    reader_hostgroup INT NOT NULL CHECK (reader_hostgroup<>writer_hostgroup AND reader_hostgroup>0),

    comment VARCHAR NOT NULL DEFAULT '', UNIQUE (reader_hostgroup))

1 row in set (0.00 sec)


mysql> insert into mysql_replication_hostgroups values(10,20,'proxy info');

Query OK, 1 row affected (0.00 sec)



mysql> load mysql servers to runtime;

Query OK, 0 rows affected (0.00 sec)


mysql> 

mysql> save mysql servers to disk;

Query OK, 0 rows affected (0.47 sec)


mysql> select * from mysql_replication_hostgroups;

+------------------+------------------+------------+

| writer_hostgroup | reader_hostgroup | comment    |

+------------------+------------------+------------+

| 10               | 20               | proxy info |

+------------------+------------------+------------+

1 row in set (0.00 sec)


mysql> select * from mysql_servers;

+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+

| hostgroup_id | hostname       | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment |

+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+

| 10           | 192.168.140.51 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |

| 20           | 192.168.16.150 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |

| 20           | 192.168.140.52 | 3306 | ONLINE | 1      | 0           | 1000            | 0                   | 0       | 0              |         |

+--------------+----------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+

3 rows in set (0.00 sec)


备注:

proxysql会根据server的read_only的值将服务器自动进行分组,read_only=0的分到编号为10的写组,read_only=1的分到编号为20的读组。


配置对外访问账号,开启事务持久化保护:

mysql> insert into mysql_users(username,password,default_hostgroup) values('dsf','dsf',10);

Query OK, 1 row affected (0.00 sec)


mysql> update mysql_users set transaction_persistent=1 where username='dsf';

Query OK, 1 row affected (0.00 sec)



mysql> load mysql users to runtime;

Query OK, 0 rows affected (0.00 sec)


mysql> 

mysql> 

mysql> save mysql users to disk;

Query OK, 0 rows affected (0.10 sec)


验证主服务器,需要指定对外端口号6033:

#mysql -udsf -pdsf -h 192.168.140.52 -P 6033 -e "show slave hosts"

mysql: [Warning] Using a password on the command line interface can be insecure.

+-----------+------+------+-----------+--------------------------------------+

| Server_id | Host | Port | Master_id | Slave_UUID                           |

+-----------+------+------+-----------+--------------------------------------+

|     16150 |      | 3306 |     14051 | e982cd68-cac0-11e8-8cfc-525400a6c4f1 |

|     14052 |      | 3306 |     14051 | dab0225f-952d-11e8-ac10-52540098ed65 |

+-----------+------+------+-----------+--------------------------------------+


#mysql -udsf -pdsf -h 192.168.140.52 -P 6033 -e "select @@hostname"

mysql: [Warning] Using a password on the command line interface can be insecure.

+--------------------+

| @@hostname         |

+--------------------+

| test-140-51        |

+--------------------+


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15498/viewspace-2215684/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15498/viewspace-2215684/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Protobuf是一种高效的序列化协议,可以用于数据交换和数据存储。它的主要优势是大小小,速度快,可扩展性强。下面是使用Protobuf的一些小记: 1. 定义消息格式 首先,需要定义消息格式,以便Protobuf可以将数据序列化和反序列化。消息格式定义在.proto文件中,使用protobuf语言编写。例如,下面是一个简单的消息格式定义: ``` syntax = "proto3"; message Person { string name = 1; int32 age = 2; } ``` 这个消息格式定义了一个名为Person的消息,包含两个字段:name和age。 2. 生成代码 一旦消息格式定义好,就可以使用Protobuf编译器生成代码。编译器将根据消息格式定义生成相应的代码,包括消息类、序列化和反序列化方法等。可以使用以下命令生成代码: ``` protoc --java_out=. message.proto ``` 这将生成一个名为message.pb.javaJava类,该类包含Person消息的定义以及相关方法。 3. 序列化和反序列化 一旦生成了代码,就可以使用Protobuf序列化和反序列化数据。例如,下面是一个示例代码,将一个Person对象序列化为字节数组,并将其反序列化为另一个Person对象: ``` Person person = Person.newBuilder() .setName("Alice") .setAge(25) .build(); byte[] bytes = person.toByteArray(); Person deserializedPerson = Person.parseFrom(bytes); ``` 这个示例代码创建了一个Person对象,将其序列化为字节数组,然后将其反序列化为另一个Person对象。在这个过程中,Protobuf使用生成的代码执行序列化和反序列化操作。 以上是使用Protobuf的一些基本步骤和注意事项,希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值