以两台Linux主机在docker中实现mysql主主备份以用nginx实现mysql高可用
使用nginx反向代理主主備份的兩臺(tái)mysql,連接時(shí)連接nginx,當(dāng)其中一臺(tái)myql停止后,仍然可以正常使用,如果使用k8s 會(huì)簡(jiǎn)單許多。所謂主主復(fù)制就是在主從復(fù)制的基礎(chǔ)上掉了個(gè)頭。
?請(qǐng)博主買(mǎi)塊糖吃吧??: 打開(kāi)支付寶首頁(yè)搜“585262065”領(lǐng)紅包,領(lǐng)到大紅包的小伙伴趕緊使用哦!
?
裝備了兩臺(tái)linux主機(jī)
在兩臺(tái)主機(jī)上均安裝了 docker?
在兩臺(tái)主機(jī)的docker上均安裝了nginx和mysql ,同時(shí)安裝了docker容器的可視化管理工具 portainer/portainer
首先實(shí)現(xiàn) mysql的主主備份
其中的??master_log_file? ? ?master_log_pos? 是通過(guò)查詢(xún)得到的
查詢(xún)命令為show master status
在這之前應(yīng)該打開(kāi)二進(jìn)制日志 配置好?
在配置工程中鳴謝 以下博客給予的幫助
主主復(fù)制相關(guān)博客
????? ?https://www.cnblogs.com/ygqygq2/p/6045279.html????2016
? ? ? ??https://www.cnblogs.com/zhenyuyaodidiao/p/4635458.html??MySQL Replication
???https://www.cnblogs.com/wclwcw/p/6281608.html
1 .docker中安裝mysql實(shí)現(xiàn)配置文件的掛載
//啟動(dòng)mysql5.6 容器 掛載配置文件和 數(shù)據(jù)存儲(chǔ)目錄 兩臺(tái)機(jī)子的docker可以運(yùn)行 docker run -d -p 3309:3306 --privileged=true \-v /docker/mysql/mysql-5.6/conf/mysql.conf.d:/etc/mysql/mysql.conf.d \ 掛載的配置目錄
-v /docker/mysql/mysql-5.6/data:/var/lib/mysql \ 掛載的數(shù)據(jù)存儲(chǔ)目錄
-e MYSQL_ROOT_PASSWORD=密碼 \
--name mysql56b mysql:5.6
?
[root@hd50 mysql.conf.d]# pwd /docker/mysql/mysql-5.6/conf/mysql.conf.d [root@hd50 mysql.conf.d]# ls mysqld.cnf [root@hd50 mysql.conf.d]# cat mysqld.cnf # Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved. # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; version 2 of the License. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA# # The MySQL Server configuration file. # # For explanations see # http://dev.mysql.com/doc/mysql/en/server-system-variables.html [mysqld] lower_case_table_names=1 server_id=2 # 另一臺(tái)機(jī)子為 1 這個(gè)為了標(biāo)志日志的來(lái)源 log_bin=/var/lib/mysql/mysql-bin binlog_format=mixed auto_increment_increment=2 # 自增步長(zhǎng) auto_increment_offset=2 # 另一臺(tái)機(jī)子為1 自增起始位置pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql #log-error = /var/log/mysql/error.log # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0[root@hd50 mysql.conf.d]#?
如何查詢(xún) 本機(jī)作為主節(jié)點(diǎn)的日志狀態(tài)
Last login: Fri Jan 25 19:33:22 2019 from 111.203.45.3-- 進(jìn)入容器中 [root@hd49 ~]# docker exec -it mysql56b /bin/bash
-- 進(jìn)入mysql命令行 root@1694afdf240c:/# mysql -uroot -p數(shù)據(jù)庫(kù)密碼 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 15 Server version: 5.6.43-log MySQL Community Server (GPL)Copyright (c) 2000, 2019, 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.
-- 查詢(xún)本機(jī)器作為 master的日志情況 mysql> show master status-> ; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000004 | 1201 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
-- 結(jié)尾用 \G 的話會(huì)更加清晰 mysql> show master status\G *************************** 1. row ***************************File: mysql-bin.000004Position: 1201Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 1 row in set (0.00 sec)mysql>
?
通過(guò)命令實(shí)現(xiàn)互為主從
-- 進(jìn)入到兩臺(tái)機(jī)器的 mysql容器的命令行界面--進(jìn)容器的 命令
docker exec -it 容器id /bin/bash
--通過(guò)一下命令
mysql -u用戶名 -p密碼 進(jìn)入到mysq的命令行界面
-- 在 A 機(jī)器上創(chuàng)建 B 機(jī)子可以登錄的賬戶 GRANT replication slave ON *.* TO '用戶名a'@'%' IDENTIFIED BY '密碼a'; flush privileges;-- 在 B 機(jī)器上創(chuàng)建 A 機(jī)器可以登錄的賬戶 GRANT replication slave ON *.* TO '用戶名b'@'%' IDENTIFIED BY '密碼b'; flush privileges; -- -- 給 A 安排master 他的主是 B change master to master_host='ip b',master_port=3309,master_user='用戶名b',master_password='密碼b',master_log_file='mysql-bin.000004',master_log_pos=408; -- 給 B 安排maser 他的主是 A
change master to master_host='ip a',master_port=3309,master_user='用戶名a',master_password='密碼b',master_log_file='mysql-bin.000004',master_log_pos=419;
查看配置成功后查看本機(jī)作為從節(jié)點(diǎn)的狀態(tài)
命令為? show slave status\G
mysql> show slave status\G *************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 主節(jié)點(diǎn)的ipMaster_User: sijiuMaster_Port: 3309Connect_Retry: 60Master_Log_File: mysql-bin.000004Read_Master_Log_Pos: 1326Relay_Log_File: mysqld-relay-bin.000002Relay_Log_Pos: 1201Relay_Master_Log_File: mysql-bin.000004Slave_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: 1326Relay_Log_Space: 1375Until_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: 2Master_UUID: b8378125-1faf-11e9-b17a-0242ac110003Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update itMaster_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: 0 1 row in set (0.00 sec)?
?啟動(dòng)nginx容器
啟動(dòng)命令為docker run -d -p 8091:80 -p 3307:3306 --name nginx_a nginx 其中nginx_a 為容器名
8091映射容器內(nèi)端口80
3307映射容器內(nèi)端口3306 3306為stream中的listen監(jiān)聽(tīng)端口
先進(jìn)入容器把配置文件拷貝出來(lái)? 把拷貝出的配置文件修改后替換掉容器內(nèi)的配置文件
采用以下命令 用外部的配置文件替換掉容器內(nèi)的
docker cp nginx.conf nginx_a:/etc/nginx/替換文件后進(jìn)入容器中 重啟nginx
nginx -s reloadnginx.conf的內(nèi)容如下
[root@hd50 nginx-1.15.8]# cat nginx.conf user nginx; worker_processes 1;error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid;events {worker_connections 1024; }http {include /etc/nginx/mime.types;default_type application/octet-stream;log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';access_log /var/log/nginx/access.log main;sendfile on;#tcp_nopush on;keepalive_timeout 65;#gzip on;include /etc/nginx/conf.d/*.conf; } # 相比原來(lái)添加了 stream模塊 stream {upstream mysql {server ip1:3309 weight=5;server ip2:3309 weight=5;}server {listen 3306 ;# 數(shù)據(jù)庫(kù)服務(wù)器監(jiān)聽(tīng)端口proxy_connect_timeout 10s;proxy_timeout 300s; # 設(shè)置客戶端和代理之間的超時(shí)時(shí)間,如果5分鐘內(nèi)沒(méi)有操作將自動(dòng)斷開(kāi)proxy_pass mysql;} }鳴謝
https://jingyan.baidu.com/article/0320e2c102fcc11b87507b17.html
https://www.cnblogs.com/trydoit/p/7129039.html
https://blog.csdn.net/woniu211111/article/details/80968154
https://www.cnblogs.com/tangxuliang/p/9341271.html
?請(qǐng)博主買(mǎi)塊糖吃吧? :? 打開(kāi)支付寶首頁(yè)搜“585262065”領(lǐng)紅包,領(lǐng)到大紅包的小伙伴趕緊使用哦!
下一步打算用zookeeper 實(shí)現(xiàn)一下
轉(zhuǎn)載于:https://www.cnblogs.com/huanglei2010/p/10321586.html
與50位技術(shù)專(zhuān)家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的以两台Linux主机在docker中实现mysql主主备份以用nginx实现mysql高可用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 华硕电脑怎么更换系统教程视频 华硕电脑系
- 下一篇: Redis 介绍