原材料

正所谓“巧妇难为无米之炊”,在进行服务器端缓存搭建之前,需要将整个流程所需的元素准备好。

  • Nginx作为Web服务器的存在
  • Mongodb磁盘数据库
  • Lua与Nginx结合,进行服务器端的逻辑控制
  • Redis内存数据库,高速缓存
  • PHP读取磁盘数据库信息,返回给客户端

调料包

有了原材料,当然需要将它们融合在一起,才能烹调出美味,下面这些内容则可以称之为调料包:

Mongodb的安装配置

柿子先挑软的捏,整个流程中需要安装和配置的内容比较多,相比来说,Mongodb算是最简单的一个了。

下载

在写这篇博客的时候,最新的Mongodb版本是3.0.5,下载的方法如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$ cd /usr/local
$ su
Password: 
# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.0.5.tgz
	--2015-08-01 11:06:41--  https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.0.5.tgz
	Resolving fastdl.mongodb.org (fastdl.mongodb.org)... 54.230.156.11, 54.230.156.102, 54.230.156.123, ...
	Connecting to fastdl.mongodb.org (fastdl.mongodb.org)|54.230.156.11|:443... connected.
	HTTP request sent, awaiting response... 200 OK
	Length: 50203367 (48M) [application/x-gzip]
	Saving to: ‘mongodb-linux-x86_64-rhel70-3.0.5.tgz’

100%[======================================>] 50,203,367   582KB/s   in 4m 40s 

	2015-08-01 11:11:31 (175 KB/s) - ‘mongodb-linux-x86_64-rhel70-3.0.5.tgz’ saved [50203367/50203367]

首先进入/usr/local目录,下载这个文件需要root权限,输入su之后,输入自己的root密码,使用wget命令,下载MongoDB到/usr/local目录下; 有的时候官网上资源不容易下载,提供下载地址MongoDB3.0.5点击下载,访问密码0cdf

解压缩

1
2
# tar zxvf mongodb-linux-x86_64-rhel70-3.0.5.tgz 
# mv ./mongodb-linux-x86_64-rhel70-3.0.5 /usr/local/Mongodb3.0

使用tar zxvf 解压缩*.tgz文件,mv 命令将解压缩后的文件夹重命名为Mongodb3.0;

运行起来

在运行之前,需要建立两个文件夹:

[root@localhost local]# cd Mongodb3.0/
[root@localhost Mongodb3.0]# mkdir ./db
[root@localhost Mongodb3.0]# mkdir ./log

先进入/usr/local/Mongodb3.0文件夹:cd Mongodb3.0/ ,在当前文件夹中建立db和log文件夹。

[root@localhost Mongodb3.0]# cd ./bin/
[root@localhost bin]# ls
bsondump  mongodump    mongoimport  mongorestore  mongotop
mongo     mongoexport  mongooplog   mongos
mongod    mongofiles   mongoperf    mongostat
[root@localhost bin]# ./mongod --dbpath=../db --port=27017 --logpath=../log/mongodb.log --fork
about to fork child process, waiting until server is ready for connections.
forked process: 12246
child process started successfully, parent exiting

进入/usr/local/Mongodb3.0/bin/文件夹:cd ./bin/; bin文件夹下mongod为mongodb启动文件,运行命令 ./mongod –dbpath=../db –port=27017 –logpath=../log/mongodb.log –fork 启动Mongodb。 –dbpath 代表Mongodb数据存储的位置; –port 表示Mongodb所占用的端口号; –logpath 表示日志文件的路径;–fork 表示Mongodb以守护进程的形式启动。 现在可以看到 about to fork child process, waiting until server is ready for connections. forked process: 12246 child process started successfully, parent exiting 表示Mongodb已经正常启动!

停止Mongodb

按照上边的步骤,可以已守护进程的方式启动Mongodb,在需要停止Mongodb的时候,可以使用以下操作:

[root@localhost bin]# ps -e | grep mongod
12246 ?        00:00:04 mongod

键入命令 ps -e | grep mongod,可以看到正在运行的Mongodb的pid(进程号);

[root@localhost bin]# kill 12246
[root@localhost bin]# ps -e | grep mongod
[root@localhost bin]# 

得到进程号之后,使用kill命令来停止Mongodb,再次输入ps -e | grep mongod 终端就不会再有显示,表示进程已被杀死。

Redis 集群的搭建

这里需要说明以下,下载的时候选择支持集群模式的Redis3.0之后的版本。

下载

跟Mongodb一样,第一步肯定是先把文件下载下来:

[root@localhost bin]# cd /usr/local
[root@localhost local]# wget http://download.redis.io/releases/redis-3.0.3.tar.gz
--2015-08-01 13:02:27--  http://download.redis.io/releases/redis-3.0.3.tar.gz
Resolving download.redis.io (download.redis.io)... 109.74.203.151
Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1360959 (1.3M) [application/x-gzip]
Saving to: ‘redis-3.0.3.tar.gz.1’

100%[======================================>] 1,360,959    149KB/s   in 1m 41s 

2015-08-01 13:04:20 (13.2 KB/s) - ‘redis-3.0.3.tar.gz.1’ saved [1360959/1360959]

相比Mongodb,Redis文件还是很小的。 同样,在下载失败的时候,可以点击下载Redis3.0.3 访问密码dc5a

解压缩

[root@localhost local]# tar -zxvf redis-3.0.3.tar.gz.1 

将文件解压缩到/usr/local文件夹下。

单独实例先Run起来

这里与Mongodb不太一样,运行之前需要编译一下:

[root@localhost local]# cd redis-3.0.3/
[root@localhost redis-3.0.3]# make 

需要稍等一下,终端显示

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/usr/local/redis-3.0.3/src'

则表示make成功。

[root@localhost redis-3.0.3]# cd src/
[root@localhost src]# ./redis-server ../redis.conf 

16790:M 01 Aug 13:15:44.020 * The server is now ready to accept connections on port 6379

进入src文件夹下,键入命令:./redis-server ../redis.conf ,就可以启动Redis实例。正常的运行结果则如上所示。

在这里,因为不是以守护进程的形式启动的Redis,如果需要停止进行,在运行Redis的终端使用Ctrl+C来停止进程。

^C16790:signal-handler (1438406388) Received SIGINT scheduling shutdown...
16790:M 01 Aug 13:19:48.465 # User requested shutdown...
16790:M 01 Aug 13:19:48.465 * Saving the final RDB snapshot before exiting.
16790:M 01 Aug 13:19:48.507 * DB saved on disk
16790:M 01 Aug 13:19:48.507 # Redis is now ready to exit, bye bye...

Redis集群,High起来

Redis作为一个内存数据库,在实际的使用环境中,经常会受到机器内存大小的限制,在3.0之前的版本,集群需要采用proxy的方法,现在redis自身提供了cluster的支持。

集群搭建准备工作

集群的搭建需要使用Redis下src文件夹中redis-trib.rb,这是一个ruby程序,因此需要系统中有ruby环境。

root@localhost src]# yum install ruby 
[root@localhost src]# gem install redis

键入上边的命令,安装ruby 和 ruby 的redis 扩展。在安装扩展的时候,很有可能不能安装,可能是资源被墙了。可以使用我自己下载到的资源redis.gem 访问密码9727进行本地安装。

[root@localhost src]# gem install --local /usr/local/redis-3.0.0.gem 

将下载到的gem文件放到/usr/local目录下,使用上边的命令进行安装。这样,Redis集群的运行环境就搭建完毕。

要做redis集群,至少需要3个Master实例。 先把redis的配置文件redis.conf和redis-server文件放到一个文件夹中:

[root@localhost local]# cd /usr/local
[root@localhost local]# mkdir -p ./redis-cluster/6379
[root@localhost local]# cp ./redis-3.0.3/redis.conf ./redis-3.0.3/src/redis-server ./redis-cluster/6379/

然后将文件夹6379复制两份:

[root@localhost local]# cd ./redis-cluster/
[root@localhost redis-cluster]# cp -rf ./6379/ ./6380
[root@localhost redis-cluster]# cp -rf ./6379 ./6381

接下来,需要作的就是修改对应文件夹下的redis.conf文件。

port 6379 #不同目录,这里端口对应不同
cluster-enabled yes #开启集群功能
cluster-config-file nodes.conf #节点配置文件,这个文件是服务启动时自己配置创建的
cluster-node-timeout 6000
appendonly yes

建议将conf文件中的save字段都屏蔽了,在使用过程中,发现开启之后,redis写rdb文件的时候对集群性能有很大影响。

集群 走起

上边的步骤,完成了集群搭建的准备工作。下边要开始运行起来了! 先把三个redis实例启动起来。

[root@localhost src]# ps -ef | grep redis
root      18979  18297  0 14:24 pts/0    00:00:00 ./redis-server *:6379 [cluster]
root      19094  19057  0 14:25 pts/4    00:00:00 ./redis-server *:6380 [cluster]
root      19181  19146  0 14:25 pts/7    00:00:00 ./redis-server *:6381 [cluster]

键入 ps -ef | grep redis 可以看到三个redis实例都在运行。 接下来使用redis-trib.rb来将三个实例连接起来。

[root@localhost src]# cd /usr/local/redis-3.0.3/src/
[root@localhost src]# ./redis-trib.rb create --replicas 0 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381

终端会有这样的显示:

>>> Performing hash slots allocation on 3 nodes...
Using 3 masters:
127.0.0.1:6379
127.0.0.1:6380
127.0.0.1:6381
M: 4ee119039aebd0e1d48b3f52149a7940db34c6fb 127.0.0.1:6379
   slots:0-5460 (5461 slots) master
M: f6ad85a038a05e297130f6e112e223db54c47ea5 127.0.0.1:6380
   slots:5461-10922 (5462 slots) master
M: 0b97e4a7f8e5ced1f2f2471694b76dcf77ce1fc7 127.0.0.1:6381
   slots:10923-16383 (5461 slots) master
Can I set the above configuration? (type 'yes' to accept):

键入 yes

>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join.
>>> Performing Cluster Check (using node 127.0.0.1:6379)
M: 4ee119039aebd0e1d48b3f52149a7940db34c6fb 127.0.0.1:6379
   slots:0-5460 (5461 slots) master
M: f6ad85a038a05e297130f6e112e223db54c47ea5 127.0.0.1:6380
   slots:5461-10922 (5462 slots) master
M: 0b97e4a7f8e5ced1f2f2471694b76dcf77ce1fc7 127.0.0.1:6381
   slots:10923-16383 (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

测试一下,集群是否正常工作。

[root@localhost src]# ./redis-cli -c -h 127.0.0.1 -p 6379
127.0.0.1:6379> set key values
-> Redirected to slot [12539] located at 127.0.0.1:6381
OK

这样,集群就算搭建完成了。哈哈哈。

未完待续

至此,整个服务器缓存流程中的数据库部分已经搭建完毕,接下来要配置的是Web服务器和PHP的安装配置,我会在后续的文章中继续更新。