赞
踩
本文主要介绍从零开始搭建一个FastDFS集群,希望对你有所帮助。
1、安装libevent依赖库
FastDFS依赖libevent库,需要安装:
yum -y install libevent
2、安装libfastcommon
libfastcommon是FastDFS官方提供的,libfastcommon包含了FastDFS运行所需要的一些基础库。
- git clone https://github.com/happyfish100/libfastcommon.git --depth 1
- cd libfastcommon/
- ./make.sh && ./make.sh install #编译安装
3、安装fastdfs(storage和tracker一样)
- cd /Data/apps
- git clone https://github.com/happyfish100/fastdfs.git --depth 1
- cd fastdfs/
- ./make.sh && ./make.sh install #编译安装
4、拷贝配置文件
- cp /Data/apps/fastdfs/conf/http.conf /etc/fdfs/ #供nginx访问使用
- cp /Data/apps/fastdfs/conf/mime.types /etc/fdfs/ #供nginx访问使用
-
- #配置文件准备(Notice: 根据本机的角色,按需复制粘贴配置文件!)
- cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf
- cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
- cp /etc/fdfs/client.conf.sample /etc/fdfs/client.conf #客户端文件,测试用
此 module 用于避免上传完成后,且 group storage 同步文件完成前,客户端从正在同步的主机处下载文件导致的出错
- cd /Data/apps
-
- git clone https://github.com/happyfish100/fastdfs-nginx-module.git --depth 1
-
- cp /Data/apps/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs
- tracker 和 storage 均需要安装 nginx
- 对于 tracker, nginx 可以均衡负载到不同的 storage
- 对于 storage, nginx 可以提供反向代理,从而访问数据存储位置的文件
- 由于需要使用启用非默认 module , 故需要通过源码编译安装,不能使用 yum . 使用源码安装 nginx 的注意事项见《nginx - make install》
- wget http://nginx.org/download/nginx-1.15.7.tar.gz #下载 nginx mainline 版本的压缩包
- tar -zxvf nginx-1.15.4.tar.gz #解压
- cd nginx-1.15.7/
- #记得添加fastdfs-nginx-module模块
- # nginx configure arguments 内容丰富、配置自由,详见《nginx - make install》
- ./configure \
- --prefix=/usr/local/nginx \
- --user=nginx \
- --group=nginx \
- --conf-path=/etc/nginx/nginx.conf \
- --add-module=/usr/local/src/fastdfs-nginx-module/src/
- make && make install #编译安装
- 需要配置 nginx, tracker, storage 和 client
- tracker 和 storage 需要搭配 nginx 使用,故在本教程中, nginx 的配置都是关于 tracker 和 storage 的,与 client 无关。
- tracker, storage 和 client 都有自身的配置项,需要单独配置。
1、tracker配置
vim /etc/fdfs/tracker.conf
- port = 1001
- base_path = /Data/apps/fastdfs/tracker
- connect_timeout = 5
- max_connections = 1024
-
- # the method for selecting group to upload files
- # 0: round robin
- # 1: specify group
- # 2: load balance, select the max free space group to upload file
- store_lookup = 2
-
- # which storage server to upload file
- # 0: round robin (default)
- # 1: the first server order by ip address
- # 2: the first server order by priority (the minimal)
- # Note: if use_trunk_file set to true, must set store_server to 1 or 2
- store_server = 0
-
- # which path (means disk or mount point) of the storage server to upload file
- # 0: round robin
- # 2: load balance, select the max free space path to upload file
- store_path = 0
-
- # which storage server to download file
- # 0: round robin (default)
- # 1: the source storage server which the current file uploaded to
- download_server = 0

启动tracker
/usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf start|stop|restart
2、storage配置
vim /etc/fdfs/storage.conf
- port=1002
-
- #当前主机是什么组,就写什么组
- group_name=g1
-
- base_path=/Data/apps/fastdfs/storage
-
- heart_beat_interval = 30
-
- stat_report_interval = 60
-
- store_path0 = /Data/apps/fastdfs/storage_data
-
- subdir_count_per_path = 256
-
- tracker_server = 172.31.125.65:1001
-
- upload_priority = 10
-
- use_access_log = true

启动storage
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf start|stop|restart
成功画面
至此,tracker和storage都成功启动了
3、client配置
vim /etc/fdfs/client.conf
- base_path=/Data/apps/fastdfs/client
- tracker_server = 172.31.125.65:1001
mod_FastDFS配置(不是必须)
vim /etc/fdfs/mod_fastdfs.conf
- base_path=/Data/apps/fastdfs/module
-
- tracker_server= 172.31.125.65:1001
- url_have_group_name=true #url中包含group名称
- store_path0=/Data/apps/fastdfs/storage_data #指定文件存储路径
1、检测集群fdfs_monitor
/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
- [2020-02-22 17:42:17] DEBUG - base_path=/Data/apps/fastdfs/storage, connect_timeout=5, network_timeout=60, tracker_server_count=1, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=1, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0
-
- server_count=1, server_index=0
-
- tracker server is 172.31.125.65:1001
-
- group count: 1
-
- Group 1:
- group name = g1
- disk total space = 40,183 MB
- disk free space = 30,677 MB
- trunk free space = 0 MB
- storage server count = 1
- active server count = 1
- storage server port = 1002
- storage HTTP port = 8888
- store path count = 1
- subdir count per path = 256
- current write server index = 0
- current trunk file id = 0
-
- Storage 1:
- id = 172.31.125.65
- ip_addr = 172.31.125.65 ACTIVE
- http domain =
- version = 6.06
- join time = 2020-02-22 17:22:45
- up time = 2020-02-22 17:24:44
- total storage = 40,183 MB
- free storage = 30,677 MB
- upload priority = 10
- store_path_count = 1
- subdir_count_per_path = 256
- storage_port = 1002
- storage_http_port = 8888
- current_write_path = 0
- source storage id =
- if_trunk_server = 0
- connection.alloc_count = 256
- connection.current_count = 0
- connection.max_count = 0
- total_upload_count = 0
- success_upload_count = 0
- total_append_count = 0
- success_append_count = 0
- total_modify_count = 0
- success_modify_count = 0
- total_truncate_count = 0
- success_truncate_count = 0
- total_set_meta_count = 0
- success_set_meta_count = 0
- total_delete_count = 0
- success_delete_count = 0
- total_download_count = 0
- success_download_count = 0
- total_get_meta_count = 0
- success_get_meta_count = 0
- total_create_link_count = 0
- success_create_link_count = 0
- total_delete_link_count = 0
- success_delete_link_count = 0
- total_upload_bytes = 0
- success_upload_bytes = 0
- total_append_bytes = 0
- success_append_bytes = 0
- total_modify_bytes = 0
- success_modify_bytes = 0
- stotal_download_bytes = 0
- success_download_bytes = 0
- total_sync_in_bytes = 0
- success_sync_in_bytes = 0
- total_sync_out_bytes = 0
- success_sync_out_bytes = 0
- total_file_open_count = 0
- success_file_open_count = 0
- total_file_read_count = 0
- success_file_read_count = 0
- total_file_write_count = 0
- success_file_write_count = 0
- last_heart_beat_time = 2020-02-22 17:41:49
- last_source_update = 1970-01-01 08:00:00
- last_sync_update = 1970-01-01 08:00:00
- last_synced_timestamp = 1970-01-01 08:00:00
- [root@iZm5eetbb1qeqn5t6ynbyiZ ~]#

2、上传测试fdfs_upload_file
- [root@iZm5eetbb1qeqn5t6ynbyiZ swh]# fdfs_upload_file /etc/fdfs/client.conf conf.php
- g1/M00/00/00/rB99QV5Q-IiAJeOIAAAAH75Mdr0476.php
- [root@iZm5eetbb1qeqn5t6ynbyiZ swh]#
3、下载测试fdfs_download_file
- [root@iZm5eetbb1qeqn5t6ynbyiZ home]# fdfs_download_file /etc/fdfs/client.conf g1/M00/00/00/rB99QV5Q-IiAJeOIAAAAH75Mdr0476.php
- [root@iZm5eetbb1qeqn5t6ynbyiZ home]# ls
- rB99QV5Q-IiAJeOIAAAAH75Mdr0476.php
4、php上传和下载
上传或者下载,PHP通过安装fastdfs_client扩展完成。
tracker_get_connection();是通过读取本地的/etc/fdfs/client.conf配置文件的配置,连接storage进行上传和下载
参考文档:Fast DFS安装教程
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。