update product set owner_member_id='d' where owner_member_id='a';
,执行之后,日志中记录的不是这条update语句所对应的事件(mysql是以事件的形式来记录bin-log日志),而是这条语句所更新的每一条记录的变化情况,这样就记录成很多条记录被更新的很多事件。自然,bin-log日志的量会很大。mysql> show variables like 'binlog_format';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW |
+---------------+-------+
1 row in set (0.00 sec)
mysql> set binlog_format=STATEMENT;
Query OK, 0 rows affected (0.00 sec)
mysql> show variables like 'binlog_format';
+---------------+-----------+
| Variable_name | Value |
+---------------+-----------+
| binlog_format | STATEMENT |
+---------------+-----------+
1 row in set (0.00 sec)
mysqlbinlog --base64-output=decode-rows -v -v /var/lib/mysql/mysql-bin.000001 > binlog
systemctl stop iptables(需要安装 iptables 服务)
systemctl stop firewalld(默认)
systemctl disable firewalld.service(设置开机不启动)
mysql> show variables like 'log_bin%';
+---------------------------------+--------------------------------+
| Variable_name | Value |
+---------------------------------+--------------------------------+
| log_bin | ON |
| log_bin_basename | /var/lib/mysql/mysql-bin |
| log_bin_index | /var/lib/mysql/mysql-bin.index |
| log_bin_trust_function_creators | OFF |
| log_bin_use_v1_row_events | OFF |
+---------------------------------+--------------------------------+
5 rows in set (0.01 sec)
[mysqld]
# 启用二进制日志
log-bin = mysql-bin
# 服务器唯一ID,一般取IP最后一段
serverid = 128
systemctl restart mysqld
mysql> GRANT REPLICATION SLAVE ON *.* TO '从机MySQL用户名'@'从机IP' identified by '从机MySQL密码';
例如:
grant replication slave on *.* to 'root'@'%' identified by '123456';
-- 注意事项
-- 一般不用root账号,“%”表示所有客户端都可能连,只要账号、密码正确,此处可用具体客户端IP代替,如:192.168.0.102,加强安全
mysql5.7对密码的强度是有要求的,必须是字母+数字+符号组成的,> 可以使用如下方法调整密码强度
设置密码长度最低位数:mysql> set global validate_password_length=4;
设置密码强度级别:mysql> set global validate_password_policy=0;
validate_password_policy有以下取值:
Policy | Tests Performe |
---|---|
0 or LOW | Length |
1 or MEDIUM | numeric, lowercase/uppercase, and special characters |
2 or STRONG | Length; numeric, lowercase/uppercase, and special characters |
默认是1,即MEDIUM,所以刚开始设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符。
mysql> FLUSH PRIVILEGES;
mysql> show master status;
[mysqld]
server-id=135
Fatal error: The slave I/O thread stops beacause master and slave has equal MySQL sercer UUIDs; these UUIDs must be different for replication to work.
mysql> change master to
master_host='192.168.254.128',
master_port=3306,
master_user='root',
master_password='yw@910714',
master_log_file='mysql-bin.000001',
master_log_pos=397;
mysql> start slave;
mysql> show slave status \G;
说明:Slave_IO_Running 和 Slave_SQL_Running进程必须正常运行,即 YES 状态,否则都是错误的状态,以上操作过程,从服务器配置完成。
为什么要有读写分离集群?
名词解释:
注意事项:
* MySQL master:192.168.254.128:3306
* MySQL slave:192.168.254.130:3306
* MySQL proxy:192.168.254.132:4040
* MySQL router:192.168.254.132
wget https://downloads.mysql.com/archives/get/file/mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz
[root@localhost ~]# tar -xf mysql-proxy-0.8.5-linux-el6-x86-64bit.tar.gz -C .
# 修改下名字
[root@localhost ~]# mv mysql-proxy-0.8.5-linux-el6-x86-64bit mysql-proxy
[mysql-proxy]
user=root
admin-username=root
admin-password=123456
proxy-address=192.168.254.132:4040
proxy-backend-addresses=192.168.254.128:3306
proxy-read-only-backend-addresses=192.168.254.130:3306
proxy-lua-script=/usr/apps/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua
log-file=/usr/apps/mysql-proxy/logs/mysql-proxy.log
log-level=debug
keepalive=true
daemon=true
chmod 660 mysql-proxy.cnf
nohup ./mysql-proxy --defaults-file=mysql-proxy.cnf > mysql-proxy.out 2>&1 &
mysql -uroot -p123456 -h192.168.254.132 -P4040
wget https://cdn.mysql.com//Downloads/MySQL-Router/mysql-router-8.0.20-el7-x86_64.tar.gz
tar -zxvf mysql-router-8.0.20-el7-x86_64.tar.gz -C /usr/apps/
[logger]
level = INFO
[routing:secondary]
bind_address = 192.168.254.132
bind_port = 7001
destinations = 192.168.254.129:3306,192.168.254.130:3306
routing_strategy = round-robin
[routing:primary]
bind_address = 192.168.254.132
bind_port = 7002
destinations = 192.168.254.128:3306
routing_strategy = first-available
./mysqlrouter -c mysqlrouter.conf &
[root@centos130 apps]# mysql -uroot -p123456 -h192.168.254.132 -P7001 --protocol=tcp -e"select @@hostname"
Warning: Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| centos129 |
+------------+
[root@centos130 apps]# mysql -uroot -p123456 -h192.168.254.132 -P7001 --protocol=tcp -e"select @@hostname"
Warning: Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| centos130 |
+------------+
[root@centos130 apps]# mysql -uroot -p123456 -h192.168.254.132 -P7002 --protocol=tcp -e"select @@hostname"
Warning: Using a password on the command line interface can be insecure.
+------------+
| @@hostname |
+------------+
| centos128 |
+------------+
wget https://github.com/Qihoo360/Atlas/releases/download/2.2.1/Atlas-2.2.1.el6.x86_64.rpm
rpm -ivh Atlas-2.2.1.el6.x86_64.rpm
[root@centos132 tools]# ll /usr/local/mysql-proxy/
总用量 4
drwxr-xr-x. 2 root root 75 9月 30 21:45 bin
drwxr-xr-x. 2 root root 22 9月 30 21:45 conf
drwxr-xr-x. 3 root root 4096 9月 30 21:46 lib
drwxr-xr-x. 2 root root 6 12月 17 2014 log
[root@centos132 bin]# ./encrypt 123456
/iZxz+0GRoA=
[root@centos132 conf]# cd /usr/local/mysql-proxy/conf/
[root@centos132 conf]# vim test.cnf
#管理接口的用户名
admin-username = admin
#管理接口的密码
admin-password = admin
#Atlas后端连接的MySQL主库的IP和端口,可设置多项,用逗号分隔
proxy-backend-addresses = 192.168.254.128:3306
#Atlas后端连接的MySQL从库的IP和端口,@后面的数字代表权重,用来作负载均衡,若省略则默认为1,可设置多项,用逗号分隔
proxy-read-only-backend-addresses = 192.168.254.129:3306@1,192.168.254.130:3306@2
#用户名与其对应的加密过的MySQL密码,密码使用PREFIX/bin目录下的加密程序encrypt加密,下行的user1和user2为示例,将其替换为你的MySQL的用户名和加密密码!
# pwds = user1:+jKsgB3YAG8=, user2:GS+tr4TPgqc=
pwds = root:/iZxz+0GRoA=
#Atlas监听的工作接口IP和端口
proxy-address = 0.0.0.0:1234
#Atlas监听的管理接口IP和端口
admin-address = 0.0.0.0:2345
[root@centos132 bin]# ./mysql-proxyd test start
OK: MySQL-Proxy of test is started
[root@centos132 bin]# ./mysql-proxyd test stop
[root@centos130 apps]# mysql -uadmin -padmin -h192.168.254.132 -P2345
mysql> select * from help \G;
*************************** 1. row ***************************
command: SELECT * FROM help
description: shows this help
*************************** 2. row ***************************
command: SELECT * FROM backends
description: lists the backends and their state
*************************** 3. row ***************************
command: SET OFFLINE $backend_id
description: offline backend server, $backend_id is backend_ndx's id
*************************** 4. row ***************************
command: SET ONLINE $backend_id
description: online backend server, ...
*************************** 5. row ***************************
command: ADD MASTER $backend
description: example: "add master 127.0.0.1:3306", ...
*************************** 6. row ***************************
command: ADD SLAVE $backend
description: example: "add slave 127.0.0.1:3306", ...
*************************** 7. row ***************************
command: REMOVE BACKEND $backend_id
description: example: "remove backend 1", ...
*************************** 8. row ***************************
command: SELECT * FROM clients
description: lists the clients
*************************** 9. row ***************************
command: ADD CLIENT $client
description: example: "add client 192.168.1.2", ...
*************************** 10. row ***************************
command: REMOVE CLIENT $client
description: example: "remove client 192.168.1.2", ...
*************************** 11. row ***************************
command: SELECT * FROM pwds
description: lists the pwds
*************************** 12. row ***************************
command: ADD PWD $pwd
description: example: "add pwd user:raw_password", ...
*************************** 13. row ***************************
command: ADD ENPWD $pwd
description: example: "add enpwd user:encrypted_password", ...
*************************** 14. row ***************************
command: REMOVE PWD $pwd
description: example: "remove pwd user", ...
*************************** 15. row ***************************
command: SAVE CONFIG
description: save the backends to config file
*************************** 16. row ***************************
command: SELECT VERSION
description: display the version of Atlas
16 rows in set (0.00 sec)
[root@centos130 apps]# mysql -uroot -p123456 -h192.168.254.132 -P1234
mysql -uroot -p123456 -h192.168.254.132 -P1234 --protocol=tcp -e"select @@hostname"