wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.0.tgz
下载完毕后解压缩
[root@master dgm]# tar -zxf mongodb-linux-x86_64-4.0.0.tgz
名字那么长,可以进行重命名:
[root@master dgm]# mv mongodb-linux-x86_64-4.0.0 mongodb
进入到mongodb主目录下:
cd mongodb
分别建配置文件、数据目录、日志文件
建立配置文件
vim mongodb.conf
键入以下内容
#端口号
port=27017
#db目录
dbpath=/dgm/mongodb/data/db
#日志目录
logpath=/dgm/mongodb/logs/mongodb.log
#后台
fork=true
#日志输出
logappend=true
#允许远程IP连接
bind_ip=0.0.0.0
注意路径根据自己电脑的实际情况填写
建立数据目录和日志文件
[root@master mongodb]# mkdir -p data/db logs
[root@master mongodb]# touch logs/mongodb.log
编辑环境配置
vim /etc/profile
追加以下内容:
切记路径按实际情况写
source /etc/profile
启动mongodb测试
[root@master mongodb]# mongod --config mongodb.conf
最后连接测试
[root@master mongodb]# mongo
为了安全起见,我们需要设置相应的用户名和密码。
创建数据库系统管理员
> use admin
switched to db admin
> db.createUser({ user: "admin", pwd: "12345678", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
> db.auth("admin", "12345678")
1
修改mongodb配置追加认证标志
## 是否以安全认证方式运行,默认是不认证的非安全方式
#noauth = true
auth = true
然后重启mongodb服务
再次通过shell进入mongodb,创建需要管理的mongodb 数据的账号密码,此处是库是douyin,用户名是root,密码87654321
[root@master mongorest]# mongo
> use admin
switched to db admin
> db.auth("admin", "12345678")
1
> use douyin
switched to db douyin
> db.createUser({ user: "root", pwd: "87654321", roles: [{ role: "dbOwner", db: "douyin" }] })
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "dbOwner",
"db" : "douyin"
}
]
}
> use admin
switched to db admin
> db.system.users.find()
{ "_id" : "admin.admin", "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "Qp+6YETcIsDyrPPwQVtWHQ==", "storedKey" : "0X27QePHJknrd+5qg4Ai5TeEWjg=", "serverKey" : "ZYRXkrQfJY875GC1qf76xAmBFow=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "E3LYqVegf93pTNjwNqXh4Gy8zQ1qNXXz0HtHHw==", "storedKey" : "yUZq7I9/w8Kf6fLu6Q8OqJcKNv77KUDmvGo4gEGi5ng=", "serverKey" : "+sNL0T/gfa+B+HoNSeid+HCC7OulvZlvSi2FDtE3wfk=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
{ "_id" : "douyin.root", "user" : "root", "db" : "douyin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "gVHTUMMMC38slLBW15kxPw==", "storedKey" : "591b17pCtkEZcVXf7ZO/Ll4DPAs=", "serverKey" : "vd5w+StBi9eXfViwWY30tc66lmo=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "2CHwq56N9MLts2v+PSqN3qs6tpWX1BKHY0aTDA==", "storedKey" : "2v5I/nnNoGVst2tTljoQs34z7bDNZS4euv6nAFZGxyc=", "serverKey" : "/F2xcR86DH4ErEEEbDyprmm1ttnl1o6sniWrj3vjjTY=" } }, "roles" : [ { "role" : "dbOwner", "db" : "douyin" } ] }
> exit
测试密码权限是否生效
先看失败的尝试案例
[root@master mongorest]# mongo
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.0
> use douyin
switched to db douyin
> show collections
Warning: unable to run listCollections, attempting to approximate collection names by parsing connectionStatus
> db.douyin.find()
Error: error: {
"ok" : 0,
"errmsg" : "command find requires authentication",
"code" : 13,
"codeName" : "Unauthorized"
}
>
再看成功的案例
[root@master mongorest]# mongo "mongodb://root:[email protected]:27017/douyin"
MongoDB shell version v4.0.0
connecting to: mongodb://127.0.0.1:27017/douyin
MongoDB server version: 4.0.0
> show collections
douyin
> db.douyin.find()
{ "_id" : ObjectId("5f083625b8b307c7e889f276") }
{ "_id" : ObjectId("5f08365cb8b307c7e889f277"), "name" : "dongguangming", "age" : 31 }
>
很好,权限认证通过了,可以查询数据记录
新建go文件mongdb-add.go,键入以下代码
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"goji.io"
"goji.io/pat"
"gopkg.in/mgo.v2"
)
func ErrorWithJSON(w http.ResponseWriter, message string, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
fmt.Fprintf(w, "{message: %q}", message)
}
func ResponseWithJSON(w http.ResponseWriter, json []byte, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
w.Write(json)
}
type Book struct {
ISBN string `json:"isbn"`
Title string `json:"title"`
Authors []string `json:"authors"`
Price string `json:"price"`
}
func main() {
session, err := mgo.Dial("mongodb://root:[email protected]:27017/douyin")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
ensureIndex(session)
mux := goji.NewMux()
mux.HandleFunc(pat.Post("/books"), addBook(session))
http.ListenAndServe("192.168.8.200:8888", mux)
}
func ensureIndex(s *mgo.Session) {
session := s.Copy()
defer session.Close()
c := session.DB("douyin").C("books")
index := mgo.Index{
Key: []string{"isbn"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err := c.EnsureIndex(index)
if err != nil {
panic(err)
}
}
func addBook(s *mgo.Session) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
session := s.Copy()
defer session.Close()
var book Book
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&book)
if err != nil {
ErrorWithJSON(w, "数据格式不对", http.StatusBadRequest)
return
}
c := session.DB("douyin").C("books")
err = c.Insert(book)
if err != nil {
if mgo.IsDup(err) {
ErrorWithJSON(w, "书的isbn已存在", http.StatusBadRequest)
return
}
ErrorWithJSON(w, "服务器错误", http.StatusInternalServerError)
log.Println("添加书失败!!!: ", err)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Location", r.URL.Path+"/"+book.ISBN)
w.WriteHeader(http.StatusCreated)
}
}
然后就就执行以上程序,
go run mongdb-add.go
打开postman测试
查询数据库是否已存在
新建go文件mongdb-select.go,键入以下代码
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"goji.io"
"goji.io/pat"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func ErrorWithJSON(w http.ResponseWriter, message string, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
fmt.Fprintf(w, "{message: %q}", message)
}
func ResponseWithJSON(w http.ResponseWriter, json []byte, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
w.Write(json)
}
type Book struct {
ISBN string `json:"isbn"`
Title string `json:"title"`
Authors []string `json:"authors"`
Price string `json:"price"`
}
func main() {
session, err := mgo.Dial("mongodb://root:[email protected]:27017/douyin")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
ensureIndex(session)
mux := goji.NewMux()
mux.HandleFunc(pat.Get("/books/:isbn"), bookByISBN(session))
http.ListenAndServe("192.168.8.200:8888", mux)
}
func ensureIndex(s *mgo.Session) {
session := s.Copy()
defer session.Close()
c := session.DB("douyin").C("books")
index := mgo.Index{
Key: []string{"isbn"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err := c.EnsureIndex(index)
if err != nil {
panic(err)
}
}
func bookByISBN(s *mgo.Session) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
session := s.Copy()
defer session.Close()
isbn := pat.Param(r, "isbn")
c := session.DB("store").C("books")
var book Book
err := c.Find(bson.M{"isbn": isbn}).One(&book)
if err != nil {
ErrorWithJSON(w, "数据库错误", http.StatusInternalServerError)
log.Println("查找isbn的书失败: ", err)
return
}
if book.ISBN == "" {
ErrorWithJSON(w, "书不存在", http.StatusNotFound)
return
}
respBody, err := json.MarshalIndent(book, "", " ")
if err != nil {
log.Fatal(err)
}
ResponseWithJSON(w, respBody, http.StatusOK)
}
}
执行程序
go run mongdb-select.go
继续用postman测试接口
新建go文件mongdb-update.go,键入以下代码
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"goji.io"
"goji.io/pat"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func ErrorWithJSON(w http.ResponseWriter, message string, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
fmt.Fprintf(w, "{message: %q}", message)
}
func ResponseWithJSON(w http.ResponseWriter, json []byte, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
w.Write(json)
}
type Book struct {
ISBN string `json:"isbn"`
Title string `json:"title"`
Authors []string `json:"authors"`
Price string `json:"price"`
}
func main() {
session, err := mgo.Dial("mongodb://root:[email protected]:27017/douyin")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
ensureIndex(session)
mux := goji.NewMux()
mux.HandleFunc(pat.Put("/books/:isbn"), updateBook(session))
http.ListenAndServe("192.168.8.200:8888", mux)
}
func ensureIndex(s *mgo.Session) {
session := s.Copy()
defer session.Close()
c := session.DB("douyin").C("books")
index := mgo.Index{
Key: []string{"isbn"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err := c.EnsureIndex(index)
if err != nil {
panic(err)
}
}
func updateBook(s *mgo.Session) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
session := s.Copy()
defer session.Close()
isbn := pat.Param(r, "isbn")
var book Book
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&book)
if err != nil {
ErrorWithJSON(w, "Incorrect body", http.StatusBadRequest)
return
}
c := session.DB("douyin").C("books")
err = c.Update(bson.M{"isbn": isbn}, &book)
if err != nil {
switch err {
default:
ErrorWithJSON(w, "Database error", http.StatusInternalServerError)
log.Println("Failed update book: ", err)
return
case mgo.ErrNotFound:
ErrorWithJSON(w, "Book not found", http.StatusNotFound)
return
}
}
w.WriteHeader(http.StatusNoContent)
}
}
执行以上程序
go run mongdb-update.go
postman继续测试
再此查询isbn为1234567890的信息
新建go文件mongdb-all.go,键入以下代码
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"goji.io"
"goji.io/pat"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func ErrorWithJSON(w http.ResponseWriter, message string, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
fmt.Fprintf(w, "{message: %q}", message)
}
func ResponseWithJSON(w http.ResponseWriter, json []byte, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
w.Write(json)
}
type Book struct {
ISBN string `json:"isbn"`
Title string `json:"title"`
Authors []string `json:"authors"`
Price string `json:"price"`
}
func main() {
session, err := mgo.Dial("mongodb://root:[email protected]:27017/douyin")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
ensureIndex(session)
mux := goji.NewMux()
mux.HandleFunc(pat.Get("/books"), allBooks(session))
http.ListenAndServe("192.168.8.200:8889", mux)
}
func ensureIndex(s *mgo.Session) {
session := s.Copy()
defer session.Close()
c := session.DB("douyin").C("books")
index := mgo.Index{
Key: []string{"isbn"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err := c.EnsureIndex(index)
if err != nil {
panic(err)
}
}
func allBooks(s *mgo.Session) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
session := s.Copy()
defer session.Close()
c := session.DB("douyin").C("books")
var books []Book
err := c.Find(bson.M{}).All(&books)
if err != nil {
ErrorWithJSON(w, "Database error", http.StatusInternalServerError)
log.Println("Failed get all books: ", err)
return
}
respBody, err := json.MarshalIndent(books, "", " ")
if err != nil {
log.Fatal(err)
}
ResponseWithJSON(w, respBody, http.StatusOK)
}
}
运行该程序
go run mongdb-all.go
打开postman或浏览器
新建go文件mongdb-delete.go(根据isbn编号),键入以下代码
package main
import (
"fmt"
"log"
"net/http"
"goji.io"
"goji.io/pat"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
func ErrorWithJSON(w http.ResponseWriter, message string, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
fmt.Fprintf(w, "{message: %q}", message)
}
func ResponseWithJSON(w http.ResponseWriter, json []byte, code int) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
w.Write(json)
}
type Book struct {
ISBN string `json:"isbn"`
Title string `json:"title"`
Authors []string `json:"authors"`
Price string `json:"price"`
}
func main() {
session, err := mgo.Dial("mongodb://root:[email protected]:27017/douyin")
if err != nil {
panic(err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true)
ensureIndex(session)
mux := goji.NewMux()
mux.HandleFunc(pat.Delete("/books/:isbn"), deleteBook(session))
http.ListenAndServe("192.168.8.200:8889", mux)
}
func ensureIndex(s *mgo.Session) {
session := s.Copy()
defer session.Close()
c := session.DB("douyin").C("books")
index := mgo.Index{
Key: []string{"isbn"},
Unique: true,
DropDups: true,
Background: true,
Sparse: true,
}
err := c.EnsureIndex(index)
if err != nil {
panic(err)
}
}
func deleteBook(s *mgo.Session) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
session := s.Copy()
defer session.Close()
isbn := pat.Param(r, "isbn")
c := session.DB("douyin").C("books")
err := c.Remove(bson.M{"isbn": isbn})
if err != nil {
switch err {
default:
ErrorWithJSON(w, "Database error", http.StatusInternalServerError)
log.Println("Failed delete book: ", err)
return
case mgo.ErrNotFound:
ErrorWithJSON(w, "Book not found", http.StatusNotFound)
return
}
}
w.WriteHeader(http.StatusNoContent)
}
}
执行以上程序
go run mongdb-delete.go
用postman测试删除
然后去数据库查看是否还有该isbn为1234567890的书籍
mongo
Shell https://docs.mongodb.com/manual/mongo/