mysql 5.7 nginx負載+mysql雙主實現
一臺不夠再加一臺
現在先寫個一臺nginx的,後續修改為熱nginx
準備工作
mysql 5.7 安裝包
mysql-5.7.31-linux-glibc2.12-x86_64.tar
# 百度雲:不推薦,學習下載安裝工具,肯定是選找新的來,我是迫於無奈,公司主用的是5.7的 https://pan.baidu.com/s/1zIDvIEouGQSpAfPFR8hSnQ 1234 # 推薦清華映象站,感謝清華,雖說拒絕了去讀書,但是依然為我這個廢柴提供了映象服務 https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-5.7/
三臺伺服器
nginx:172.165.165.121
mysqlA:172.165.165.131
mysqlB:172.165.165.132
nginx安裝:
資料庫安裝:https://blog.csdn.net/qq_37809967/article/details/109396043
mysql配置檔案
mysql A 配置檔案 /etc/my.cnf
[client] port = 3306 socket = /tmp/mysql.sock [mysqld] # 跳過登入密碼校驗,用於重置密碼時使用 # skip-grant-tables user=root basedir = /usr/local/mysql port = 3306 socket = /tmp/mysql.sock datadir = /data/mysql pid-file = /data/mysql/mysql.pid log-error = /data/mysql/mysql.err server-id = 1 auto_increment_offset = 1 auto_increment_increment = 2 #奇數ID log-bin = mysql-bin #開啟二進位制功能,MASTER主伺服器必須開啟此項 binlog-format=ROW log-slave-updates=true gtid-mode=on enforce-gtid-consistency=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 slave-parallel-workers=0 sync_binlog=0 binlog-checksum=CRC32 master-verify-checksum=1 slave-sql-verify-checksum=1 binlog-rows-query-log_events=1 expire_logs_days=5 max_binlog_size=1024M #binlog單檔案最大值 replicate-ignore-db = mysql #忽略不同步主從的資料庫 replicate-ignore-db = information_schema replicate-ignore-db = performance_schema replicate-ignore-db = test replicate-ignore-db = zabbix max_connections = 3000 max_connect_errors = 30 explicit_defaults_for_timestamp=true sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' skip-character-set-client-handshake #忽略應用程式想要設定的其他字符集 init-connect='SET NAMES utf8' #連線時執行的SQL character-set-server=utf8 #服務端預設字符集 wait_timeout=1800 #請求的最大連線時間 interactive_timeout=1800 #和上一引數同時修改才會生效 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES #sql模式 max_allowed_packet = 10M bulk_insert_buffer_size = 8M query_cache_type = 1 query_cache_size = 128M query_cache_limit = 4M key_buffer_size = 256M read_buffer_size = 16K skip-name-resolve slow_query_log=1 long_query_time = 6 slow_query_log_file=slow-query.log innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 20M sort_buffer_size = 20M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout [mysqldump] quick max_allowed_packet = 16M [mysqld_safe]
mysql B 配置檔案 /etc/my.cnf
[client] port = 3306 socket = /tmp/mysql.sock [mysqld] # 跳過登入密碼校驗,用於重置密碼時使用 # skip-grant-tables user=root basedir = /usr/local/mysql port = 3306 socket = /tmp/mysql.sock datadir = /data/mysql pid-file = /data/mysql/mysql.pid log-error = /data/mysql/mysql.err server-id = 2 auto_increment_offset = 2 auto_increment_increment = 2 #奇數ID log-bin = mysql-bin #開啟二進位制功能,MASTER主伺服器必須開啟此項 binlog-format=ROW log-slave-updates=true gtid-mode=on enforce-gtid-consistency=true master-info-repository=TABLE relay-log-info-repository=TABLE sync-master-info=1 slave-parallel-workers=0 sync_binlog=0 binlog-checksum=CRC32 master-verify-checksum=1 slave-sql-verify-checksum=1 binlog-rows-query-log_events=1 expire_logs_days=5 max_binlog_size=1024M #binlog單檔案最大值 replicate-ignore-db = mysql #忽略不同步主從的資料庫 replicate-ignore-db = information_schema replicate-ignore-db = performance_schema replicate-ignore-db = test replicate-ignore-db = zabbix max_connections = 3000 max_connect_errors = 30 explicit_defaults_for_timestamp=true sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' skip-character-set-client-handshake #忽略應用程式想要設定的其他字符集 init-connect='SET NAMES utf8' #連線時執行的SQL character-set-server=utf8 #服務端預設字符集 wait_timeout=1800 #請求的最大連線時間 interactive_timeout=1800 #和上一引數同時修改才會生效 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES #sql模式 max_allowed_packet = 10M bulk_insert_buffer_size = 8M query_cache_type = 1 query_cache_size = 128M query_cache_limit = 4M key_buffer_size = 256M read_buffer_size = 16K skip-name-resolve slow_query_log=1 long_query_time = 6 slow_query_log_file=slow-query.log innodb_flush_log_at_trx_commit = 2 innodb_log_buffer_size = 16M [mysql] no-auto-rehash [myisamchk] key_buffer_size = 20M sort_buffer_size = 20M read_buffer = 2M write_buffer = 2M [mysqlhotcopy] interactive-timeout [mysqldump] quick max_allowed_packet = 16M [mysqld_safe]
主要配置都是一樣的,注意的是broker-id 不同,自增初始id不同,
mysqA與mysqlB 配置從許可權
A 與 B 均執行以下指令
# 進入資料庫
/usr/mysql/bin/mysql -uroot -proot
# 設定從使用者 'slave'@'%' 密碼為 'slave'
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY 'slave';
# 重新整理許可權
flush privileges;
區別開始
mysql A 檢視master狀態
show master status;
mysql B 檢視master狀態
show master status;
在mysql A上執行指令
change master to master_host = '172.165.165.132',
master_user = 'slave',
master_password = 'slave',
master_log_file = 'mysql-bin.000012',
master_log_pos = 1167;
# 開始從操作,從主庫同步資料
start slave;
# 檢視從狀態
show slave status \G
在mysqlB上執行指令
change master to master_host = '172.165.165.131',
master_user = 'slave',
master_password = 'slave',
master_log_file = 'mysql-bin.000017',
master_log_pos = 68153006;
# 開始從操作,從主庫同步資料
start slave;
nginx配置
安裝過程極其簡單,主要注意的是要有make安裝環境就行,安裝結束後,nginx預設是在 /usr/local/nginx 資料夾下
解壓
執行configuration
強迫症患者看的難受的話可以在.configuration的時候指定openssl地址,一般沒影響,大部分伺服器都具備基礎環境的,這只是沒有顯示的指出地址,找不到,又不是不能用。
更多安裝詳情請看官網說明:http://nginx.org/en/docs/configure.html,
其實大部分軟體基礎使用,官方都會給出最為權威的文件,一定要看官網
找到openssl地址,在官方案例上 指定openssl庫源路徑配置nginx
open
./configure\
--sbin-path=/usr/local/nginx/nginx\
--conf-path=/usr/local/nginx/nginx.conf\
--pid-path=/usr/local/nginx/nginx.pid\
--with-http_ssl_module\
--with-stream_ssl_module;
進入nginx安裝包資料夾,執行make && make install 指令進行安裝
這裡我腦子抽了,看到別人的教程,沒注意,以為是有個make檔案,執行make指令碼進行安裝。硬是要找make檔案,其實make是一個安裝環境,一幫的伺服器都有的,沒有就執行
yum install gcc automake autoconf libtool make
啟動nginx
nginx預設使用80埠演示案例
如果無法訪問八成是防火牆的原因,請移步Linux常用指令操作防火牆篇
配置nginx代理資料庫
在 /usr/local/nginx/conf/nginx.cnf檔案裡面新增流配置
重啟nginx、配置防火牆
/usr/local/nginx/sbin/nginx -s reload
stream {
server {
listen 3306;
proxy_pass db;
}
upstream db {
server 172.165.165.131:3306 weight=2 max_fails=2 fail_timeout=20;
server 172.165.165.132:3306 weight=3 max_fails=2 fail_timeout=30;
}
}
中間有些小問題,
- 警告缺失openssl,這個不裝其實也沒事,沒用到就可以不裝,
- 安裝openssl又報缺失 perl 5
- mysql代理設定後啟動nginx、異常缺失流模組,官方有說明,配置的時候加上這個,然後在nginx.conf檔案在stream使用前加上一行
load_module '/usr/local/nginx/modules/ngx_stream_module.so'; 就行
反正缺什麼裝什麼就是了
我的nginx.conf檔案
load_module '/usr/local/nginx/modules/ngx_stream_module.so';
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
stream {
server {
listen 9000;
proxy_pass db;
}
upstream db {
server 172.165.165.131:3306 weight=2 max_fails=2 fail_timeout=20;
server 172.165.165.132:3306 weight=3 max_fails=2 fail_timeout=30;
}
}
events {
worker_connections 1024;
}
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}