第一节需求
由于上一节lnmp搭建(discuz单机版)存在缺陷,所以本次教程主要升级单机版到集群版,提高系统的并发量和可靠性等等,架构如下:
第二节工具准备
linux==>CentOS Linux release 7.4.1708 (Core)
php==> php-5.4.16
nginx==>nginx-1.12.2
mysql==>mysql-5.7.26
discuz==>Discuz!X3.4
maxscale ==>maxscale-2.3.6
第三节实战操作
注意注意!!!!!!
每一台服务器前期准备:
[root@web50 iso]# yum -y install epel-realease
[root@web50 iso]# vim /etc/selinux/config
SELINUX=disabled
[root@web50 iso]# systemctl stop firewalld
[root@web50 iso]# systemctl disable firewalld
[root@web50 iso]#yum -y install iptables-services
[root@web50 iso]# iptables -F
[root@web50 iso]# service iptables save
3.1安装web
51-53都一样,以下都是已一台机器威力
1)安装网站nginx
[root@web51 ~]# useradd -s /sbin/nologin nginx
[root@web51~]# yum -y install gcc automake autoconf libtool make gcc gcc-c++ glibc
[root@web51~]# yum -y install zlib-devel openssl-devel pcre-devel
[root@web51~]# curl -O http://nginx.org/download/nginx-1.12.2.tar.gz
[root@web51 nginx-1.12.2]# # tar -xvf nginx-1.12.2.tar.gz
[root@web51 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module
[root@web51 nginx-1.12.2]# make && make install
[root@web51 ~]# ln -s /usr/local/nginx/sbin/nginx /sbin/
[root@web51 ~]# cd /usr/local/nginx/
[root@web51 nginx]# vim conf/nginx.conf
..........
location / {
root html;
index index.php index.html index.htm;
}
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.conf;
}
.........
[root@web51 ~]# ss -tunlp | grep :80
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=1270,fd=6),("nginx",pid=1269,fd=6))
[root@web51 ~]# scp -r 192.168.68.50:/usr/local/nginx/html/* /usr/local/nginx/html/
[root@web51 ~]# chown -R nginx:nginx /usr/local/nginx/html/
[root@web51 ~]# echo '/usr/local/nginx/sbin/nginx' >> /etc/rc.local
[root@web51 ~]# chmod +x /etc/rc.local
2)安装php
[root@web51 ~]# yum -y install libmcrypt-devel mhash-devel libxslt-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel libxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2 bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-devel krb5 krb5-devel libidn libidn-devel openssl openssl-devel libzip
[root@web51 ~]# yum -y install php php-mysql php-fpm
[root@web51 ~]# vim /etc/php-fpm.d/www.conf
......
user = nginx
group = nginx
.......
[root@web51 ~]# systemctl start php-fpm
[root@web51 ~]# systemctl enable php-fpm
[root@web51 ~]# ss -tunlp | grep :9000
tcp LISTEN 0 128 127.0.0.1:9000 *:* users:(("php-fpm",pid=23108,fd=5),("php-fpm",pid=23107,fd=5),("php-fpm",pid=23106,fd=7))
[root@web51 ~]# echo '<?php echo phpinfo();?>' >/usr/local/nginx/html/test.php
浏览器:http://192.168.68.50/test.php
3.2安装调度器nginx
[root@dispatch103 ~]# useradd -s /sbin/nologin nginx
[root@dispatch103 ~]# yum -y install gcc automake autoconf libtool make gcc gcc-c++ glibc
[root@dispatch103 ~]# yum -y install zlib-devel openssl-devel pcre-devel
[root@dispatch103 ~]# curl -O http://nginx.org/download/nginx-1.12.2.tar.gz
[root@dispatch103 nginx-1.12.2]# # tar -xvf nginx-1.12.2.tar.gz
[root@dispatch103 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-stream
[root@dispatch103 nginx-1.12.2]# make && make install
[root@dispatch103 ~]# ln -s /usr/local/nginx/sbin/nginx /sbin/
[root@dispatch103 ~]# cd /usr/local/nginx/
[root@dispatch103 nginx]# vim conf/nginx.conf
..........
http {
......
upstream myweb {
ip_hash;
server 192.168.68.50:80 weight=1 max_fails=1 fail_timeout=30;
server 192.168.68.51:80 weight=1 max_fails=1 fail_timeout=30;
server 192.168.68.52:80 weight=1 max_fails=1 fail_timeout=30;
server 192.168.68.53:80 weight=1 max_fails=1 fail_timeout=30;
}
server {
location / {
proxy_pass http://myweb;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 10m;
: }
}
.........
}
}
[root@dispatch103 ~]# ss -tunlp | grep :80
tcp LISTEN 0 128 *:80 *:* users:(("nginx",pid=1270,fd=6),("nginx",pid=1269,fd=6))
[root@dispatch103 ~]# echo '/usr/local/nginx/sbin/nginx' >> /etc/rc.local
[root@dispatch103 ~]# chmod +x /etc/rc.local
3.3安装mysql
1)103,110,111,112都这样配置
[root@mysql102 iso]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
[root@mysql102 iso]# ls
mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
[root@mysql102 iso]# tar -xvf mysql-5.7.26-1.el7.x86_64.rpm-bundle.tar
[root@mysql102 iso]# yum -y install *.rpm
[root@mysql102 iso]# systemctl restart mysqld
[root@mysql102 iso]# systemctl stop mysqld
[root@mysql102 iso]# rm -rf /var/lib/mysql/*
[root@mysql102 iso]# scp -r 192.168.68.50:/var/lib/mysql/* /var/lib/mysql/
[root@mysql102 ~]# mv /var/lib/mysql/auto.cnf /var/lib/mysql/auto.cnf.bak
[root@mysql102 iso]# chown -R mysql:mysql /var/lib/mysql/
2)主库配置mysql102
[root@mysql102 iso]# vim /etc/my.cnf
......
[mysqld]
character_set_server=utf8
log_bin=master102
server_id=1
binlog_format="mixed"
........
[root@mysql102 iso]# systemctl restart mysqld
[root@mysql102 iso]# ss -tunlp | grep :3306
tcp LISTEN 0 80 :::3306 :::* users:(("mysqld",pid=23295,fd=22))
[root@mysql102 ~]# mysql -uroot -pLuojun666.com522527
mysql> show master status\G;
*************************** 1. row ***************************
File: master102.000001
Position: 154
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set:
1 row in set (0.00 sec)
ERROR:
No query specified
mysql> grant replication slave on *.* to repluser@'%' identified by 'Repluser.com522527';;
3)从库配置以110为例,111-113一样
[root@mysql110 ~]# vim /etc/my.cnf
....
[mysqld]
character_set_server=utf8
server_id=2 #111配置3;112配置4,不可以重复
......
[root@mysql110 ~]# systemctl restart mysqld
[root@mysql110 ~]# mysql -uroot -pLuojun666.com522527
mysql> change master to
-> master_host='192.168.68.102',
-> master_user='repluser',
-> master_password='Repluser.com522527',
-> master_log_file=' master102.000001',
-> master_log_pos=441;
mysql> start slave;
mysql> show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.68.102
Master_User: repluser
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: master102.000002
Read_Master_Log_Pos: 154
Relay_Log_File: web50-relay-bin.000003
Relay_Log_Pos: 320
Relay_Master_Log_File: master102.000002
Slave_IO_Running: Yes #这个ok就可以了
Slave_SQL_Running: Yes #这个ok就可以了
..............
mysql>exit
3.4安装maxscale
maxscale 链接:https://pan.baidu.com/s/1cEyNxe9JA_cidYLU-RN6XQ 提取码:392g
[root@maxscale10 iso]# ls
maxscale-2.3.6-1.centos.7.x86_64.rpm
[root@maxscale10 iso]# yum -y install maxscale-2.3.6-1.centos.7.x86_64.rpm
[root@maxscale10 ~]# vim /etc/maxscale.cnf
[maxscale]
threads=auto
[server1]
type=server
address=192.168.68.102
port=3306
#protocol=MariaDBBackend
protocol=MySQLBackend
[server2]
type=server
address=192.168.68.110
port=3306
#protocol=MariaDBBackend
protocol=MySQLBackend
[server3]
type=server
address=192.168.68.110
port=3306
#protocol=MariaDBBackend
protocol=MySQLBackend
[server4]
type=server
address=192.168.68.110
port=3306
#protocol=MariaDBBackend
protocol=MySQLBackend
[MariaDB-Monitor]
type=monitor
#module=mariadbmon
module=mysqlmon
servers=server1, server2, server3, server4
user=scalemon
password=Scalemon.com522527
monitor_interval=2000
detect_stale_master=true
#[Read-Only-Service]
#type=service
#router=readconnroute
#servers=server1
#user=myuser
#password=mypwd
#router_options=slave
[Read-Write-Service]
type=service
router=readwritesplit
servers=server1, server2, server3, server4
user=pliadmin
password=Pliadmin.com522527
max_slave_connections=100%
[MaxAdmin-Service]
type=service
router=cli
#[Read-Only-Listener]
#type=listener
#service=Read-Only-Service
#protocol=MariaDBClient
#port=4008
[Read-Write-Listener]
type=listener
service=Read-Write-Service
protocol=MariaDBClient
port=3306
[MaxAdmin-Listener]
type=listener
service=MaxAdmin-Service
protocol=maxscaled
socket=default
3.5添加授权
1)在主库操作(在102主机操作),丛库自动同步
[root@mysql102 ~]# mysql -uroot -pLuojun666.com522527
mysql> grant all on bbsdb.* to BBSuser@'%' identified by 'Bbsuser.com522527';
mysql> grant replication slave,replication client on *.* to scalemon@'%' identified by 'Scalemon.com522527';
mysql> grant select on mysql.* to pliadmin@'%' identified by 'Pliadmin.com522527';
mysql> select user,host from mysql.user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| BBSuser | % |
| pliadmin | % |
| repluser | % |
| scalemon | % |
| bbsuser | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
2)丛库查看110,111,112
mysql> select user,host from mysql.user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| BBSuser | % |
| pliadmin | % |
| scalemon | % |
| bbsuser | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
3.6启动maxscale
[root@maxscale10 ~]# systemctl restart maxscale 或者 maxscale -f /etc/maxscale.cnf
[root@maxscale10 ~]# systemctl enablemaxscale
[root@maxscale10 ~]# ss -tunlp | grep maxscale
tcp LISTEN 0 128 127.0.0.1:8989 *:* users:(("maxscale",pid=3421,fd=22))
tcp LISTEN 0 128 :::3306 :::* users:(("maxscale",pid=3421,fd=19))
##maxscale验证
[root@maxscale10 ~]# maxadmin
MaxScale> list servers
Servers.
-------------------+-----------------+-------+-------------+--------------------
Server | Address | Port | Connections | Status
-------------------+-----------------+-------+-------------+--------------------
server1 | 192.168.68.102 | 3306 | 0 | Master, Running
server2 | 192.168.68.110 | 3306 | 0 | Slave, Running
server3 | 192.168.68.111 | 3306 | 0 | Slave, Running
server4 | 192.168.68.112 | 3306 | 0 | Slave, Running
-------------------+-----------------+-------+-------------+--------------------
3.7数据库测试连接
[root@mysql102 ~]# mysql -uBBSuser -pBbsuser.com522527 -P3306 -h192.168.68.10
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bbsdb |
+--------------------+
2 rows in set (0.00 sec)
3.8修改web中discuz数据库配置文件
1)第一个文件
[root@web50 ~]# cd /usr/local/nginx/html/
[root@web50 html]# vim config/config_global.php
// ---------------------------- CONFIG DB ----------------------------- //
$_config['db']['1']['dbhost'] = '192.168.68.103';
$_config['db']['1']['dbuser'] = 'BBSuser';
$_config['db']['1']['dbpw'] = 'Bbsuser.com522527';
$_config['db']['1']['dbcharset'] = 'utf8';
$_config['db']['1']['pconnect'] = '0';
$_config['db']['1']['dbname'] = 'bbsdb';
.......
2)第二个文件
[root@web50 html]# vim uc_server/data/config.inc.php
define('UC_DBHOST', '192.168.68.103');
define('UC_DBUSER', 'BBSuser');
define('UC_DBPW', 'Bbsuser.com522527');
define('UC_DBNAME', 'bbsdb');
define('UC_DBCHARSET', 'utf8');
.......
3)第三个文件
[root@web50 html]# vim config/config_ucenter.php
.......
define('UC_DBHOST', '192.168.68.103');
define('UC_DBUSER', 'BBSuser');
define('UC_DBPW', 'Bbsuser.com522527');
define('UC_DBNAME', 'bbsdb');
define('UC_DBCHARSET', 'utf8');
........
4)同步文件给web51,52,53
[root@web50 html]# yum -y install rsync
[root@web50 html]# for i in 51 52 53
> do
> rsync -ar /usr/local/nginx/html/ 192.168.68.$i:/usr/local/nginx/html/
> done
第四节测试
在浏览器上输入http://192.168.68.103
总结
本次教程和lnmp搭建(discuz单机版)是有关联的,是lnmp搭建(discuz单机版)的升级版,虽然本次架构挺不错了,不过存在单点故障,下一节lnmp搭建(discuz高可用版)会解决此问题
温馨提示:
做实验时,每一台服务器都要关闭防火墙,禁用selinux!!!!
看官福利:
结束语:
更多精彩内容持续更新中,关注我,有你更精彩。
网友评论