lnmp應用分離+keepalived+mysql互爲主備

nginx+php:192.168.198.135,192.168.198.131
keepalived-master:192.168.198.228
keepalived-slave:192.168.198.229
mysql+keepalived:192.168.198.134,192.168.198.141

discuz:http://ahdx.down.chinaz.com/201710/Discuz_X3.4_SC_UTF8_1001.zip

1、安裝nginx
①源碼
②yum安裝(我使用了這個)
yum install -y nginx

lnmp應用分離+keepalived+mysql互爲主備

2、安裝mariadb
①源碼
②yum安裝(我使用了這個)
yum install -y epel-release
yum install -y mariadb-server mariadb
yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes -y

(1)mariadb主從配置(my.cnf)
主:
[mysqld]
server-id=1
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
wait_timeout = 300
#skip_name_resolve = 1
#explicit_defaults_for_timestamp=true
#symbolic-links =0
max_allowed_packet = 100M
thread_stack = 192K
thread_cache_size = 8
max_connections = 1000
max_connect_errors = 1500
transaction_isolation = READ-COMMITTED
#explicit_defaults_for_timestamp = 1
join_buffer_size = 134217728
tmp_table_size = 67108864
tmpdir = /tmp
#innodb_log_buffer_size = 32M
#innodb_log_file_size = 48M
#innodb_page_size = 8192
#innodb_buffer_pool_size = 12G
#innodb_buffer_pool_instances = 8
#innodb_buffer_pool_load_at_startup = 1
#innodb_buffer_pool_dump_at_shutdown = 1
#innodb_lru_scan_depth = 2000
#innodb_lock_wait_timeout = 5
#innodb_io_capacity = 4000
#innodb_io_capacity_max = 8000
#innodb_flush_method = O_DIRECT
#innodb_file_format = Barracuda
#innodb_file_format_max = Barracuda
query_cache_limit = 32M
query_cache_size = 32M
expire_logs_days = 10
max_binlog_size = 100M
#for replication
log-bin=mysql-bin
log-bin=test-bin
binlog-do-db=test
binlog-do-db=discuz
binlog-do-db=db
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
binlog-ignore-db=performance_schema
#binlog-ignore-db=oc
[client]
#socket=/app/mysql/lib/mysql.sock
socket=/var/lib/mysql/mysql.sock

Disabling symbolic-links is recommended to prevent assorted security risks

#symbolic-links=0

Settings user and group are ignored when systemd is used.

If you need to run mysqld under a different user or group,

customize your systemd unit file for mariadb according to the

instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#

include all files from the config directory

#
!includedir /etc/my.cnf.d

從:
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
character_set_server=utf8
#default-storage-engine=innodb
init_connect='SET NAMES utf8'
log-bin=test-bin
port=3306
#import csv file
secure_file_priv=""
max_connections = 1000
max_connect_errors = 1000
join_buffer_size = 11382893
tmp_table_size = 6710886
tmpdir = /tmp

Disabling symbolic-links is recommended to prevent assorted security risks

symbolic-links=0

Settings user and group are ignored when systemd is used.

If you need to run mysqld under a different user or group,

customize your systemd unit file for mariadb according to the

instructions in http://fedoraproject.org/wiki/Systemd

inno settings

#innodb_buffer_pool_size = 5G
#innodb_log_buffer_size = 32M
#innodb_log_file_size = 512M
#innodb_lru_scan_depth = 2000
#innodb_lock_wait_timeout = 5
#innodb_io_capacity = 4000
#innodb_io_capacity_max = 8000
#innodb_flush_method = O_DIRECT
#innodb_file_format = Barracuda
#innodb_file_format_max = Barracuda
#innodb_flush_neighbors = 1
#innodb_log_file_size = 2G
#innodb_log_buffer_size = 10777216
#innodb_purge_threads = 4
#innodb_large_prefix = 1
#innodb_thread_concurrency = 64
#innodb_print_all_deadlocks = 1
#innodb_strict_mode = 1
#innodb_sort_buffer_size = 67108864
#####Logs Settings#####
#innodb_log_buffer_size = 10777216
#innodb_purge_threads = 4
#innodb_large_prefix = 1
#innodb_thread_concurrency = 64
#innodb_print_all_deadlocks = 1
#innodb_strict_mode = 1
#innodb_sort_buffer_size = 67108864
#####Logs Settings#####
query_cache_limit = 32M
query_cache_size = 32M
expire_logs_days = 10
max_binlog_size = 100M
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
server-id=134
replicate-do-db=db
replicate-do-db=test
replicate-do-db=discuz
replicate_wild_do_table=test.t2
replicate_wild_do_table=test.t4
###replicate-do-db=erp
#replicate-do-db=payment
#replicate-do-db=crm
#replicate-do-db=marketing
#replicate-do-db=ocrp
#replicate-do-db=oc
slave-skip-errors=1032,1062,1050,1051,1677
log-slave-updates=true
relay-log=test-relay-bin
#relay-log-index=relay-bin-index

#skip-slave-start
#slave-parallel-type=LOGICAL_CLOCK
#slave-parallel-workers=16
#slave_pending_jobs_size_max=120M
#relay_log_info_repository=table
#master_info_repository=table

[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

#

include all files from the config directory

#
!includedir /etc/my.cnf.d

(2)mariadb主從數據庫的配置
主:
①創建用戶
[root@zimbra ~]# mysql -u root -p123456 -e "create user repl;"
②授權遠程訪問(這一步不做好的話,通過vip訪問會導致discuz會出現1045的報錯)
[root@zimbra ~]# mysql -u root -p123456 -e "grant all privileges on . to 'root'@'192.168.198.134' identified by '123456' with grant option;"

MariaDB [mysql]> grant all privileges on . to 'root'@'192.168.198.141' identified by '123456' with grant option;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> grant all privileges on . to 'root'@'192.168.198.135' identified by '123456' with grant option;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> grant all privileges on . to 'root'@'192.168.198.131' identified by '123456' with grant option;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> grant all privileges on . to 'root'@'192.168.198.23' identified by '123456' with grant option;
Query OK, 0 rows affected (0.00 sec)

MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
#####授權從庫同步的賬號
[root@zimbra ~]# mysql -u root -p123456 -e "grant all privileges on . to 'repl'@'192.168.198.134' identified by '123456' with grant option;"

lnmp應用分離+keepalived+mysql互爲主備
lnmp應用分離+keepalived+mysql互爲主備

從:
[root@slave ~]# mysql -uroot -e "stop slave\G"
[root@slave ~]# mysql -uroot
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 6
Server version: 5.5.52-MariaDB MariaDB Server

Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> change master to master_host='192.168.198.141',master_user='repl',master_password='123456',master_log_file='test-bin.000002',master_log_pos=3503;
###根據主pos來做從的數據庫配置
lnmp應用分離+keepalived+mysql互爲主備

3、安裝php
wget http://cn2.php.net/distributions/php-5.6.0.tar.xz 再解壓
cd /usr/local/src/php-5.6.0
./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-inline-optimization --disable-debug --disable-rpath --enable-shared --enable-opcache --enable-fpm --with-fpm-user=www --with-fpm-group=www --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-gettext --enable-mbstring --with-iconv --with-mcrypt --with-mhash --with-openssl --enable-bcmath --enable-soap --with-libxml-dir --enable-pcntl --enable-shmop --enable-sysvmsg --enable-sysvsem --enable-sysvshm --enable-sockets --with-curl --with-zlib --enable-zip --with-bz2 --with-readline --with-jpeg-dir=/usr/local/jpeg --with-png-dir=/usr/local/png --with-zlib --with-freetype-dir=/usr/local/freetype --with-gd
lnmp應用分離+keepalived+mysql互爲主備
配置 PHP
配置文件:

cp php.ini-development /usr/local/php/etc/php.ini
php-fpm 服務

cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
chmod +x /etc/init.d/php-fpm

php-fpm 可用參數 start|stop|force-quit|restart|reload|status

添加 PHP 命令到環境變量
編輯 ~/.bash_profile,添加:

###java env
export JAVA_HOME=/usr/local/src/jdk1.6.0_45
#export JRE_HOME=$JAVA_HOME/jre
export PATH=$JAVE_HOME/bin:$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib:$JAVA_HOME/lib

###php env
export PHP_HOME=/usr/local/php56
#export JRE_HOME=$JAVA_HOME/jre
export PATH=$PHP_HOME/bin:$PHP_HOME/bin:$PATH
export CLASSPATH=.:$PHP_HOME/lib:$PHP_HOME/lib

~/.bash_profile

查看看 PHP 版本
php -v
PHP 5.6.0 (cli) (built: Sep 23 2014 03:44:18)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies

4、keepalived配置
mysql-keepalived
lnmp應用分離+keepalived+mysql互爲主備
因爲mysql用不到那個lvs的東西,就註釋了。

nginx-keepalived(主和從的區別在於state和priority)
lnmp應用分離+keepalived+mysql互爲主備
lnmp應用分離+keepalived+mysql互爲主備

在mariadb的show master status看到不會因爲主的nginx機器掛掉而導致數據庫連接丟失,因爲有vip的
lnmp應用分離+keepalived+mysql互爲主備
lnmp應用分離+keepalived+mysql互爲主備
lnmp應用分離+keepalived+mysql互爲主備
lnmp應用分離+keepalived+mysql互爲主備

5、安裝phpMyAdmin
wget https://files.phpmyadmin.net/phpMyAdmin/4.7.7/phpMyAdmin-4.7.7-all-languages.zip
把壓縮包放進去網址根目錄,然後解壓,把解壓出來的文件夾重命名爲phpmyadmin,然後再進去這個目錄,把config.sample.inc.php複製爲config.inc.php,有行$cfg['Servers'][$i]['host'] = 'localhost'; 把localhost改爲你的數據庫IP
lnmp應用分離+keepalived+mysql互爲主備
更改完畢就重啓mariadb和nginx,再通過瀏覽器訪問http://ip/phpmyadmin即可
lnmp應用分離+keepalived+mysql互爲主備

關於報錯
如果打開php頁面404的話,很可能是nginx的配置文件導致的。
lnmp應用分離+keepalived+mysql互爲主備
紅色圈住的那裏要改成網站根目錄才行
lnmp應用分離+keepalived+mysql互爲主備
mysql 1236報錯
lnmp應用分離+keepalived+mysql互爲主備
解決:stop slave;reset slave;start slave;
mysql 1050報錯
lnmp應用分離+keepalived+mysql互爲主備
在my.cnf報錯添加slave-skip-errors=1032,1062,1050,1677這段配置後重啓mariadb即可

重啓機器時候或者keepalived服務沒起來,通過這個https://blog.51cto.com/wsw26/2067195 腳本就能踢起來vip了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章