1. 本地命令使用
配置client.conf文件
[root@linux-node1 /etc/fdfs]# cp client.conf.sample client.conf # the base path to store log files base_path=/tmp # tracker_server can ocur more than once, and tracker_server format is # "host:port", host can be hostname or ip address tracker_server=192.168.56.11:22122 tracker_server=192.168.56.12:22122
上传文件
#上传本地文件,返回一个fid [root@linux-node1 ~]# fdfs_upload_file Usage: fdfs_upload_file[storage_ip:port] [store_path_index] [root@linux-node1 ~]# fdfs_upload_file /etc/fdfs/client.conf /etc/passwd group1/M00/00/00/wKg4C1m1enuAMUBfAAAJLig745w0396526 [root@linux-node1 ~]# cd /data/fdfs_storage/store/data/ [root@linux-node1 /data/fdfs_storage/store/data]# ls 00 08 10 18 20 28 30 38 40 48 50 58 60 68 70 78 80 88 90 98 A0 A8 B0 B8 C0 C8 D0 D8 E0 E8 F0 F8 01 09 11 19 21 29 31 39 41 49 51 59 61 69 71 79 81 89 91 99 A1 A9 B1 B9 C1 C9 D1 D9 E1 E9 F1 F9 02 0A 12 1A 22 2A 32 3A 42 4A 52 5A 62 6A 72 7A 82 8A 92 9A A2 AA B2 BA C2 CA D2 DA E2 EA F2 FA 03 0B 13 1B 23 2B 33 3B 43 4B 53 5B 63 6B 73 7B 83 8B 93 9B A3 AB B3 BB C3 CB D3 DB E3 EB F3 FB 04 0C 14 1C 24 2C 34 3C 44 4C 54 5C 64 6C 74 7C 84 8C 94 9C A4 AC B4 BC C4 CC D4 DC E4 EC F4 FC 05 0D 15 1D 25 2D 35 3D 45 4D 55 5D 65 6D 75 7D 85 8D 95 9D A5 AD B5 BD C5 CD D5 DD E5 ED F5 FD 06 0E 16 1E 26 2E 36 3E 46 4E 56 5E 66 6E 76 7E 86 8E 96 9E A6 AE B6 BE C6 CE D6 DE E6 EE F6 FE 07 0F 17 1F 27 2F 37 3F 47 4F 57 5F 67 6F 77 7F 87 8F 97 9F A7 AF B7 BF C7 CF D7 DF E7 EF F7 FF [root@linux-node1 /data/fdfs_storage/store/data]# pwd /data/fdfs_storage/store/data [root@linux-node1 /data/fdfs_storage/store/data]# cd 00 [root@linux-node1 /data/fdfs_storage/store/data/00]# ls 00 08 10 18 20 28 30 38 40 48 50 58 60 68 70 78 80 88 90 98 A0 A8 B0 B8 C0 C8 D0 D8 E0 E8 F0 F8 01 09 11 19 21 29 31 39 41 49 51 59 61 69 71 79 81 89 91 99 A1 A9 B1 B9 C1 C9 D1 D9 E1 E9 F1 F9 02 0A 12 1A 22 2A 32 3A 42 4A 52 5A 62 6A 72 7A 82 8A 92 9A A2 AA B2 BA C2 CA D2 DA E2 EA F2 FA 03 0B 13 1B 23 2B 33 3B 43 4B 53 5B 63 6B 73 7B 83 8B 93 9B A3 AB B3 BB C3 CB D3 DB E3 EB F3 FB 04 0C 14 1C 24 2C 34 3C 44 4C 54 5C 64 6C 74 7C 84 8C 94 9C A4 AC B4 BC C4 CC D4 DC E4 EC F4 FC 05 0D 15 1D 25 2D 35 3D 45 4D 55 5D 65 6D 75 7D 85 8D 95 9D A5 AD B5 BD C5 CD D5 DD E5 ED F5 FD 06 0E 16 1E 26 2E 36 3E 46 4E 56 5E 66 6E 76 7E 86 8E 96 9E A6 AE B6 BE C6 CE D6 DE E6 EE F6 FE 07 0F 17 1F 27 2F 37 3F 47 4F 57 5F 67 6F 77 7F 87 8F 97 9F A7 AF B7 BF C7 CF D7 DF E7 EF F7 FF [root@linux-node1 /data/fdfs_storage/store/data/00]# cd 00 [root@linux-node1 /data/fdfs_storage/store/data/00/00]# ls wKg4C1m1enuAMUBfAAAJLig745w0396526 [root@linux-node1 /data/fdfs_storage/store/data/00/00]# cat wKg4C1m1enuAMUBfAAAJLig745w0396526 root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin ...
下载文件
#下载刚才上传的文件 [root@linux-node1 /data/fdfs_storage/store/data/00/00]# md5sum wKg4C1m1enuAMUBfAAAJLig745w0396526 598c1d3c15dc2aedd47f4035a6e63dc6 wKg4C1m1enuAMUBfAAAJLig745w0396526 [root@linux-node1 ~]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKg4C1m1enuAMUBfAAAJLig745w0396526 [root@linux-node1 ~]# ls w* wKg4C1m1enuAMUBfAAAJLig745w0396526 [root@linux-node1 ~]# md5sum wKg4C1m1enuAMUBfAAAJLig745w0396526 598c1d3c15dc2aedd47f4035a6e63dc6 wKg4C1m1enuAMUBfAAAJLig745w0396526 [root@linux-node1 ~]# md5sum /etc/passwd 598c1d3c15dc2aedd47f4035a6e63dc6 /etc/passwd
查看文件
#查看文件信息 [root@linux-node1 ~]# fdfs_file_info /etc/fdfs/client.conf group1/M00/00/00/wKg4C1m1enuAMUBfAAAJLig745w0396526 source storage id: 0 source ip address: 192.168.56.11 file create timestamp: 2017-09-11 01:46:35 file size: 2350 file crc32: 675013532 (0x283BE39C)
删除文件
[root@linux-node1 ~]# fdfs_delete_file /etc/fdfs/client.conf group1/M00/00/00/wKg4C1m1enuAMUBfAAAJLig745w0396526
文件追加
[root@linux-node1 ~]# echo "hello" > append.txt [root@linux-node1 ~]# echo "world" > append2.txt [root@linux-node1 ~]# fdfs_upload_appender /etc/fdfs/client.conf append.txt group1/M00/00/00/wKg4C1m1f3uEX17hAAAAAHcc3SA370.txt [root@linux-node1 ~]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKg4C1m1f3uEX17hAAAAAHcc3SA370.txt [root@linux-node1 ~]# cat wKg4C1m1f3uEX17hAAAAAHcc3SA370.txt hello [root@linux-node1 ~]# fdfs_append_file /etc/fdfs/client.conf group1/M00/00/00/wKg4C1m1f3uEX17hAAAAAHcc3SA370.txt append2.txt [root@linux-node1 ~]# fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKg4C1m1f3uEX17hAAAAAHcc3SA370.txt [root@linux-node1 ~]# cat wKg4C1m1f3uEX17hAAAAAHcc3SA370.txt hello world
fastdfs状态查看
#fastdfs状态查看: fdfs_monitor [root@linux-node1 ~]# fdfs_monitor Usage: fdfs_monitor[-h] [list|delete|set_trunk_server[storage_id]] [2017-09-11 02:29:40] DEBUG - base_path=/tmp, connect_timeout=30, network_timeout=60, tracker_server_count=2, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0 server_count=2, server_index=1 tracker server is 192.168.56.12:22122 group count: 1 Group 1: group name = group1 disk total space = 48580 MB disk free space = 26262 MB trunk free space = 0 MB storage server count = 2 active server count = 2 storage server port = 23000 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 = 192.168.56.11 ip_addr = 192.168.56.11 (mystorage1) ACTIVE http domain = version = 5.11 join time = 2017-09-11 01:33:02 up time = 2017-09-11 02:26:05 total storage = 48580 MB free storage = 26262 MB upload priority = 10 store_path_count = 1 subdir_count_per_path = 256 storage_port = 23000 storage_http_port = 8888 current_write_path = 0 source storage id = if_trunk_server = 0 connection.alloc_count = 256 connection.current_count = 1 connection.max_count = 1 total_upload_count = 4 success_upload_count = 4 total_append_count = 1 success_append_count = 1 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 = 2 success_delete_count = 2 total_download_count = 5 success_download_count = 5 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 = 4712 success_upload_bytes = 4712 total_append_bytes = 6 success_append_bytes = 6 total_modify_bytes = 0 success_modify_bytes = 0 stotal_download_bytes = 4724 success_download_bytes = 4724 total_sync_in_bytes = 0 success_sync_in_bytes = 0 total_sync_out_bytes = 0 success_sync_out_bytes = 0 total_file_open_count = 10 success_file_open_count = 10 total_file_read_count = 5 success_file_read_count = 5 total_file_write_count = 5 success_file_write_count = 5 last_heart_beat_time = 2017-09-10 17:25:50 last_source_update = 2017-09-11 02:21:53 last_sync_update = 1970-01-01 08:00:00 last_synced_timestamp = 1970-01-01 08:00:00 Storage 2: id = 192.168.56.12 ip_addr = 192.168.56.12 (mystorage2) ACTIVE http domain = version = 5.11 join time = 2017-09-10 16:29:26 up time = 2017-09-10 17:25:24 total storage = 48580 MB free storage = 43669 MB upload priority = 10 store_path_count = 1 subdir_count_per_path = 256 storage_port = 23000 storage_http_port = 8888 current_write_path = 0 source storage id = 192.168.56.11 if_trunk_server = 0 connection.alloc_count = 256 connection.current_count = 1 connection.max_count = 1 total_upload_count = 0 success_upload_count = 0 ... last_synced_timestamp = 2017-09-11 02:21:54 (-1s delay) #删除node2节点 [root@linux-node2 ~]# /etc/init.d/fdfs_storaged stop Stopping fdfs_storaged (via systemctl): [ OK ] [root@linux-node1 ~]# fdfs_monitor /etc/fdfs/client.conf delete group1 192.168.56.12 [2017-09-11 02:32:52] DEBUG - base_path=/tmp, connect_timeout=30, network_timeout=60, tracker_server_count=2, anti_steal_token=0, anti_steal_secret_key length=0, use_connection_pool=0, g_connection_pool_max_idle_time=3600s, use_storage_id=0, storage server id count: 0 server_count=2, server_index=0 tracker server is 192.168.56.11:22122 delete storage server group1::192.168.56.12 success [root@linux-node1 ~]# fdfs_monitor /etc/fdfs/client.conf Storage 2: id = 192.168.56.12 ip_addr = 192.168.56.12 (mystorage2) DELETED [root@linux-node2 ~]# /etc/init.d/fdfs_storaged start Starting fdfs_storaged (via systemctl): [ OK ] [root@linux-node1 ~]# fdfs_monitor /etc/fdfs/client.conf Storage 2: id = 192.168.56.12 ip_addr = 192.168.56.12 (mystorage2) OFFLINE 同步文件中...... Storage 2: id = 192.168.56.12 ip_addr = 192.168.56.12 (mystorage2) ACTIVE
2. Php访问FastDFS
安装php_client模块:https://github.com/happyfish100/fastdfs/tree/master/php_client
#php编译fastdfs_client模块 [root@linux-node2 php_client]# cd /usr/local/src/fastdfs-5.11/php_client/ [root@linux-node2 php_client]# /usr/local/php/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api No: 20131226 Zend Extension Api No: 220131226 [root@linux-node2 php_client]# ./configure --with-php-config=/usr/local/php/bin/php-config [root@linux-node2 php_client]# make && make install [root@linux-node2 php_client]# ls -l /usr/local/php/lib/php/extensions/no-debug-non-zts-20131226/ total 6992 -rwxr-xr-x 1 root root 398513 Sep 10 18:13 fastdfs_client.so -rwxr-xr-x. 1 root root 1351016 Jun 5 2016 imagick.so -rw-r--r--. 1 root root 1482544 Jun 5 2016 ioncube_loader_lin_5.6.so -rwxr-xr-x. 1 root root 383559 Jun 5 2016 memcached.so -rwxr-xr-x. 1 root root 505381 Jun 5 2016 memcache.so -rwxr-xr-x. 1 root root 1334840 Jun 5 2016 opcache.a -rwxr-xr-x. 1 root root 619139 Jun 5 2016 opcache.so -rwxr-xr-x. 1 root root 1072925 Jun 5 2016 redis.so
php_client配置文件设置
[root@linux-node2 php_client]# egrep -v "^;|^$" fastdfs_client.ini extension = fastdfs_client.so fastdfs_client.base_path = /tmp fastdfs_client.connect_timeout = 2 fastdfs_client.network_timeout = 60 fastdfs_client.log_level = info fastdfs_client.log_filename = fastdfs_client.http.anti_steal_secret_key = fastdfs_client.tracker_group_count = 1 fastdfs_client.tracker_group0 = /etc/fdfs/client.conf fastdfs_client.use_connection_pool = true fastdfs_client.connection_pool_max_idle_time = 3600 [root@linux-node2 php_client]# cat fastdfs_client.ini >> /usr/local/php/etc/php.ini
执行测试文件
[root@linux-node2 php_client]# /usr/local/php/bin/php fastdfs_test.php 5.11 fastdfs_tracker_make_all_connections result: 1 array(1) { ["group1"]=> array(13) { ["total_space"]=> int(48580) ["free_space"]=> int(26263) ["trunk_free_space"]=> int(0) ...
3. fastdfs-nginx-module配置
nginx编译fastdfs-nginx-module模块
[root@linux-node1 /usr/local/src]# cd ~/nginx-1.10.1/ [root@linux-node1 ~/nginx-1.10.1]# ll /usr/local/nginx nginx/ nginx-1.10.1/ [root@linux-node1 ~/nginx-1.10.1]# ./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --add-module=/usr/local/src/fastdfs-nginx-module/src/ [root@linux-node1 ~/nginx-1.10.1]# make && make install
配置nginx vhost文件
[root@linux-node1 ~/nginx-1.10.1]# cp /usr/local/src/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs/ [root@linux-node1 ~/nginx-1.10.1]# cd /usr/local/src/fastdfs-5.11/conf [root@linux-node1 /usr/local/src/fastdfs-5.11/conf]# cp anti-steal.jpg http.conf mime.types /etc/fdfs/ [root@linux-node1 /usr/local/src/fastdfs-5.11/conf]# touch /var/log/mod_fastdfs.log [root@linux-node1 /usr/local/src/fastdfs-5.11/conf]# chown www.www /var/log/mod_fastdfs.log [root@linux-node1 /usr/local/nginx/conf/vhost_online]# cat fdfs.conf server { listen 80; server_name 192.168.56.11; access_log logs/fdfs.log; location /group1/M00 { root /data/fdfs_storage/store/; ngx_fastdfs_module; } }
编辑/etc/fdfs/mod_fastdfs.conf文件
tracker_server=192.168.56.11:22122 tracker_server=192.168.56.11:22122 url_have_group_name = true store_path0=/data/fdfs_storage/store log_filename=/var/log/mod_fastdfs.log # response mode when the file not exist in the local file system ## proxy: get the content from other storage server, then send to client ## redirect: redirect to the original storage server (HTTP Header is Location) response_mode=proxy
查看效果
[root@linux-node1 ~]# /usr/local/nginx/sbin/nginx -s stop; /usr/local/nginx/sbin/nginx [root@linux-node1 ~]# fdfs_upload_file /etc/fdfs/client.conf starcraft.jpg group1/M00/00/00/wKg4DFm1JLSAOkwaAARjWqsUfd4391.jpg