准备:新建三台虚拟机:Tracker、Storage1、Storage2
三台机器都需安装FastDFS
其中作为Tracker的机器安装Nginx用于负载均衡
作为Storage的机器安装Nginx用于使用http请求访问上传的文件
1、安装Fastdfs
Fastdfs使用rpm文件安装,将安装包放入/opt下
执行命令:
左侧为安装完成后,右侧为未安装时
执行:
cd /opt
rpm -ivh libfastcommon-1.13-2.x86_64.rpm
rpm -ivh fastdfs-5.0.5-1.el6.x86_64.rpm
rpm -ivh db-6.1.19-1.x86_64.rpm
rpm -ivh FastDHT-2.0.1-1.x86_64.rpm
2、配置Fastdfs(区分 tracker 与 storage )
2.1、配置tracker
进入/etc/fdfs文件夹下,编辑 tracker.conf 文件,配置以下几个选项
// 启用该配置文件
disabled=false
port=22122
base_path=/home/fastdfs/tracker
然后启动tracker,使用命令
/usr/local/bin/fdfs_trackerd /etc/fdfs/tracker.conf restart
需注意对应目录下是否有这些文件,或许 fdfs_trackerd 在 /usr/bin 下
启动完成后查看22122端口是否在运行
若 -bash: netstat: 未找到命令 ,则执行 yum install net-tools 安装
netstat -unltp|grep fdfs
启动成功
2.2、配置storage
进入/etc/fdfs文件夹下,编辑 storage.conf 文件,配置以下几个选项
disabled=false
group_name=group1
port=23000
// 日志存放位置
base_path=/home/fastdfs/storage
// 存储路径个数
store_path_count=1
// 存储路径
store_path0=/home/fastdfs/storage
// tracker服务器的IP地址和端口号
tracker_server=192.168.197.128:22122
然后执行以下命令启动
/usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf restart
但启动报错 db_log_verify from install of db-6.1.19-1.x86_64 conflicts with file from XXX
回到之前安装rpm文件步骤时发现,其实 db-6.1.19-1.x86_64.rpm 并没有安装成功!
使用同事Centos 6.5的虚拟机安装成功了!猜测是现有的rpm文件问题
新建虚拟机后重新安装、配置、启动,执行netstat -unltp|grep fdfs 查看没有进程
查看日志:
[2019-02-18 07:17:14] ERROR - file: fdht_client/fdht_func.c, line: 462, "group0" 's value "storage1" is invalid, invalid hostname: storage1
[2019-02-18 07:17:14] CRIT - exit abnormally!
大致意思 storage1 为hostname不对,到 /etc/fdht 路径下打开 fdht_servers.conf 发现
group_count = 1
group0 = storage1:11411
立马将 storage1 修改为 tracker的ip
再次启动
[2019-02-18 07:33:31] ERROR - file: storage_ip_changed_dealer.c, line: 180, connect to tracker server 192.168.197.128:22000 fail, errno: 113, error info: No route to host
发现 storage1 ping tracker 通畅,那就是端口问题
tracker开启的端口为:22122
而storage1 连接的端口却是22000,应该是storage.conf中配置问题
与网上文章对比一下发现,!!!
修改为:22122后,再次启动,成功!
查看storage 和 tracker 是否通信
/opt/fdfs/usr/bin/fdfs_monitor /etc/fdfs/storage.conf
fdfs_monitor 可以通过命令 find / -name fdfs_monitor 查找
3、简单上传文件的测试:
在tracker服务器使用命令上传文件,文件存储到storage1服务器中去
/usr/local/bin/fdfs_upload_file /etc/fdfs/client.conf timg.jpg
报了个错
upload file fail, error no: 2, error info: No such file or directory
修改下命令,指定上传文件的绝对路径
/usr/local/bin/fdfs_upload_file /etc/fdfs/client.conf /home/fastdfs/timg.jpg
报了另一个错
ERROR - file: connection_pool.c, line: 110, connect to 192.168.197.132:23000 fail, errno: 113, error info: No route to host
猜测storage防火墙重启又开启了,关闭再次执行命令,连接完成
但是再报一个错,
ERROR - file: tracker_proto.c, line: 48, server: 192.168.197.132:23000, response status 2 != 0
upload file fail, error no: 2, error info: No such file or directory
查找资料说是storage配置文件原因,然后去storage修改了 base_path=/home/fastdfs/storage
使用命令
/usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf restart
重启报错:
[2019-02-19 10:02:04] ERROR - file: sockopt.c, line: 770, bind port 23000 failed, errno: 98, error info: Address already in use.
[2019-02-19 10:02:04] CRIT - exit abnormally!
这是因为重启未成功,端口还在占用,我的解决方法是找到该进程,kill 后重启就可以了
[root@localhost ~]# ps -ef | grep storage
fastdfs 2759 1 0 17:29 ? 00:00:00 /usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf
root 2941 2924 0 18:05 pts/0 00:00:00 grep storage
[root@localhost ~]# kill -9 2759
[root@localhost ~]# ps -ef | grep storage
root 2945 2924 0 18:05 pts/0 00:00:00 grep storage
[root@localhost ~]# /usr/local/bin/fdfs_storaged /etc/fdfs/storage.conf restart
再次执行上传
[root@localhost ~]# /usr/local/bin/fdfs_upload_file /etc/fdfs/client.conf /home/fastdfs/timg.jpg
[2019-02-19 18:16:40] ERROR - file: tracker_proto.c, line: 48, server: 192.168.197.132:23000, response status 2 != 0
upload file fail, error no: 2, error info: No such file or directory
到storage服务器中storage文件夹下log查看日志
[2019-02-18 18:16:39] ERROR - file: fdht_client/fdht_proto.c, line: 197, connect to 192.168.197.128:11411 fail, errno: 111, error info: Connection refused
[2019-02-18 18:16:39] ERROR - file: fdht_client/fdht_proto.c, line: 197, connect to 192.168.197.128:11411 fail, errno: 111, error info: Connection refused
[2019-02-18 18:16:39] ERROR - file: storage_service.c, line: 2669, client ip: 192.168.197.128, fdht_set fail,errno: 2, error info: No such file or directory
此时防火墙已关闭,但11411端口是如何设置的呢
查找到 /etc/fdht 下 fdhtd.conf 文件中设置了 port=11411
果然。fdhtd没有启动好的原因:
FastDHT是分布式哈希系统(DHT),为的是实现文件去重
使用命令启动:
/usr/local/bin/fdhtd /etc/fdht/fdhtd.conf
监听11411端口
netstat -an | grep 11411
测试上传
/usr/local/bin/fdfs_test /etc/fdfs/client.conf upload /home/fastdfs/timg.jpg
完成,同时在storage服务器下
/home/fastdfs/file/data/00/00
找到该文件,但直接使用该URL进行访问失败,网上讲fastdfs4.05以后需要配置Nginx才能使用URL访问。
FastDFS在v4.05版本之后就不提供http支持了,所以,需要结合其他服务器,结合nginx,安装fastdfs-nginx-module模块