1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
| # 准备一个集群,有一个master主机,地址192.168.1.14,两个从节点,地址一个192.168.1.15,一个192.168.1.13。再准备一个读写分离器,地址是192.168.1.10。请求都到192.168.1.10,写到192.168.1.14,读到192.168.1.15和192.168.1.13。以上面的主从复制为基础 # 一定要同步服务器的时间,保证所有服务器的时间是一样的
* 从节点192.168.1.15,将只复制一个库改回来 MariaDB [(none)]> STOP SLAVE; MariaDB [(none)]> SET @@global.replicate_do_db=''; # 改为空就可以了 MariaDB [(none)]> START SLAVE; MariaDB [(none)]> SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.14 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-log.000003 Read_Master_Log_Pos: 7043 Relay_Log_File: relay-log.000004 Relay_Log_Pos: 530 Relay_Master_Log_File: master-log.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB:
* 从节点192.168.1.13 [root@test ~]# yum install -y mariadb-server [root@test ~]# vim /etc/my.cnf [mysqld] innodb_file_per_table=ON skip_name_resolve=ON server_id=12 relay_log=relay-log read_only=ON [root@test ~]# systemctl start mariadb
* 主节点192.168.1.14 [root@test ~]# mysqldump -uroot --all-databases -R -E --triggers -x --master-data=2 -p > alldb.sql # --all-databases , -A:导出全部数据库。 # --routines, -R:导出存储过程以及自定义函数。 # --events, -E:导出事件。 # --triggers:导出触发器。该选项默认启用,用--skip-triggers禁用它。 # --lock-all-tables, -x:提交请求锁定所有数据库中的所有表,以保证数据的一致性。这是一个全局读锁,并且自动关闭--single-transaction 和--lock-tables 选项。 # --master-data:该选项将当前服务器的binlog的位置和文件名追加到输出文件中(show master status)。如果为1,将会输出CHANGE MASTER 命令;如果为2,输出的CHANGE MASTER命令前添加注释信息。 [root@test ~]# scp alldb.sql 192.168.1.13:/root # 复制到刚加上来的从节点 [root@test ~]# rm -rf alldb.sql
* 从节点192.168.1.13 [root@test ~]# mysql -uroot -p < alldb.sql MariaDB [(none)]> CHANGE MASTER TO MASTER_HOST='192.168.1.14',MASTER_USER='repluser',MASTER_PASSWORD='replpass',MASTER_LOG_FILE='master-log.000003',MASTER_LOG_POS=7043; # 设置与主节点同步 MariaDB [(none)]> START SLAVE; MariaDB [(none)]> SHOW SLAVE STATUS\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.1.14 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-log.000003 Read_Master_Log_Pos: 7295 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 782 Relay_Master_Log_File: master-log.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes
* 主节点192.168.1.14 MariaDB [(none)]> CREATE DATABASE testdb; # 到主节点创建一个库测试一下
* 两个从节点 MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | jiaowu | | mydb | | mysql | | performance_schema | | testdb | # 在两个从节点可以看到创建的库
* 主节点192.168.1.14 MariaDB [(none)]> DROP DATABASE testdb; MariaDB [(none)]> GRANT ALL ON *.* TO 'myadmin'@'192.168.1.%' IDENTIFIED BY 'mypass'; # 创建一个有权限连接主从服务器的帐号。这个用户会同步到两个从节点 MariaDB [(none)]> FLUSH PRIVILEGES;
* 两个从节点 MariaDB [(none)]> SELECT User FROM mysql.user; +----------+ | User | +----------+ | root | | myadmin |
* 分离器192.168.1.10 下载 proxysql-1.3.6-1-centos7.x86_64.rpm [root@test ~]# yum install -y proxysql-1.3.6-1-centos7.x86_64.rpm [root@test ~]# cp /etc/proxysql.cnf{,.bak} [root@test ~]# vim /etc/proxysql.cnf datadir="/var/lib/proxysql" # 数据目录路径,这是proxysql的状态数据,与mysql无关 admin_variables= { admin_credentials="admin:admin" # 登录proxysql进行管理时用的帐号密码 mysql_ifaces="127.0.0.1:6032;/tmp/proxysql_admin.sock" # 连接时用的地址和端口 } mysql_variables= { threads=4 # 启动几个线程,它是单线程响应多个请求的,与CPU有关。测试时CPU为双核,但还是启动了四个线程 max_connections=2048 # 并发连接数 default_query_delay=0 default_query_timeout=36000000 have_compress=true poll_timeout=2000 interfaces="0.0.0.0:3306;/tmp/proxysql.sock" # 监听在所有地址的3306端口接收请求,测试调整上面的启动线程和此项的端口都不起作用,启动后还是启动4个线程,监听在6033端口。用1.4.7和1.3.6版本都是如此.恢复镜像后正常了 default_schema="mydb" # 登录后默认操作的数据库 stacksize=1048576 server_version="5.5.30" connect_timeout_server=3000 monitor_history=600000 monitor_connect_interval=60000 monitor_ping_interval=10000 monitor_read_only_interval=1500 monitor_read_only_timeout=500 ping_interval_server=120000 ping_timeout_server=500 commands_stats=true sessions_sort=true connect_retries_on_failure=10 } mysql_servers = # 每一组花括号记录一台服务器,用逗号隔开,再记录下一台服务器 ( # 要有括号,不然无法启动,在message日志中会提示语法错误 { address = "192.168.1.14" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain port = 3306 # no default, required . If port is 0 , address is interpred as a Unix Socket Domain hostgroup = 0 # no default, required # 所处的主机组,将读定义一组,写定义一组 status = "ONLINE" # default: ONLINE # 配置完以后,这个服务器默认是在线的还是离线的 weight = 1 # default: 1 # 权重 compression = 0 # default: 0 # 压缩 max_connections = 200 # 最大并发连接数 # max_replication_lag = 10 # 读服务器是否延迟,这里默认是10秒 }, { address = "192.168.1.15" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain port = 3306 # no default, required . If port is 0 , ddress is interpred as a Unix Socket Domain hostgroup = 1 # no default, required status = "ONLINE" # default: ONLINE weight = 1 # default: 1 compression = 0 # default: 0 max_connections = 500 }, { address = "192.168.1.13" # no default, required . If port is 0 , address is interpred as a Unix Socket Domain port = 3306 # no default, required . If port is 0 ,ddress is interpred as a Unix Socket Domain hostgroup = 1 # no default, required status = "ONLINE" # default: ONLINE weight = 1 # default: 1 compression = 0 # default: 0 max_connections = 500 } ) mysql_users: # 定义以哪个用户组的身份连接至哪台服务器上 ( { username = "myadmin" # no default , required password = "mypass" # default: '' default_hostgroup = 0 # default: 0 # 默认连接哪个组 active = 1 # default: 1 default_schema="mydb" # 连接后默认使用的数据库 } ) mysql_query_rules: # 语句路由,实际是一个防火墙,可以屏蔽一些语句 ( ) scheduler= # 调度器 ( ) mysql_replication_hostgroups= # 指明哪个组读,哪个组写。可以同时调度多个集群 ( { writer_hostgroup=0 reader_hostgroup=1 comment="test repl 1" # 说明 } ) [root@test ~]# systemctl start proxysql # 启动 [root@test ~]# ss -tln State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 127.0.0.1:6032 *:* LISTEN 0 128 *:6033 *:* LISTEN 0 128 *:6033 *:* LISTEN 0 128 *:6033 *:* LISTEN 0 128 *:6033 *:* [root@test ~]# yum install -y mariadb # 安装客户端 [root@test ~]# mysql -h192.168.1.10 -P6033 -umyadmin -pmypass Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.5.30 (ProxySQL) # 连接后提示连接的是"Server version: 5.5.30 (ProxySQL)" MySQL [(none)]> SHOW TABLES; +----------------+ | Tables_in_mydb | +----------------+ | tbl1 | | tbl2 | +----------------+ MySQL [(none)]> CREATE TABLE tbl3(id INT); MySQL [(none)]> SHOW TABLES; +----------------+ | Tables_in_mydb | +----------------+ | tbl1 | | tbl2 | | tbl3 | +----------------+
* 两台从节点 MariaDB [mydb]> SHOW TABLES; +----------------+ | Tables_in_mydb | +----------------+ | tbl1 | | tbl2 | | tbl3 | +----------------+ # 两个从节点都有tbl3表了。这说明上面的创建语句被路由到了主节点上,因为如果路由到了从节点上,那么也只能有一个从服务器有此表,如果两个都有,说明是复制主节点的
* 主节点192.168.1.14 MariaDB [(none)]> SHOW PROCESSlist; +------+----------+--------------------+------+-------------+-------+-----------------------------------------------------------------------+------------------+----------+ | Id | User | Host | db | Command | Time | State | Info | Progress | +------+----------+--------------------+------+-------------+-------+-----------------------------------------------------------------------+------------------+----------+ | 17 | repluser | 192.168.1.15:51118 | NULL | Binlog Dump | 15388 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL | 0.000 | | 21 | repluser | 192.168.1.13:41342 | NULL | Binlog Dump | 14775 | Master has sent all binlog to slave; waiting for binlog to be updated | NULL | 0.000 | | 6183 | root | localhost | NULL | Query | 0 | NULL | SHOW PROCESSlist | 0.000 | +------+----------+--------------------+------+-------------+-------+-----------------------------------------------------------------------+------------------+----------+ # 可以看到有几个客户端连上来了 MariaDB [(none)]> USE mydb MariaDB [mydb]> SHOW TABLES; +----------------+ | Tables_in_mydb | +----------------+ | tbl1 | | tbl2 | | tbl3 | +----------------+ # 主节点上也有tbl3表 [root@test ~]# tcpdump -i ens33 -nn -vv port 3306 # 监听3306端口,这时可以看到很多信息,是分离器的ping探测。在两个从节点和主节点上做此操作
* 分离器192.168.1.10 [root@test ~]# mysql -uadmin -padmin -hlocalhost -S /tmp/proxysql_admin.sock Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 5 Server version: 5.5.30 (ProxySQL Admin Module) MySQL [(none)]> SHOW DATABASES; +-----+---------+-------------------------------+ | seq | name | file | +-----+---------+-------------------------------+ | 0 | main | | | 2 | disk | /var/lib/proxysql/proxysql.db | | 3 | stats | | | 4 | monitor | | +-----+---------+-------------------------------+ MySQL [(none)]> USE monitor MySQL [monitor]> SHOW TABLES; +--------------------------------------+ | tables | +--------------------------------------+ | global_variables | | mysql_collations | | mysql_query_rules | | mysql_replication_hostgroups | | mysql_servers | | mysql_users | | runtime_global_variables | | runtime_mysql_query_rules | | runtime_mysql_replication_hostgroups | | runtime_mysql_servers | | runtime_mysql_users | | runtime_scheduler | | scheduler | +--------------------------------------+ MySQL [monitor]> SELECT * FROM mysql_users; +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+ | username | password | active | use_ssl | default_hostgroup | default_schema | schema_locked | transaction_persistent | fast_forward | backend | frontend | max_connections | +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+ | myadmin | mypass | 1 | 0 | 0 | mydb | 0 | 0 | 0 | 1 | 1 | 10000 | +----------+----------+--------+---------+-------------------+----------------+---------------+------------------------+--------------+---------+----------+-----------------+ # 只要向这个表中加入数据,定义好信息,就可以实现运行时修改主从节点的主机了 MySQL [monitor]> SELECT * FROM runtime_mysql_replication_hostgroups; +------------------+------------------+-------------+ | writer_hostgroup | reader_hostgroup | comment | +------------------+------------------+-------------+ | 0 | 1 | test repl 1 | +------------------+------------------+-------------+ # 显示读写组的编号 MySQL [monitor]> select * from mysql_servers; +--------------+--------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | hostgroup_id | hostname | port | status | weight | compression | max_connections | max_replication_lag | use_ssl | max_latency_ms | comment | +--------------+--------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ | 0 | 192.168.1.14 | 3306 | ONLINE | 1 | 0 | 200 | 0 | 0 | 0 | | | 1 | 192.168.1.15 | 3306 | ONLINE | 1 | 0 | 500 | 0 | 0 | 0 | | | 1 | 192.168.1.13 | 3306 | ONLINE | 1 | 0 | 500 | 0 | 0 | 0 | | +--------------+--------------+------+--------+--------+-------------+-----------------+---------------------+---------+----------------+---------+ MySQL [monitor]> UPDATE mysql_servers SET hostgroup_id=0 WHERE hostname='192.168.1.13'; # 其他库也支持运行时修改
|