下载安装 libfastcommon
libfastcommon是从 FastDFS 和 FastDHT 中提取出来的公共 C 函数库,基础环境,安装即可 。
git clone https://github.com/happyfish100/libfastcommon.git
[root@tom01 tools]# cd libfastcommon/
[root@tom01 libfastcommon]# ./make.sh
[root@tom01 libfastcommon]# ./make.sh install
下载安装FastDFS
[root@tom01 tools]# git clone https://github.com/happyfish100/fastdfs.git
[root@tom01 tools]# cd fastdfs/
[root@tom01 fastdfs]# ./make.sh
[root@tom01 fastdfs]# ./make.sh install
安装完成后默认配置文件
[root@tom01 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
默认启动文件
[root@tom01 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
修改启动文件
[root@tom01 fastdfs]# sed -i ‘s#/usr/local/bin/#/usr/bin/#g’ /etc/init.d/fdfs_storaged
[root@tom01 fastdfs]# sed -i ‘s#/usr/local/bin/#/usr/bin/#g’ /etc/init.d/fdfs_trackerd
创建存储目录
[root@tom01 fastdfs]# mkdir -p /data/fdfs_tracker/
[root@tom01 fastdfs]# mkdir -p /data/fdfs_storage/{base,store}
[root@tom01 fastdfs]# tree /data/
/data/
|-- fdfs_storage
| |-- base
| -- store
– fdfs_tracker
配置FastDFS跟踪器(Tracker)
跟踪服务默认端口(Tracker):22122
[root@tom01 fastdfs]# cd /etc/fdfs/
[root@tom01 fdfs]# cp tracker.conf.sample tracker.conf
[root@tom01 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
[root@tom01 fdfs]# /etc/init.d/fdfs_trackerd start
[root@tom01 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两个目录。目录结构如下
[root@tom01 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
[root@tom01 fastdfs]# cd /etc/fdfs/
[root@tom01 fdfs]# cp storage.conf.sample storage.conf
[root@tom01 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
[root@tom01 fdfs]# /etc/init.d/fdfs_storaged start
[root@tom01 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 的信息
[root@tom01 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是否在通信
[root@tom01 fdfs]# fdfs_monitor /etc/fdfs/storage.conf
注:出现ACTIVE表示成功
上传文件测试
与Fastdfs通讯只能通过API
配置自带的通讯工具
[root@tom01 fdfs]# cd /etc/fdfs/
[root@tom01 fdfs]# cp client.conf.sample client.conf
[root@tom01 fdfs]# vim client.conf
10 base_path=/var/log/ 存放日志路径
14 tracker_server=192.168.56.7:22122 追踪服务器地址
上传文件
[root@tom01 fdfs]# fdfs_upload_file /etc/fdfs/client.conf /etc/passwd
group1/M00/00/00/wKg4B1yKcUeAEXVGAAAEKHTvTHY4632631
查看上传文件信息
[root@tom01 ~]# fdfs_file_info /etc/fdfs/client.conf
Usage: fdfs_file_info
[root@tom01 ~]# 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)
下载文件
[root@tom01 ~]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKg4B1yKcUeAEXVGAAAEKHTvTHY4632631
与源文件MD5比对
[root@tom01 ~]# md5sum wKg4B1yKcUeAEXVGAAAEKHTvTHY4632631
0ae03b0180d749ad73951ed52767771e wKg4B1yKcUeAEXVGAAAEKHTvTHY4632631
[root@tom01 ~]# md5sum /etc/passwd
0ae03b0180d749ad73951ed52767771e /etc/passwd
为上传文件追加内容
[root@tom01 ~]# echo “hello” >1.txt
[root@tom01 ~]# echo “word” >2.txt
[root@tom01 ~]# fdfs_upload_appender /etc/fdfs/client.conf 1.txt
group1/M00/00/00/wKg4B1yLBW-EJppPAAAAADY6MCA477.txt
[root@tom01 ~]# fdfs_append_file /etc/fdfs/client.conf group1/M00/00/00/wKg4B1yLBW-EJppPAAAAADY6MCA477.txt 2.txt
[root@tom01 ~]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKg4B1yLBW-EJppPAAAAADY6MCA477.txt
[root@tom01 ~]# cat wKg4B1yLBW-EJppPAAAAADY6MCA477.txt
hello
word
删除上传文件
[root@tom01 ~]# fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKg4B1yLBW-EJppPAAAAADY6MCA477.txt