安裝nginx和nginx-gridfs和mongodb

1.安裝依賴包:java

[root@mongo_rs1 ~]# yum -y install pcre-devel openssl-devel zlib-devel git gcc gcc-c++
[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.安裝nginxnginx

[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

  

#若是報錯
[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.修改配置文件c++

[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.安裝mongodbgit

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
mongodb的配置文件中的bind_ip 默認爲127.0.0.1,默認只有本機能夠鏈接。  此時,須要將bind_ip配置爲0.0.0.0,表示接受任何IP的鏈接。 啓動mongodb :systemctl start mongod.service 中止mongodb :systemctl stop mongod.service

若是報錯:github

[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..

#解決方法: [root@ansible mongodb]# rm /var/run/mongodb/mongod.pid -f [root@ansible mongodb]# systemctl start mongod.service

5.使用mongofiles工具web

[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訪問:spring

http://172.16.160.91/girdfstest/1.JPGsql

7.使用java開發:mongodb

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.xmlshell

<?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>
相關文章
相關標籤/搜索