數據讀寫分離
MySQL讀寫分離
概念:把客戶端查詢數據的請求和存儲數據的SQL命令,分別給不同的數據庫服務器處理
讀寫分離的原理
- 由MySQL代理面向客戶端提供服務
- 收到SQL的寫請求時,交給master服務器處理
- 收到SQL的讀請求時,交給slave服務器處理
讀寫分離拓撲圖
部署maxscale服務
Maxscale代理軟件
- 由MariaDB公司開發
- 下載地址
- 軟件包在此提取碼:5x5c
構建MySQL讀寫分離
部署MySQL一主一從結構
- 主服務器 192.168.4.51
- 從服務器 192.168.4.52
部署MySQL代理服務器
測試配置
1.配置MySQL主從同步:host52數據庫服務器作為host51的從服務器host51主機:
[root@host51 ~
]
[mysqld
]
server_id
=51
log_bin
=master51
[root@host53 mysql
]
[root@host51 ~
]
mysql
> grant replication slave on *.* to repluser@
"%" identified by
"123qqq...A";
Query OK, 0 rows affected, 1 warning
(0.01 sec
)
mysql
> show master status
;
+-----------------+----------+--------------+------------------+-------------------+
| File
| Position
| Binlog_Do_DB
| Binlog_Ignore_DB
| Executed_Gtid_Set
|
+-----------------+----------+--------------+------------------+-------------------+
| master51.000003
| 365
| | | |
+-----------------+----------+--------------+------------------+-------------------+
1 row
in set (0.00 sec
)host52主機:
[root@host52 ~
][mysqld
]server_id
=52
[root@host54 ~
]
[root@host52 ~
]mysql
> change master to-
> master_host
="192.168.4.51",-
> master_user
="repluser",-
> master_password
="123qqq...A",-
> master_log_file
="master51.000003",-
> master_log_pos
=365
;
Query OK, 0 rows affected, 2 warnings
(0.08 sec
)mysql
> start slave
;
Query OK, 0 rows affected
(0.01 sec
)mysql
> show slave status \G
*************************** 1. row ***************************Slave_IO_State: Waiting
for master to send eventMaster_Host: 192.168.4.51Master_User: repluserMaster_Port: 3306Connect_Retry: 60Master_Log_File: master51.000003Read_Master_Log_Pos: 154Relay_Log_File: host52-relay-bin.000011Relay_Log_Pos: 365Relay_Master_Log_File: master51.000003Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 154Relay_Log_Space: 6474Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_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: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 51Master_UUID: 6d7b632c-4e3f-11ea-8a8d-000c29875030Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has
read all relay log
; waiting
for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row
in set (0.00 sec
)此時做初步測試:首先在host51主機上查看授權用戶的權限
mysql
> show grants
for repluser@
"%";
+--------------------------------------------------+
| Grants
for repluser@%
|
+--------------------------------------------------+
| GRANT REPLICATION SLAVE ON *.* TO
'repluser'@
'%' |
+--------------------------------------------------+
1 row
in set (0.00 sec
)mysql
> create database aaa
;
Query OK, 1 row affected
(0.06 sec
)mysql
> create table aaa.b
(name char
(10
));
Query OK, 0 rows affected
(0.24 sec
)mysql
> insert into aaa.b values
("zzz");
Query OK, 1 row affected
(0.11 sec
)mysql
> select * from aaa.b
;
+------+
| name
|
+------+
| zzz
|
+------+
1 row
in set (0.00 sec
)在host52主機上連接數據庫服務器,查看是否有剛剛寫入的數據
mysql
> select * from aaa.b
;
+------+
| name
|
+------+
| zzz
|
+------+
1 row
in set (0.00 sec
)*****
環境要求:關閉防火墻,保證yum源可以正常使用,安裝提供服務的軟件
配置讀寫分離服務器192.168.4.57,具體操作如下:1.安裝軟件maxscale-2.1.2-1.rhel.7.x86_64.rpm
[root@host57 ~
]
提供服務程序和管理命令
[root@host57 ~
]
maxadmin maxbinlogcheck maxpasswd
maxavrocheck maxkeys maxscale 2.修改配置文件
[root@host57 ~
]
/etc/maxscale.cnf
[root@host57 ~
][root@host57 ~
]9
[maxscale
] 10 threads
=auto
...18
[server1
] 19 type
=server20 address
=192.168.4.51 21 port
=330622 protocol
=MySQLBackend23 24
[server2
] 25 type
=server26 address
=192.168.4.52 27 port
=330628 protocol
=MySQLBackend35
[MySQL Monitor
] 36 type
=monitor37 module
=mysqlmon38 servers
=server1,server2 39 user
=maxscalemon 40 passwd
=123qqq
...A 41 monitor_interval
=1000052 53 54 55 56 57 58 63
[Read-Write Service
] 64 type
=service65 router
=readwritesplit66 servers
=server1,server2 67 user
=maxscalerouter 68 passwd
=123qqq
...A 69 max_slave_connections
=100%75
[MaxAdmin Service
] 76 type
=service77 router
=cli85 86 87 88 89 91
[Read-Write Listener
] 92 type
=listener93 service
=Read-Write Service94 protocol
=MySQLClient95 port
=400697
[MaxAdmin Listener
] 98 type
=listener99 service
=MaxAdmin Service
100 protocol
=maxscaled
101 socket
=default
102 port
=4016
[root@host57 ~
]3.添加授權用戶根據maxscale.cnf文件配置,在主、從服務器上添加對應的授權用戶,因為兩臺數據庫服務器是主從同步結構,只在主數據庫服務器添加用戶即可,從服務器會自動同步mysql
> grant replication slave ,replication client on *.* to maxscalemon@
"%" identified by
"123qqq...A";
Query OK, 0 rows affected, 1 warning
(0.07 sec
)mysql
> grant
select on mysql.* to maxscalerouter@
"%" identified by
"123qqq...A";
Query OK, 0 rows affected, 1 warning
(0.00 sec
)4.查看授權用戶
mysql
> select host , user from mysql.user where user
in ("maxscalerouter",
"maxscalemon");
+------+----------------+
| host
| user
|
+------+----------------+
| %
| maxscalemon
|
| %
| maxscalerouter
|
+------+----------------+
2 rows
in set (0.08 sec
)在代理服務器57主機上,測試授權用戶
[root@host57 ~
]
[root@host57 ~
]
[root@host57 ~
]
[root@host57 ~
]
[root@host57 ~
]5.啟動maxscale代理服務
[root@host57 ~
]
[root@host57 ~
]
tcp6 0 0 :::4006 :::* LISTEN 11249/maxscale
tcp6 0 0 :::4016 :::* LISTEN 11249/maxscale
[root@host57 ~
]PID TTY TIME CMD11249 ? 00:00:00 maxscale測試配置1.查看監控信息(在主機57本機訪問自己)
[root@host57 ~
]
MaxScale
> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server
| Address
| Port
| Connections
| Status
-------------------+-----------------+-------+-------------+--------------------
server1
| 192.168.4.51
| 3306
| 0
| Master, Running
server2
| 192.168.4.52
| 3306
| 0
| Slave, Running
-------------------+-----------------+-------+-------------+--------------------2.在主服務器上添加授權連接用戶
在主服務器上添加即可,從服務器會自動同步
[root@host50 ~
]
/usr/bin/mysql
[root@host50 ~
]
mariadb-5.5.56-2.el7.x86_64
[root@host50 ~
]
ERROR 1045
(28000
): Access denied
for user
'jim'@
'::ffff:192.168.4.50' (using password: YES
)
在主服務器添加授權用戶給客戶端連接使用
客戶端使用授權用戶連接
mysql
> create database db6
;
Query OK, 1 row affected
(0.00 sec
)mysql
> create table db6.user
(name char
(15
));
Query OK, 0 rows affected
(0.06 sec
)mysql
> grant
select ,insert on db6.* to jim@
"%" identified by
"123qqq...A";
Query OK, 0 rows affected, 1 warning
(0.00 sec
)mysql
> desc db6.user
;
+-------+----------+------+-----+---------+-------+
| Field
| Type
| Null
| Key
| Default
| Extra
|
+-------+----------+------+-----+---------+-------+
| name
| char
(15
) | YES
| | NULL
| |
+-------+----------+------+-----+---------+-------+
1 row
in set (0.00 sec
)mysql
> select user ,host from mysql.user
;
+----------------+--------------+
| user
| host
|
+----------------+--------------+
| haha
| %
|
| jim
| %
|
| maxscalemon
| %
|
| maxscalerouter
| %
|
| repluser
| %
|
| webadmin
| %
|
| admin
| 192.168.4.%
|
| root
| 192.168.4.52
|
| admin2
| localhost
|
| mysql.sys
| localhost
|
| root
| localhost
|
| tian
| localhost
|
+----------------+--------------+
12 rows
in set (0.00 sec
)在host52主機上面查看mysql
> desc db6.user
;
+-------+----------+------+-----+---------+-------+
| Field
| Type
| Null
| Key
| Default
| Extra
|
+-------+----------+------+-----+---------+-------+
| name
| char
(15
) | YES
| | NULL
| |
+-------+----------+------+-----+---------+-------+
1 row
in set (0.00 sec
)在host50客戶端訪問代理服務器,插入數據可以成功查看
[root@host50 ~
]
MySQL
[(none
)]> insert into db6.user values
("bob");
Query OK, 1 row affected
(0.01 sec
)MySQL
[(none
)]> select * from db6.user
;
+------+
| name
|
+------+
| bob
|
+------+
1 row
in set (0.01 sec
)在host51以及host52主機也可以看到插入的數據
mysql
> select * from db6.user
;
+------+
| name
|
+------+
| bob
|
+------+
1 row
in set (0.00 sec
)
MySQL多實例
多實例概述
配置多實例
環境準備:
在192.168.4.57主機上配置,要提前安裝軟件包mysql-5.7.20可以支持配置多實例
軟件包在此!提取碼:iptb
[root@host57 ~
]
mysql-5.7.20-linux-glibc2.12-x86_64.tar.gz
[root@host57 ~
][root@host57 ~
]
bin COPYING docs include lib
man README share support-files
[root@host57 ~
][root@host57 ~
][root@host57 mysql
]
bin(SQL命令) COPYING docs(幫助文檔,二進制) include(源碼調用程序文件) lib(源碼調用模塊) man(幫助文檔) README share(幫助文檔) support-files(配置文件模板)
[root@host57 mysql
][root@host57 mysql
]
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@host57 mysql
]
mysql mysqld_multi mysql_secure_installation
mysqladmin mysqld_safe mysqlshow
mysqlbinlog mysqldump mysqlslap
mysqlcheck mysqldumpslow mysql_ssl_rsa_setup
mysql_client_test_embedded mysql_embedded mysqltest_embedded
mysql_config mysqlimport mysql_tzinfo_to_sql
mysql_config_editor mysql_install_db mysql_upgrade
mysqld mysql_plugin mysqlxtest
mysqld-debug mysqlpump
[root@host57 mysql
]
export PATH
=/usr/local/mysql/bin:
$PATH[root@host57 ~
][root@host57 ~
]創建并編寫主配置文件 /etc/my.cnf多實例服務的運行配置
如果原本有下載mariadb或者安裝過mysql軟件就會產生/etc/my.cnf,要先刪除
[root@host57 ~
]socket文件:當自己訪問自己時,通過socket文件傳遞數據,是訪問mysql接口程序的文件,套接字文件,虛擬文件,服務啟動時有該文件,服務關閉時文件即消失
[root@host57 ~
]
[mysqld_multi
]
mysqld
=/usr/local/mysql/bin/mysqld_safe
mysqladmin
=/usr/local/mysql/bin/mysqladminer
=root
user
=root
[mysqld1
]
datadir
=/dir1
log-error
=/dir1/mysqld1.error
socket
=/dir1/mysqld1.sock
pid-file
=/dir1/mysqld1.pid
port
=3307
[mysqld2
]
datadir
=/dir2
log-error
=/dir2/mysqld2.error
socket
=/dir2/mysqld2.sock
pid-file
=/dir2/mysqld2.pid
port
=3308
[root@host57 ~
]
[root@host57 ~
]啟動服務(首次啟動服務會創建root登錄的初始密碼)
[root@host57 ~
][root@host57 ~
]
[root@host57 ~
]
tcp LISTEN 0 80 :::3307 :::* users:
(("mysqld",pid
=11365,fd
=20
))[root@host57 ~
]PID TTY TIME CMD11365 pts/0 00:00:00 mysqld
[root@host57 ~
]
auto.cnf ib_logfile0 mysql mysqld1.sock sys
ib_buffer_pool ib_logfile1 mysqld1.error mysqld1.sock.lock
ibdata1 ibtmp1 mysqld1.pid performance_schema
[root@host57 ~
]
mysql
> alter user root@
"localhost" identified by
"123456"; [root@host57 ~
]
mysql
> show databases
;
+--------------------+
| Database
|
+--------------------+
| information_schema
|
| mysql
|
| performance_schema
|
| sys
|
+--------------------+
4 rows
in set (0.00 sec
)[root@host57 ~
][root@host57 ~
][root@host57 ~
]
auto.cnf ib_logfile0 mysql mysqld2.sock.lock
ib_buffer_pool ib_logfile1 mysqld2.error performance_schema
ibdata1 ibtmp1 mysqld2.sock sys
[root@host57 ~
]
tcp LISTEN 0 80 :::3308 :::* users:
(("mysqld",pid
=11734,fd
=20
))[root@host57 ~
]PID TTY TIME CMD11365 pts/0 00:00:01 mysqld11734 pts/0 00:00:00 mysqld
[root@host57 ~
]
mysql
> alter user root@
"localhost" identified by
"123456";
Query OK, 0 rows affected
(0.03 sec
)[root@host57 ~
]
mysql
> show databases
;
+--------------------+
| Database
|
+--------------------+
| information_schema
|
| mysql
|
| performance_schema
|
| sys
|
+--------------------+
4 rows
in set (0.00 sec
)
如果啟動服務而且有端口號但無法進入數據庫,可以進行如下操作:
rm -rf /dir1/*
killall -9 mysqld
檢查配置文件/etc/my.cnf
再次重新啟動服務mysqld_multi start 1
停止服務(如果停止不了服務就殺掉進程)
[root@host57 ~
]
tcp LISTEN 0 80 :::3307 :::* users:
((mysqld
",pid=11365,fd=20))
tcp LISTEN 0 80 :::3308 :::* users:((mysqld",pid
=11734,fd
=20
))[root@host57 ~
][root@host57 ~
]
tcp LISTEN 0 80 :::3308 :::* users:
((mysqld
",pid=11734,fd=20))[root@host57 ~]# mysqld_multi start 1 #再次啟動[root@host57 ~]# mysql -uroot -p123456 -S /dir1/mysqld1.sock #此時再次啟動時直接用修改后的密碼登錄即可
[root@host57 ~]# ss -nutlp | grep mysqld
tcp LISTEN 0 80 :::3307 :::* users:((mysqld",pid
=11945,fd
=28
))
tcp LISTEN 0 80 :::3308 :::* users:
((mysqld
",pid=11734,fd=20))添加實例3.自定義運行參數[root@host57 ~]# vim /etc/my.cnf
[mysqld_multi]
mysqld=/usr/local/mysql/bin/mysqld_safe
mysqladmin=/usr/local/mysql/bin/mysqladmin
user=root[mysqld1]
datadir=/dir1
log-error=/dir1/mysqld1.error
socket=/dir1/mysqld1.sock
pid-file=/dir1/mysqld1.pid
port=3307[mysqld2]
datadir=/dir2
log-error=/dir2/mysqld2.error
socket=/dir2/mysqld2.sock
pid-file=/dir2/mysqld2.pid
port=3308[mysqld3]
datadir=/dir3
log-error=/dir3/mysqld3.error
socket=/dir3/mysqld3.sock
pid-file=/dir3/mysqld3.pid
port=3309[root@host57 ~]#mkdir /dir3
[root@host57 ~]# ls /dir3
[root@host57 ~]# mysqld_multi start 3
[root@host57 ~]# ls /dir3
auto.cnf ib_logfile0 mysql mysqld3.sock sys
ib_buffer_pool ib_logfile1 mysqld3.error mysqld3.sock.lock
ibdata1 ibtmp1 mysqld3.pid performance_schema
[root@host57 ~]# ss -nutlp | grep mysqld
tcp LISTEN 0 80 :::3307 :::* users:((mysqld",pid
=11945,fd
=28
))
tcp LISTEN 0 80 :::3308 :::* users:
((mysqld
",pid=11734,fd=20))
tcp LISTEN 0 80 :::3309 :::* users:((mysqld",pid
=12140,fd
=20
))[root@host57 ~
]
mysql
> alter user root@
"localhost" identified by
"123456";
[root@host57 ~
]允許客戶端50可以連接57主機的實例1服務 ,對db1庫有完全權限,且實例1可以記錄用戶執行的SQL命令
[root@host57 ~
][mysqld1
]
server_id
=1
log_bin
=mysqld1
datadir
=/dir1
log-error
=/dir1/mysqld1.error
socket
=/dir1/mysqld1.sock
pid-file
=/dir1/mysqld1.pid
port
=3307
[root@host57 ~
]
[root@host57 ~
]
[root@host57 ~
]
mysql
> show master status
;
+----------------+----------+--------------+------------------+-------------------+
| File
| Position
| Binlog_Do_DB
| Binlog_Ignore_DB
| Executed_Gtid_Set
|
+----------------+----------+--------------+------------------+-------------------+
| mysqld1.000001
| 154
| | | |
+----------------+----------+--------------+------------------+-------------------+
1 row
in set (0.04 sec
)mysql
> grant all on db1.* to yaya@
"192.168.4.50" identified by
"123456";
Query OK, 0 rows affected, 1 warning
(0.07 sec
)mysql
> select user ,host from mysql.user
;
+---------------+--------------+
| user
| host
|
+---------------+--------------+
| yaya
| 192.168.4.50
|
| mysql.session
| localhost
|
| mysql.sys
| localhost
|
| root
| localhost
|
+---------------+--------------+
4 rows
in set (0.00 sec
)mysql
> show grants
for yaya@
"192.168.4.50" ;
+----------------------------------------------------------+
| Grants
for yaya@192.168.4.50
|
+----------------------------------------------------------+
| GRANT USAGE ON *.* TO
'yaya'@
'192.168.4.50' |
| GRANT ALL PRIVILEGES ON
`db1`.* TO
'yaya'@
'192.168.4.50' |
+----------------------------------------------------------+
2 rows
in set (0.00 sec
)[root@host50 ~
]
MySQL
[(none
)]> show grants
;
+----------------------------------------------------------+
| Grants
for yaya@192.168.4.50
|
+----------------------------------------------------------+
| GRANT USAGE ON *.* TO
'yaya'@
'192.168.4.50' |
| GRANT ALL PRIVILEGES ON
`db1`.* TO
'yaya'@
'192.168.4.50' |
+----------------------------------------------------------+
2 rows
in set (0.00 sec
)MySQL
[(none
)]> create database db1
;
Query OK, 1 row affected
(0.02 sec
)MySQL
[(none
)]> create table db1.user
(name char
(10
));
Query OK, 0 rows affected
(0.02 sec
)MySQL
[(none
)]> insert into db1.user values
("aaa");
Query OK, 1 row affected
(0.04 sec
)MySQL
[(none
)]> select * from db1.user
;
+------+
| name
|
+------+
| aaa
|
+------+
1 row
in set (0.00 sec
)在host57主機查看
[root@host57 ~
]
mysql
> select * from db1.user
;
+------+
| name
|
+------+
| aaa
|
+------+
1 row
in set (0.00 sec
)
總結
以上是生活随笔為你收集整理的DBA(四):数据读写分离,MySQL多实例操作的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。