安装nginx和nginx-gridfs和mongodb
1.安装依赖包:
1 | [root@mongo_rs1 ~]# yum -y install pcre-devel openssl-devel zlib-devel git gcc gcc-c++ |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [root@mongo_rs1 zhuyr]# git clone https://github.com/mdirolf/nginx-gridfs.git [root@mongo_rs1 zhuyr]# cd nginx-gridfs/ [root@mongo_rs1 nginx-gridfs]# git checkout v0.8 [root@mongo_rs1 nginx-gridfs]# git branch [root@mongo_rs1 nginx-gridfs]# git submodule init #子模组 'mongo-c-driver' (git://github.com/mongodb/mongo-c-driver.git) 已为路径 'mongo-c-driver' 注册 [root@mongo_rs1 nginx-gridfs]# git submodule update 正克隆到 'mongo-c-driver' ... remote: Counting objects: 101193, done. remote: Compressing objects: 100% (128/128), done. remote: Total 101193 (delta 159), reused 195 (delta 127), pack-reused 100938 接收对象中: 100% (101193/101193), 49.45 MiB | 4.55 MiB/s, done. 处理 delta 中: 100% (88309/88309), done. 子模组路径 'mongo-c-driver' :检出 '74cc0b8005fbbc84ec4a0003ff80c68f9e9cc7de' |
2.安装nginx
1 2 3 4 5 | [root@mongo_rs1 zhuyr]# wget http://nginx.org/download/nginx-1.7.9.tar.gz [root@mongo_rs1 zhuyr]# tar -zxvf nginx-1.7.9.tar.gz [root@mongo_rs1 zhuyr]# cd nginx-1.7.9/ [root@mongo_rs1 nginx-1.7.9]# ./configure --prefix=/usr/local/nginx --with-openssl=/usr/include/openssl --add-module=/zhuyr/nginx-gridfs [root@mongo_rs1 nginx-1.7.9]# make -j8 && make install -j8 |
1 2 3 4 5 | #如果报错 [root@mongo_rs1 nginx-1.7.9]# ./configure --prefix=/usr/local/nginx --with-openssl=/usr/include/openssl --add-module=/zhuyr/nginx-gridfs #把第3行的-Werror错误去掉 [root@mongo_rs1 nginx-1.7.9]# vi objs/Makefile [root@mongo_rs1 nginx-1.7.9]# make && make install |
3.修改配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | [root@mongo_rs1 nginx-1.7.9]# vi /usr/ local /nginx/conf/nginx.conf #添加以下内容 location /girdfstest/ { gridfs pics field=filename type=string; mongo 127.0.0.1:27017; } location /girdfsjava/ { gridfs pics field=_id type=objectid; mongo 127.0.0.1:27017; } #gridfstest:访问地址 #field=filename http://172.16.160.91/girdfstest/后面填写什么? #type=string; 字符窜 #field=_id #type=objectid; #pics:数据库 #mongo 127.0.0.1:10001 #mongo的服务器地址及端口 #启动nginx [root@mongo_rs1 nginx-1.7.9]# /usr/ local /nginx/sbin/nginx #配置修改后重新加载 [root@mongo_rs1 nginx-1.7.9]# /usr/ local /nginx/sbin/nginx -s reload |
4.安装mongodb
1 2 3 4 5 6 7 8 9 10 11 | vi /etc/yum.repos.d/mongodb-org-3.4.repo [mongodb-org-3.4] name =MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/ gpgcheck=0 enabled=1 gpgkey=https://www.mongodb.org/ static /pgp/server-3.4. asc $ yum -y install mongodb-org 查看修改配置文件 : vim /etc/mongod.conf<br>mongodb的配置文件中的bind_ip 默认为127.0.0.1,默认只有本机可以连接。 此时,需要将bind_ip配置为0.0.0.0,表示接受任何IP的连接。 启动mongodb :systemctl start mongod.service 停止mongodb :systemctl stop mongod.service |
如果报错:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | [root@ansible nginx-1.7.9]# journalctl -xe 7月 11 10:54:03 ansible.test yum[797]: Erased: mongodb-org-3.2.20-1.el7.x86_64 7月 11 10:54:04 ansible.test systemd[1]: Reloading. 7月 11 10:54:04 ansible.test systemd[1]: Configuration file /usr/lib/systemd/system/ebtables.service is marked executable. Ple 7月 11 10:54:04 ansible.test systemd[1]: Configuration file /usr/lib/systemd/system/wpa_supplicant.service is marked executabl 7月 11 10:54:04 ansible.test yum[797]: Erased: mongodb-org-server-3.2.20-1.el7.x86_64 7月 11 10:54:04 ansible.test yum[797]: Erased: mongodb-org-shell-3.2.20-1.el7.x86_64 7月 11 10:54:04 ansible.test yum[797]: Erased: mongodb-org-mongos-3.2.20-1.el7.x86_64 7月 11 10:54:04 ansible.test yum[797]: Erased: mongodb-org-tools-3.2.20-1.el7.x86_64 7月 11 10:59:08 ansible.test systemd[1]: Reloading. 7月 11 10:59:08 ansible.test systemd[1]: Configuration file /usr/lib/systemd/system/ebtables.service is marked executable. Ple 7月 11 10:59:08 ansible.test systemd[1]: Configuration file /usr/lib/systemd/system/wpa_supplicant.service is marked executabl 7月 11 10:59:08 ansible.test yum[928]: Installed: mongodb-org-server-3.2.20-1.el7.x86_64 7月 11 10:59:08 ansible.test yum[928]: Installed: mongodb-org-mongos-3.2.20-1.el7.x86_64 7月 11 10:59:12 ansible.test yum[928]: Installed: mongodb-org-tools-3.2.20-1.el7.x86_64 7月 11 10:59:13 ansible.test yum[928]: Installed: mongodb-org-shell-3.2.20-1.el7.x86_64 7月 11 10:59:13 ansible.test yum[928]: Installed: mongodb-org-3.2.20-1.el7.x86_64 7月 11 10:59:21 ansible.test polkitd[747]: Registered Authentication Agent for unix-process:961:41855579 (system bus name :1.1 7月 11 10:59:21 ansible.test systemd[1]: Starting SYSV: Mongo is a scalable, document-oriented database .... -- Subject: Unit mongod.service has begun start-up -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel -- -- Unit mongod.service has begun starting up. 7月 11 10:59:21 ansible.test mongod[966]: Error starting mongod. /var/run/mongodb/mongod.pid exists. 7月 11 10:59:21 ansible.test systemd[1]: mongod.service: control process exited, code=exited status=1 7月 11 10:59:21 ansible.test systemd[1]: Failed to start SYSV: Mongo is a scalable, document-oriented database .. <br>#解决方法: [root@ansible mongodb]# rm /var/run/mongodb/mongod.pid -f [root@ansible mongodb]# systemctl start mongod.service |
5.使用mongofiles工具
1 2 3 4 5 6 7 8 9 | [root@mongo_rs1 tmp]# mongofiles put 1.JPG -d pics -t jpg 2018-07-11T11:34:16.002+0800 connected to : localhost added file: 1.JPG [root@mongo_rs1 tmp]# mongofiles list -d pics 2018-07-11T11:34:33.037+0800 connected to : localhost 1.JPG 138424 [root@mongo_rs1 tmp]# mongofiles -d pics get 1.JPG 2018-07-11T13:39:50.775+0800 connected to : localhost finished writing to 1.JPG |
6.http访问:
http://172.16.160.91/girdfstest/1.JPG
7.使用java开发:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | package com.neo; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.UUID; import org.bson.types.ObjectId; import org.junit.Test; import com.mongodb.MongoClient; import com.mongodb.client.MongoDatabase; import com.mongodb.client.gridfs.GridFSBucket; import com.mongodb.client.gridfs.GridFSBuckets; import com.mongodb.client.gridfs.GridFSFindIterable; import com.mongodb.client.gridfs.model.GridFSFile; import com.mongodb.client.model.Filters; public class TestMongo { String mongoHost = "172.16.160.91" ; int mongoPort = 27017; String dbName = "pics" ; public MongoDatabase mongoDatabase() throws Exception { MongoClient mongoClient = new MongoClient(mongoHost, mongoPort); return mongoClient.getDatabase(dbName); } // 上传文件 @Test public void upload() throws Exception { // 获取文件流 File file = new File( "C:\\tmp\\10.jpg" ); // 提前放一个图片 InputStream in = new FileInputStream(file); // 创建一个容器,传入一个`MongoDatabase`类实例db GridFSBucket bucket = GridFSBuckets. create (mongoDatabase()); // 上传 ObjectId fileId = bucket.uploadFromStream(UUID.randomUUID().toString(), in ); System. out .println( "上传完成。 文件ID:" + fileId); } /* * [root@mongo_rs1 tmp]# mongofiles list -d pics * 2018-07-11T15:05:38.944+0800 connected to: localhost * f32a465b-6372-400d-b9e9-a10664e2d9a0 6644 * http://172.16.160.91/girdfsjava/5b45b34281dab5d81cca03cb */ // 查找文件 @Test public void findOne() throws Exception { // 获取文件ID String objectId = "5b45acdc81dab5fb9039ae3d" ; // 根据upload后文件ID修改 // 创建一个容器,传入一个`MongoDatabase`类实例db GridFSBucket bucket = GridFSBuckets. create (mongoDatabase()); // 获取内容 GridFSFindIterable gridFSFindIterable = bucket.find(Filters.eq( "_id" , new ObjectId(objectId))); GridFSFile gridFSFile = gridFSFindIterable. first (); System. out .println( "filename: " + gridFSFile.getFilename()); } // 下载文件 @Test public void download() throws Exception { // 获取文件ID String objectId = "5b45acdc81dab5fb9039ae3d" ; // 获取文件流,定义存放位置和名称 File file = new File( "C:\\tmp\\down\\5b45acdc81dab5fb9039ae3d.jpg" ); // 创建一个容器,传入一个`MongoDatabase`类实例db GridFSBucket bucket = GridFSBuckets. create (mongoDatabase()); // 创建输出流 OutputStream os = new FileOutputStream(file); // 下载 bucket.downloadToStream(new ObjectId(objectId), os); System. out .println( "下载完成。" ); } // 删除文件 @Test public void delete () throws Exception { // 获取文件ID String objectId = "5b45acdc81dab5fb9039ae3d" ; // 创建一个容器,传入一个`MongoDatabase`类实例db GridFSBucket bucket = GridFSBuckets. create (mongoDatabase()); // 删除 bucket. delete (new ObjectId(objectId)); System. out .println( "删除完成。" ); } // 上传文件 @Test public void upload2() throws Exception { // 获取文件流 File file = new File( "C:\\tmp\\10.jpg" ); // 提前放一个图片 InputStream in = new FileInputStream(file); // 创建一个容器,传入一个`MongoDatabase`类实例db GridFSBucket bucket = GridFSBuckets. create (mongoDatabase()); // 上传 ObjectId fileId = bucket.uploadFromStream( "10.jpg" , in ); System. out .println( "上传完成。 文件ID:" + fileId); } /* * [root@mongo_rs1 tmp]# mongofiles list -d pics * 2018-07-11T15:05:38.944+0800 connected to: localhost * 10.jpg 6644 * http://172.16.160.91/girdfstest/10.jpg */ // 查找文件 @Test public void findOne2() throws Exception { // 获取文件ID String filename = "10.jpg" ; // 根据upload后文件ID修改 // 创建一个容器,传入一个`MongoDatabase`类实例db GridFSBucket bucket = GridFSBuckets. create (mongoDatabase()); // 获取内容 GridFSFindIterable gridFSFindIterable = bucket.find(Filters.eq( "filename" , filename)); GridFSFile gridFSFile = gridFSFindIterable. first (); System. out .println( "filename: " + gridFSFile.getFilename()); } // 下载文件 @Test public void download2() throws Exception { // 获取文件ID String filename = "10.jpg" ; // 获取文件流,定义存放位置和名称 File file = new File( "C:\\tmp\\down\\10.jpg" ); // 创建一个容器,传入一个`MongoDatabase`类实例db GridFSBucket bucket = GridFSBuckets. create (mongoDatabase()); // 创建输出流 OutputStream os = new FileOutputStream(file); // 下载 bucket.downloadToStreamByName(filename, os); //bucket.downloadToStream(filename, os); System. out .println( "下载完成。" ); } // 删除文件 @Test public void delete2() throws Exception { // 获取文件ID String filename = "10.jpg" ; // 创建一个容器,传入一个`MongoDatabase`类实例db GridFSBucket bucket = GridFSBuckets. create (mongoDatabase()); GridFSFindIterable gridFSFindIterable = bucket.find(Filters.eq( "filename" , filename)); GridFSFile gridFSFile = gridFSFindIterable. first (); // 删除 bucket. delete (gridFSFile.getObjectId()); //System. out .println( "删除完成。" ); } } |
8.pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <?xml version= "1.0" encoding= "UTF-8" ?> <project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion>4.0.0</modelVersion> <groupId>com.neo</groupId> <artifactId>spring-boot-hello</artifactId> <version>1.0</version> <packaging>jar</packaging> < name >spring-boot-hello</ name > <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.2.2</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> |
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步