mogilefs工作原理
我们来做一个这样的
上面的memcached应该放在,nginx旁边。
由nginx去查询memcached,没有再找tracker,找到后由nginx缓存到memcached..
2,rpm安装
[root@node2 ~]# ls
MogileFS-Server-2.46-2.el6.noarch.rpm
MogileFS-Server-mogilefsd-2.46-2.el6.noarch.rpm
MogileFS-Server-mogstored-2.46-2.el6.noarch.rpm
MogileFS-Utils-2.19-1.el6.noarch.rpm
perl-Danga-Socket-1.61-5.el6.noarch.rpm
perl-IO-AIO-3.71-2.el6.x86_64.rpm
perl-IO-stringy-2.110-10.1.el6.noarch.rpm
perl-MogileFS-Client-1.14-1.el6.noarch.rpm
perl-Net-Netmask-1.9015-8.el6.noarch.rpm
perl-Perlbal-1.78-1.el6.noarch.rpm
perl-Sys-Syscall-0.23-1.el6.noarch.rpm
[root@node2 ~]# yum install *.rpm -y
安装mysql
# yum -y install mysql-server mysql
#service mysqld start
授权
mysql> update user set password=password('redhat') where user='root';
mysql> grant all on mogilefs.* to mogilefs@'192.168.93.%' identified by 'redhat';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
添加用户
# useradd -r mogilefs
提供一个配置文件
mkdir /etc/mogilefs
vim /etc/mogilefs/mogilefsd.conf
# Enable daemon mode to work in background and use syslog
daemonize = 1
# Where to store the pid of the daemon (must be the same in the init script)
pidfile = /var/run/mogilefsd/mogilefsd.pid
# Database connection information
db_dsn = DBI:mysql:mogilefs:host=192.168.93.103
db_user = mogilefs
db_pass = redhat
# IP:PORT to listen on for mogilefs client requests
listen = 192.168.93.102:7001
# Optional, if you don't define the port above.
conf_port = 7001
# Number of query workers to start by default.
query_jobs = 10
# Number of delete workers to start by default.
delete_jobs = 1
# Number of replicate workers to start by default.
replicate_jobs = 5
# Number of reaper workers to start by default.
# (you don't usually need to increase this)
reaper_jobs = 1
改权限
# chown -R mogilefs.mogilefs /etc/mogilefs/
#chown -R mogilefs.mogilefs /var/run/mogilefsd/
初始化数据库
mogdbsetup --dbhost=192.168.93.103 --dbrootuser=root --dbrootpass=redhat --dbuser=mogilefs --dbpass=redhat --dbname=mogilefs
启动服务
service mogilefsd start
配置storage
[root@node2 var]# vim /etc/mogilefs/mogstored.conf
maxconns = 10000
httplisten = 0.0.0.0:7500
mgmtlisten = 0.0.0.0:7501
docroot = /data/mogilefs
# mkdir -pv /data/mogilefs/dev1
# chown -R mogilefs.mogilefs /data/mogilefs/
启动服务
# /etc/init.d/mogstored start
添加节点
[root@node2 ~]# mogadm --trackers=192.168.93.102:7001 host add node2.xy.com --ip=192.168.93.102 --status=alive
[root@node2 ~]# mogadm --trackers=192.168.93.102:7001 host list
node2.xy.com [1]: alive
IP: 192.168.93.102:7500
node3.xy.com [2]: alive
IP: 192.168.93.103:7500
添加设备
[root@node2 ~]# mogadm --trackers=192.168.93.103:7001 device add node2.xy.com 1
[root@node2 ~]# mogadm --trackers=192.168.93.103:7001 device add node3.xy.com 2
添加设备时所使用的设备ID号必须要/var/mogdata中的编号一致
[root@node2 ~]# mogadm --trackers=192.168.93.103:7001 device list
node2.xy.com [1]: alive
used(G) free(G) total(G) weight(%)
dev1: alive 0.392 18.294 18.686 100
[root@node2 ~]# mogadm --trackers=192.168.93.102:7001 domain add image1
[root@node2 ~]# mogadm --trackers=192.168.93.102:7001 domain list
domain class mindevcount replpolicy hashtype
-------------------- -------------------- ------------- ------------ -------
image1 default 2 MultipleHosts() NONE
domain :区域
class:复制文件的最小单位 (最大为64M,如果一个单文件超出此大小将拆分为多个存储)
上传文件测试
# wget http://news.xj163.cn/tour/attachement/jpg/site2/20110429/001bb950e25a0f24eeb450.jpg
# mv 001bb950e25a0f24eeb450.jpg flo.jpg
# mogupload --trackers=192.168.93.103:7001 --domain=image1 --key='/test/flo.jpg' --file='./flo.jpg'
# moglistkeys --trackers=192.168.93.102:7001 --domain=image1
# mogfileinfo --trackers=192.168.93.102:7001 --domain=image1 --key='/test/flo.jpg'
- file: /test/flo.jpg
class: default
devcount: 2
domain: image1
fid: 7
key: /test/flo.jpg
length: 177504
- http://192.168.93.102:7500/dev1/0/000/000/0000000007.fid
- http://192.168.93.103:7500/dev2/0/000/000/0000000007.fid
可以看到没有问题了
安装nginx反代
要下载模块
可以去这下载:https://github.com/vkholodkov/nginx-mogilefs-module
编译nginx并加入模块
./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre --add-module=../nginx-mogilefs-module-master --without-http_ssl_module
useradd nginx -r
提供一个nginx的启动脚本
#!/bin/sh # # nginx - this script starts and stops the nginx daemon # # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then # echo "creating" $value mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
nginx的配置文件
upstream mogilefs { server 192.168.93.102:7001; server 192.168.93.103:7001; } server { listen 80; server_name localhost; location ~* ^(.*)$ { mogilefs_tracker mogilefs; mogilefs_domain image1; mogilefs_noverify on; mogilefs_pass $1 { proxy_pass $mogilefs_path; proxy_hide_header Content-Type; proxy_buffering off; } }
#service nginx start
在nginx上要指定domain,这样才能保证是取的这个文件,因为在不同的domain上key是可以重复的
默认情况下nginx只把文件名当key向后传递,所以要自己指定传递的key
启用memcached
安装Cache::Memcahced
cpan
>install Cache::Memcached
在tracker上启用memcached,暂时还不知道怎么用nginx使用memcahce存,知道的教教我。。
# mogadm --trackers=192.168.93.103:7001 settings set memcache_servers 192.168.93.102:11211
service mysqld stop
当停止mysqld后图片还是能访问
service memcached stop
又把memcahced停了后就不能访问了
启动mysqld又好了。。。
就不玩这么无聊的游戏了
[root@node2 nginx]# memcached-tool 127.0.0.1:11211 stats|egrep \(get\|set\)
cmd_get 14
cmd_set 2
get_hits 12
get_misses 2