实现Web服务器之间使用同一个MYSQL和相同的网页配置文件的方法
创新互联负载均衡(Cloud Load Balancer)是对多台云服务器进行流量分发的服务。创新互联负载均衡可以通过流量分发扩展应用系统对外的服务能力,通过消除单点故障提升应用系统的可用性。创新互联负载均衡服务通过设置虚拟服务地址(VIP),将位于同一地域的多台云服务器资源虚拟成一个高性能、高可用的应用服务池。根据应用指定的方式,将来自客户端的网络请求分发到云服务器池中。创新互联负载均衡服务会检查云服务器池中云服务器实例的健康状态,自动隔离异常状态的实例,从而解决了云服务器的单点问题,同时提高了应用的整体服务能力。今天创新互联给大家介绍下实现Web服务器之间使用同一个MySQL和相同的网页配置文件的方法
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:国际域名空间、虚拟空间、营销软件、网站建设、寿阳网站维护、网站推广。
前言:
用NFS、LAMP、BIND结合的方法,应对网络中访问量增大而导致云服务器负载运行的情况。以实现Web服务器之间使用同一个MYSQL和相同的网页配置文件。
实验环境:
HostName | IP | 规划 |
nfs.preferred.com | 192.168.1.6 | NFS |
ns.preferred.com | 192.168.1.5 | DNS |
mysql.preferred.com | 192.168.1.4 | MYSQL |
www.preferred.com | 192.168.1.3 | WEB |
www.preferred.com | 192.168.1.2 | WEB |
实验拓扑:
实验步骤:
一、搭建NFS
以下操作在nfs.preferred.com服务器上实现
[root@localhost ~]# yum install nfs-utils -y <---这里需要安装nfs这个包(基本的NFS命令与监控程序) [root@localhost ~]# rpm -qa | grep nfs-utils --color nfs-utils-1.2.3-39.el6.x86_64
创建共享目录并配置nfs文件
[root@localhost~]# mkdir /mydata [root@localhost~]# mkdir /webdata [root@localhost~]# cd /webdata/ [root@localhost webdata]# unzip wordpress-3.2.1-zh_CN.zip &> /dev/null [root@localhost webdata]# chmod -R 777 wordpress [root@localhost webdata]# ls -l drwxrwxrwx 5 root root 4096 Oct 11 2016 wordpress -rw-r--r-- 1 root root 4372680 Oct 11 2016 wordpress-3.2.1-zh_CN.zip [root@localhost webdata]# vim /etc/exports /mydata 192.168.1.4(rw,no_root_squash) /webdata 192.168.1.2(rw,no_root_squash) 192.168.1.3(rw,no_root_squash) [root@localhost ~]# service rpcbind start 注:rpcbind是一个RPC服务,主要是在nfs共享的时候负责通知客户端、服务器nfs端口号。 [root@localhost ~]# service nfs start Starting NFS services: [ OK ] Starting NFS quotas: [ OK ] Starting NFS mountd: [ OK ] Starting NFS daemon: [ OK ] Starting RPC idmapd: [ OK ] [root@localhost ~]# showmount -e 192.168.1.6 <---查看当前共享的目录 Export list for 192.168.1.6: /webdata 192.168.1.3,192.168.1.2 /mydata 192.168.1.4
二、搭建DNS
以下操作在ns.preferred.com服务器上实现
[root@localhost ~]# yum install bind -y | tail -n 5 Installed: bind.x86_64 32:9.8.2-0.47.rc1.el6_8.1 Complete! [root@localhost ~]# vim /etc/named.rfc1912.zones <---添加zone(正向与反向的) zone "preferred.com" IN { type master; file "preferred.com.zone"; }; zone "1.168.192.in-addr.arpa" IN { type master; file "192.168.1.5.zone"; }; [root@localhost ~]# vim /var/named/preferred.com.zone <---配置DNS正向区域解析 $TTL 86400 $ORIGIN preferred.com. @ IN SOA ns.preferred.com admin.preferred.com ( 2016101001 1D 5M 7D 1D ) IN NS ns ns IN A 192.168.1.5 www IN A 192.168.1.2 www IN A 192.168.1.3 nfs IN A 192.168.1.6 [root@localhost ~]# vim /var/named/192.168.1.5.zone <---配置DNS反向区域解析 $TTL 86400 @ IN SOA ns.preferred.com. admin.preferred.com ( 2016101001 1H 5M 7D 1D ) IN NS ns.preferred.com. 5 IN PTR ns.preferred.com. 2 IN PTR www.preferred.com. 3 IN PTR www.preferred.com. 6 IN PTR nfs.preferred.com. [root@localhost ~]# systemctl restart named [root@localhost ~]# dig -t A www.preferred.com @192.168.1.5 <---测试DNS能否正向解析 ; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.4 <<>> -t A www.preferred.com @192.168.1.5 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 23764 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 1, ADDITIONAL: 2 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;www.preferred.com. IN A ;; ANSWER SECTION: www.preferred.com. 86400 IN A 192.168.1.2 www.preferred.com. 86400 IN A 192.168.1.3 ;; AUTHORITY SECTION: preferred.com. 86400 IN NS ns.preferred.com. ;; ADDITIONAL SECTION: ns.preferred.com. 86400 IN A 192.168.1.5 ;; Query time: 0 msec ;; SERVER: 192.168.1.5#53(192.168.1.5) ;; WHEN: Tue Oct 11 04:22:36 EDT 2016 ;; MSG SIZE rcvd: 111 [root@localhost ~]# dig -x 192.168.1.2 @192.168.1.5 <---测试DNS能否反向解析 ; <<>> DiG 9.9.4-RedHat-9.9.4-29.el7_2.4 <<>> -x 192.168.1.2 @192.168.1.5 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 21950 ;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 1, ADDITIONAL: 2 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;2.1.168.192.in-addr.arpa. IN PTR ;; ANSWER SECTION: 2.1.168.192.in-addr.arpa. 86400 IN PTR www.preferred.com. ;; AUTHORITY SECTION: 1.168.192.in-addr.arpa. 86400 IN NS ns.preferred.com. ;; ADDITIONAL SECTION: ns.preferred.com. 86400 IN A 192.168.1.5 ;; Query time: 0 msec ;; SERVER: 192.168.1.5#53(192.168.1.5) ;; WHEN: Tue Oct 11 04:23:57 EDT 2016 ;; MSG SIZE rcvd: 117
三、搭建MYSQL
以下操作在mysql.preferred.com服务器上实现
[root@localhost ~]# mkdir /mydata/ [root@localhost ~]# mount -t nfs 192.168.1.6:/mydata/ /mydata/ <---挂载共享目录 [root@localhost ~]# groupadd -r mysql <---创建mysql组和用户 [root@localhost ~]# useradd -r -g mysql -s /sbin/nologin mysql [root@localhost ~]# cd /usr/local/ [root@localhost local]# ln -sv /root/mariadb-5.5.43-linux-x86_64 mysql "mysql" -> "/root/mariadb-5.5.43-linux-x86_64" [root@localhost local]# cd mysql/ [root@localhost mysql]# ls bin COPYING.LESSER EXCEPTIONS-CLIENT INSTALL-BINARY man README share support-files COPYING data include lib mysql-test scripts sql-bench [root@localhost mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/mydata/ <---初始化数据库 Installing MariaDB/MySQL system tables in '/mydata' ... 161010 13:33:59 [Note] ./bin/mysqld (mysqld 5.5.43-MariaDB) starting as process 2183 ... OK Filling help tables... 161010 13:33:59 [Note] ./bin/mysqld (mysqld 5.5.43-MariaDB) starting as process 2191 ... OK ... [root@localhost mysql]# ls /mydata/ <--查看初始后生成的数据 aria_log.00000001 aria_log_control mysql performance_schema test [root@localhost mysql]# mkdir /etc/mysql/ [root@localhost mysql]# cp support-files/my-large.cnf /etc/mysql/my.cnf [root@localhost mysql]# vim /etc/mysql/my.cnf <---修改配置文件,添加以下三行 datadir = /mydata innodb_file_per_table = on skip_name_resolve = on [root@localhost mysql]# service mysqld start Starting MySQL... [确定] [root@localhost mysql]# ss -tnl | grep 3306 <---服务端口已经开启 LISTEN 0 50 *:3306 *:* *:* [root@localhost mysql]# mysql -bash: mysql: command not found 注:尝试登陆MYSQL却报错,不要紧张。这是因为/usr/local/bin目录下确实mysql导致,只需要建立一个软连接即可解决。 [root@localhost ~]# ln -sv /usr/local/mysql/bin/mysql /usr/bin/ "/usr/bin/mysql" -> "/usr/local/mysql/bin/mysql" [root@localhost ~]# mysql <---连接数据库创建用户密码与授权 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 115 Server version: 5.5.43-MariaDB-log MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> CREATE DATABASE pre; Query OK, 1 rows affected (0.00 sec) MariaDB [(none)]> GRANT ALL ON pre.* TO 'jds'@'%' IDENTIFIED BY 'redhat'; <---创建用户并赋予权限 Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> FLUSH PRIVILEGES; Query OK, 0 rows affected (0.00 sec)
四、搭建WEB
以下操作在www.preferred.com(192.168.1.2)服务器上实现
[root@localhost ~]# yum install httpd php-mysql php -y | tail -n 10 warning: rpmts_HdrFromFdno: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY Importing GPG key 0xC105B9DE: Userid: CentOS-6 Key (CentOS 6 Official Signing Key)Package: centos-release-6-5.el6.centos.11.1.x86_64 (@anaconda-CentOS-201311272149.x86_64/6.5) From: /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6 php-cli.x86_64 0:5.3.3-48.el6_8 php-common.x86_64 0:5.3.3-48.el6_8 php-pdo.x86_64 0:5.3.3-48.el6_8 Updated: httpd.x86_64 0:2.2.15-54.el6.centos Dependency Updated: httpd-tools.x86_64 0:2.2.15-54.el6.centos Complete! [root@localhost ~]# mkdir /webdata [root@localhost ~]# vim /etc/httpd/conf/httpd.conf <---添加以下几行 ServerName www.preferred.com:80 ServerName www.preferred.com DocumentRoot /webdata/wordpress [root@localhost ~]# service rpcbind start [root@localhost ~]# mount -t nfs 192.168.1.6:/webdata /webdata [root@localhost ~]# ls /webdata/ wordpress wordpress-3.2.1-zh_CN.zip [root@localhost ~]# service httpd start Starting httpd: [ OK ]
接下来安装wordpress,没有的话可以自己去下载一个https://wordpress.org/download/
[root@localhost ~]# cd /webdata/wordpress [root@localhost wordpress]# ls index.php wp-app.php wp-config.php wp-includes wp-pass.php wp-settings.php license.txt wp-atom.php wp-config-sample.php wp-links-opml.php wp-rdf.php wp-signup.php readme.html wp-blog-header.php wp-content wp-load.php wp-register.php wp-trackback.php wp-activate.php wp-comments-post.php wp-cron.php wp-login.php wp-rss2.php xmlrpc.php wp-admin wp-commentsrss2.php wp-feed.php wp-mail.php wp-rss.php [root@localhost wordpress]# cp wp-config-sample.php wp-config.php <---这是一个示例配置文件,更名并配置 [root@localhost wordpress]# vim wp-config.php // ** MySQL 设置 - 具体信息来自您正在使用的主机 ** // /** WordPress 数据库的名称 */ define('DB_NAME', 'pre'); <---连接数据库所使用的,与刚刚在mysq中创建的一致,以下几个都是。 /** MySQL 数据库用户名 */ define('DB_USER', 'jds'); /** MySQL 数据库密码 */ define('DB_PASSWORD', 'redhat'); /** MySQL 主机 */ define('DB_HOST', '192.168.1.4'); /** 创建数据表时默认的文字编码 */ define('DB_CHARSET', 'utf8'); /** 数据库整理类型。如不确定请勿更改 */ define('DB_COLLATE', ''); [root@localhost ~]# mysql -ujds -predhat -h 192.168.1.4 <---尝试登陆 ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.4' (113) 注:遇到这个问题怎么个人都不好了,在MYSQL服务器上取消防火墙也不行。所以尝试在MYSQL服务器的防火墙上加上端口的允许。 [root@localhost ~]# iptables -L -n | grep 3306 [root@localhost ~]# iptables -I INPUT -s 0/0 -p tcp --dport 3306 -j ACCEPT [root@localhost mysql]# iptables -L -n | grep 3306 ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:3306 [root@localhost ~]# mysql -ujds -predhat -h 192.168.1.4 <---尝试再次登陆 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 116 Server version: 5.5.43-MariaDB-log MariaDB Server Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> SHOW DATABASES; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | pre | <---之前创建的数据库可以访问 | test | +--------------------+ 5 rows in set (0.00 sec)
测试访问正常!!!
以下操作在www.preferred.com(192.168.1.3)服务器上实现
[root@localhost ~]# yum install httpd php-mysql php -y [root@localhost ~]# mkdir /webdata [root@localhost ~]# vim /etc/httpd/conf/httpd.conf <---添加以下几行 ServerName www.preferred.com:80ServerName www.preferred.com DocumentRoot /webdata/wordpress [root@localhost ~]# service rpcbind start [root@localhost ~]# mount -t nfs 192.168.1.6:/webdata /webdata [root@localhost ~]# ls /webdata/ wordpress wordpress-3.2.1-zh_CN.zip [root@localhost ~]# service httpd start Starting httpd: [ OK ]
先用WEB(192.168.1.3)进行测试。发布一条文章
用WEB(192.168.1.2)进行测试。现在访问看看
看到了在WEB(192.168.1.3)上发布的文章,我们在WEB(192.168.1.2)上也发布一篇
最后我们在WEB(192.168.1.3)上查看
至此,我们实验的目的已经达到。
如果大家还有什么地方需要了解的可以在创新互联官网找我们的专业技术工程师的,创新互联技术工程师在行业内拥有十几年的经验了,所以会比小编回答的更加详细专业。创新互联官网链接www.cdcxhl.com
网站栏目:实现Web服务器之间使用同一个MYSQL和相同的网页配置文件的方法
文章分享:http://scyanting.com/article/gohdod.html