從庫生成兩個線程,一個I/O線程,一個SQL線程;
i/o線程去請求主庫 的binlog,并將得到的binlog日志寫到relay log(中繼日志) 文件中;
主庫會生成一個 log dump 線程,用來給從庫 i/o線程傳binlog;
SQL 線程,會讀取relay log文件中的日志,并解析成具體操作,來實現主從的操作一致,而最終數據一致;
規劃
MySQL01 |
192.168.226.127 |
mysql02 |
192.168.226.128 |
mysql03 |
192.168.226.129 |
mysql04 |
192.168.226.130 |
centos7.6
mysql-5.7.17.tar
mysql-proxy-0.8.5-linux-debian6.0-x86-64bit.tar.gz
mysql01 主 mysql02從 mysql03 讀寫分離 mysql04測試機
mysql搭建
1.解壓
[root@mysql01 ~]# ls
anaconda-ks.cfg mysql-5.7.17.tar
[root@mysql01 ~]# tar -xvf mysql-5.7.17.tar
./mysql-community-client-5.7.17-1.el7.x86_64.rpm
./mysql-community-common-5.7.17-1.el7.x86_64.rpm
./mysql-community-devel-5.7.17-1.el7.x86_64.rpm
./mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
./mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
./mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
./mysql-community-libs-5.7.17-1.el7.x86_64.rpm
./mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm
./mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
./mysql-community-server-5.7.17-1.el7.x86_64.rpm
./mysql-community-test-5.7.17-1.el7.x86_64.rpm
2.安裝
[root@mysql01 ~]# ls
anaconda-ks.cfg
mysql-5.7.17.tar
mysql-community-client-5.7.17-1.el7.x86_64.rpm #安裝
mysql-community-common-5.7.17-1.el7.x86_64.rpm #安裝
mysql-community-devel-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.17-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.17-1.el7.x86_64.rpm
mysql-community-libs-5.7.17-1.el7.x86_64.rpm #安裝
mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm #安裝
mysql-community-minimal-debuginfo-5.7.17-1.el7.x86_64.rpm
mysql-community-server-5.7.17-1.el7.x86_64.rpm #安裝
mysql-community-test-5.7.17-1.el7.x86_64.rpm
[root@mysql01 ~]#yum install -y mysql-community-client-5.7.17-1.el7.x86_64.rpm mysql-community-common-5.7.17-1.el7.x86_64.rpm mysql-community-libs-5.7.17-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.17-1.el7.x86_64.rpm mysql-community-server-5.7.17-1.el7.x86_64.rpm
3.啟動mysql服務
[root@mysql01 ~]# systemctl start mysqld
[root@mysql01 ~]# cat /var/log/mysqld.log | grep password
2022-03-11T13:48:31.592028Z 1 [Note] A temporary password is generated for root@localhost: +i2yM_qvqtxj
4 修改密碼,否則不能正常使用
[root@mysql01 ~]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: 初始密碼
The existing password for the user account root has expired. Please set a new password.
New password: 新密碼
Re-enter new password: 再次輸入新密碼
Re-enter new password:
The 'validate_password' plugin is installed on the server.
The subsequent steps will run with the existing configuration
of the plugin.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) :
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
Success.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
5 登錄mysql
[root@mysql01 ~]# mysql -uroot -p123qqq...A
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 6
Server version: 5.7.17 MySQL Community Server (GPL)
Copyright (c) 2000, 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.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
mysql>
主從搭建
mysql01和mysql02完成以上數據庫部署
1 mysql01主操作
[root@mysql01 ~]# vim /etc/my.cnf
log-bin=mysql-bin # 啟用二進制功能,主從復制基礎
server-id=1 # id唯一
[root@mysql01 ~]# systemctl restart mysqld
2 驗證配置是否生效
mysql> show variables like "server_id";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 1 | #為配置文件配置的server_id
+---------------+-------+
1 row in set (0.01 sec)
mysql> show variables like 'log_bin';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| log_bin | ON | #為on
+---------------+-------+
1 row in set (0.00 sec)
mysql> show variables like '%skip_networking%';
#skip_networking默認是OFF關閉狀態,啟用后主從將無法通信
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| skip_networking | OFF |
+-----------------+-------+
1 row in set (0.00 sec)
3 mysql02從服務器操作
[root@mysql02 ~]# vim /etc/my.cnf
server-id=3 添加
[root@mysql02 ~]# systemctl restart mysqld
4 驗證配置是否生效
mysql> show variables like "server_id";
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id | 3 |
+---------------+-------+
1 row in set (0.01 sec)
5 gitd實現主從同步
相比于傳統的主從復制優點:不需要知道復制哪個文件,也不需要知道從哪個號開始復制 ?
?5.1 mysql01?
[root@mysql01 ~]# vim /etc/my.cnf
gtid_mode =ON
enforce-gtid-consistency=true
[root@mysql01 ~]# systemctl restart mysqld
mysql> grant replication slave on *.* to repl@'192.168.226.%' identified by '123qqq...A';
Query OK, 0 rows affected, 1 warning (0.00 sec)
# 為服務器創建一個連接賬戶并授予權限,*.*表示所有權限;192.168.226.%表示這個網段的所有用戶都有這個權限
mysql> show master status;
+------------------+----------+--------------+------------------+----------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+----------------------------------------+
| mysql-bin.000002 | 449 | | | f02849a0-a141-11ec-a0a1-000c29a6904c:1 |
+------------------+----------+--------------+------------------+----------------------------------------+
1 row in set (0.00 sec)
5.2 mysql02
[root@mysql02 ~]# vim /etc/my.cnf
gtid_mode =ON
enforce-gtid-consistency=true
[root@mysql02 ~]# systemctl restart mysqld
mysql> change master to master_host='192.168.226.127',master_user='repl',master_password='123qqq...A',MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected, 2 warnings (0.09 sec)
# 改變slave服務器用于連接master服務器的參數,此處把MASTER值設為動態
mysql> start slave ;
mysql> show slave status G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.226.127
Master_User: repl
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000002
Read_Master_Log_Pos: 449
Relay_Log_File: mysql02-relay-bin.000002
Relay_Log_Pos: 662
Relay_Master_Log_File: mysql-bin.000002
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: 449
Relay_Log_Space: 871
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_UUID: f02849a0-a141-11ec-a0a1-000c29a6904c
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set: f02849a0-a141-11ec-a0a1-000c29a6904c:1
Executed_Gtid_Set: f02849a0-a141-11ec-a0a1-000c29a6904c:1
Auto_Position: 1
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
change master to master_host='192.168.226.127',master_user='repl',master_password='123qqq...A',master_log_file='mysql-bin.000002',master_log_pos=449;
不用gitd用這個命令
測試
mysql01
mysql> create database test;
Query OK, 1 row affected (0.00 sec)
mysql> use test;
mysql> create table tese01(name varchar(10) not null, age varchar(5) not null);
Query OK, 0 rows affected (0.03 sec)
mysql> insert into tese01 values("tom","18");
mysql> select * from tese01;
+------+-----+
| name | age |
+------+-----+
| tom | 18 |
+------+-----+
1 row in set (0.00 sec)
mysql02
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| test |
+--------------------+
5 rows in set (0.00 sec)
mysql> use test;
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_in_test |
+----------------+
| tese01 |
+----------------+
1 row in set (0.00 sec)
mysql> select * from tese01;
+------+-----+
| name | age |
+------+-----+
| tom | 18 |
+------+-----+
1 row in set (0.01 sec)
mysql-proxy安裝
mysql03
[root@mysql03 ~]# ls
anaconda-ks.cfg mysql-5.7.17.tar mysql-proxy-0.8.5-linux-debian6.0-x86-64bit.tar.gz
[root@mysql03 ~]# tar -zxvf mysql-proxy-0.8.5-linux-debian6.0-x86-64bit.tar.gz
[root@mysql03 ~]# mv mysql-proxy-0.8.5-linux-debian6.0-x86-64bit /usr/local/mysql-proxy
[root@mysql03 ~]# cd /usr/local/mysql-proxy/
[root@mysql03 mysql-proxy]# ls
bin include lib libexec licenses share
[root@mysql03 mysql-proxy]# mkdir conf logs
[root@mysql03 mysql-proxy]# cd conf/
[root@mysql03 conf]# vim mysql-proxy.conf
[mysql-proxy]
user=root 運行mysql-proxy用戶
proxy-address=0.0.0.0:3306
proxy-backend-addresses=172.25.44.1:3306 #指定后端主master寫入數據
proxy-read-only-backend-addresses=172.25.44.2:3306 #指定后端從slave讀取數據
proxy-lua-script=/usr/local/mysql-proxy/share/doc/mysql-proxy/rw-splitting.lua #指定讀寫分離配置文件位置
log-file=/usr/local/mysql-proxy/logs/mysql-proxy.log #日志位置
log-level=debug #定義log日志級別,由高到低分別有(error|warning|info|message|debug)
[root@mysql03 conf]#chmod 660 /usr/local/mysql-proxy/conf/mysql-proxy.conf
[root@mysql03 conf]#mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf
測試
mysql01
mysql> grant all privileges on *.* to root@'%' identified by'123qqq...A';
mysql04
[root@mysql04 ~]# yum install mysql -y
[root@mysql04 ~]# mysql -h 192.168.226.129 -uroot -p123qqq...A
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MySQL connection id is 14
Server version: 5.7.17-log MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.
MySQL [(none)]>
驗證
mysql01數據庫建庫建表,在mysql04上可以查看到。
mysql03自動抓包
[root@mysql03 conf]# mysql-proxy --defaults-file=/usr/local/mysql-proxy/conf/mysql-proxy.conf
server default db:
client default db: test
syncronizing
原文鏈接:
https://www.tuicool.com/articles/6jmyQvi