FastDFS安装

下载安装 libfastcommon
libfastcommon是从 FastDFS 和 FastDHT 中提取出来的公共 C 函数库,基础环境,安装即可 。
git clone https://github.com/happyfish100/libfastcommon.git
[[email protected] tools]# cd libfastcommon/
[[email protected] libfastcommon]# ./make.sh
[[email protected] libfastcommon]# ./make.sh install

下载安装FastDFS
[[email protected] tools]# git clone https://github.com/happyfish100/fastdfs.git
[[email protected] tools]# cd fastdfs/
[[email protected] fastdfs]# ./make.sh
[[email protected] fastdfs]# ./make.sh install

安装完成后默认配置文件
[[email protected] fastdfs]# ll /etc/fdfs/
total 24
-rw-r–r-- 1 root root 1461 Mar 14 22:25 client.conf.sample
-rw-r–r-- 1 root root 7978 Mar 14 22:25 storage.conf.sample
-rw-r–r-- 1 root root 105 Mar 14 22:25 storage_ids.conf.sample
-rw-r–r-- 1 root root 7441 Mar 14 22:25 tracker.conf.sample

默认启动文件
[[email protected] fastdfs]# ll /etc/init.d/fdfs_*
-rwxr-xr-x 1 root root 961 Mar 14 22:25 /etc/init.d/fdfs_storaged
-rwxr-xr-x 1 root root 963 Mar 14 22:25 /etc/init.d/fdfs_trackerd

修改启动文件
[[email protected] fastdfs]# sed -i ‘s#/usr/local/bin/#/usr/bin/#g’ /etc/init.d/fdfs_storaged
[[email protected] fastdfs]# sed -i ‘s#/usr/local/bin/#/usr/bin/#g’ /etc/init.d/fdfs_trackerd

创建存储目录
[[email protected] fastdfs]# mkdir -p /data/fdfs_tracker/
[[email protected] fastdfs]# mkdir -p /data/fdfs_storage/{base,store}
[[email protected] fastdfs]# tree /data/
/data/
|-- fdfs_storage
| |-- base
| -- store– fdfs_tracker

配置FastDFS跟踪器(Tracker)
跟踪服务默认端口(Tracker):22122
[[email protected] fastdfs]# cd /etc/fdfs/
[[email protected] fdfs]# cp tracker.conf.sample tracker.conf
[[email protected] fdfs]# vim tracker.conf
22 base_path=/data/fdfs_tracker

46 #the method of selecting group to upload files 存储文件的方式
47 #0: round robin 轮训
48 #1: specify group 指定group
49 #2: load balance, select the max free space group to upload file 选择剩余空间最多group
50 store_lookup=2

启动Tracker
[[email protected] fdfs]# /etc/init.d/fdfs_trackerd start
[[email protected] fdfs]# lsof -i :22122
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
fdfs_trac 2779 root 5u IPv4 56196 0t0 TCP *:22122 (LISTEN)

Tracker服务启动成功后,会在指定目录下创建data、logs两个目录。目录结构如下
[[email protected] fdfs]# tree /data/fdfs_tracker/
/data/fdfs_tracker/
|-- data
| |-- fdfs_trackerd.pid pid文件
| -- storage_changelog.dat 数据变动日志文件– logs
`-- trackerd.log tracker server 日志文件

配置 FastDFS 存储 (Storage)
storage server 服务端口 23000
[[email protected] fastdfs]# cd /etc/fdfs/
[[email protected] fdfs]# cp storage.conf.sample storage.conf
[[email protected] fdfs]# vim storage.conf
41 base_path=/data/fdfs_storage/base 日志目录地址
109 store_path0=/data/fdfs_storage/store 真正存储数据的目录
119 tracker_server=192.168.56.7:22122 有几台追踪服务器就写几个
120 tracker_server=192.168.56.8:22122

启动storage
[[email protected] fdfs]# /etc/init.d/fdfs_storaged start
[[email protected] fdfs]# lsof -i :23000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
fdfs_stor 2811 root 5u IPv4 123278 0t0 TCP *:inovaport1 (LISTEN)

启动后会在指定目录下创建 data 和 logs目录 记录着 Storage Server 的信息
[[email protected] fdfs]# tree /data/fdfs_storage/base/
/data/fdfs_storage/base/
|-- data
| |-- fdfs_storaged.pid pid文件
| |-- storage_stat.dat 记录着本storage节点 文件的 增删改读 的次数
| -- sync | |-- binlog.000 同步时使用的 记录着自己节点的文件变化 |– binlog.index
-- logs– storaged.log

store创建data目录里面创建N * N 个子目录 实际存储目录

查看Storage和Tracker是否在通信
[[email protected] fdfs]# fdfs_monitor /etc/fdfs/storage.conf
FastDFS安装
注:出现ACTIVE表示成功

上传文件测试
与Fastdfs通讯只能通过API
配置自带的通讯工具
[[email protected] fdfs]# cd /etc/fdfs/
[[email protected] fdfs]# cp client.conf.sample client.conf
[[email protected] fdfs]# vim client.conf
10 base_path=/var/log/ 存放日志路径
14 tracker_server=192.168.56.7:22122 追踪服务器地址

上传文件
[[email protected] fdfs]# fdfs_upload_file /etc/fdfs/client.conf /etc/passwd
group1/M00/00/00/wKg4B1yKcUeAEXVGAAAEKHTvTHY4632631

查看上传文件信息
[[email protected] ~]# fdfs_file_info /etc/fdfs/client.conf
Usage: fdfs_file_info <config_file> <file_id>
[[email protected] ~]# fdfs_file_info /etc/fdfs/client.conf group1/M00/00/00/wKg4B1yKcUeAEXVGAAAEKHTvTHY4632631
source storage id: 0
source ip address: 192.168.56.7
file create timestamp: 2019-03-14 23:20:39
file size: 1064
file crc32: 1961839734 (0x74EF4C76)

下载文件
[[email protected] ~]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKg4B1yKcUeAEXVGAAAEKHTvTHY4632631
与源文件MD5比对
[[email protected] ~]# md5sum wKg4B1yKcUeAEXVGAAAEKHTvTHY4632631
0ae03b0180d749ad73951ed52767771e wKg4B1yKcUeAEXVGAAAEKHTvTHY4632631
[[email protected] ~]# md5sum /etc/passwd
0ae03b0180d749ad73951ed52767771e /etc/passwd

为上传文件追加内容
[[email protected] ~]# echo “hello” >1.txt
[[email protected] ~]# echo “word” >2.txt
[[email protected] ~]# fdfs_upload_appender /etc/fdfs/client.conf 1.txt
group1/M00/00/00/wKg4B1yLBW-EJppPAAAAADY6MCA477.txt
[[email protected] ~]# fdfs_append_file /etc/fdfs/client.conf group1/M00/00/00/wKg4B1yLBW-EJppPAAAAADY6MCA477.txt 2.txt
[[email protected] ~]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKg4B1yLBW-EJppPAAAAADY6MCA477.txt
[[email protected] ~]# cat wKg4B1yLBW-EJppPAAAAADY6MCA477.txt
hello
word

删除上传文件
[[email protected] ~]# fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKg4B1yLBW-EJppPAAAAADY6MCA477.txt