linux下mongodb安裝

解壓mongodb-linux-x86_64-rhel70-4.0.6.tgz

tar -zxf  mongodb-linux-x86_64-rhel70-4.0.6.tgz -C /usr/local
mv mongodb-linux-x86_64-rhel70-4.0.6 mongodb

創建data log目錄在home/data下

mkdir -p /home/data/mongodb/data
mkdir -p /home/data/mongodb/logs
touch /home/data/mongodb/logs/mongodb.log

新建配置文件mongodb.conf

touch /usr/local/mongodb/bin/mongodb.conf
[root@localhost bin]# cat mongodb.conf
dbpath=/home/data/mongodb/data
logpath=/home/data/mongodb/logs/mongodb.log
port=27017
#自己的ip
bind_ip= 192.168.1.175
#bind_ip_all=true
#bindIp: [127.0.0.1, 192.168.1.117]
#以追加的方式記錄日誌
logappend=true
#以後臺方式運行進程
fork=true
#開啓用戶認證
auth=true
#啓用日誌文件,默認啓用
journal=true
#這個選項可以過濾掉一些無用的日誌信息,若需要調試使用請設置爲false
quiet=true

創建數據庫及用戶

> use admin
> db.createUser( {user: "root",pwd: "password",roles: [{ role: "userAdminAnyDatabase", db: "admin" }]})

> use newdb;
switched to db newdb
> db.createUser({ user: "newuser",pwd: "password111",roles:[{ role:"readWrite",db: "newdb" }]}) 

> db.auth("newuser","password111")
> use admin
> db.auth("root","password")
1
#常用的命令
  >use test
switched to db test
> show collections
mycol
mycollection
newcollection
刪除庫 將刪除當前所選數據庫。 如果沒有選擇任何數據庫,那麼它將刪除默認的’test‘數據庫。
>db.dropDatabase()
Shell

查詢某個數據庫下的用戶
>db.system.users.find() 

刪除某個數據庫下的所有用戶
>db.system.users.remove()

刪除指定用戶
>db.system.users.remove({'user':'用戶名'})

查看所有帳號
>  use admin
switched to db admin
> db.auth('dba','dba')
1
> db.system.users.find().pretty()
> db.system.users.find().count()
#刪除名稱爲 mycollection 的集合。
>db.mycollection.drop()
true

> > show dbs
> show collections
> db.system.users.find()
> show users
    mongoexport -d dbname -c collectionname -o file --type json/csv -f field
        參數說明:
            -d :數據庫名
            -c :collection名
            -o :輸出的文件名
            --type : 輸出的格式,默認爲json
            -f :輸出的字段,如果-type爲csv,則需要加上-f "字段名"
    mongoimport -d dbname -c collectionname --file filename --headerline --type json/csv -f field
        參數說明:
            -d :數據庫名
            -c :collection名
            --type :導入的格式默認json
            -f :導入的字段名
            --headerline :如果導入的格式是csv,則可以使用第一行的標題作爲導入的字段
            --file :要導入的文件

例:導出導入全部數據
  ./mongodump -h 192.168.1.179:27017 -d testdb -utestuser -ppassword -o /usr/local/mongodb/bin/newdb.dmp

  ./mongorestore -h 192.168.1.175:27017 -d testdb -utestuser -ppassword /usr/local/mongodb/bin/newdb.dmp/new

導出導入集合
  ./mongodump -h 192.168.1.179:27017 -d testdb -utestuser -ppassword -c test_resource -o /usr/local/mongodb/bin/test_resource.dmp

  ./mongorestore -h 192.168.1.175:27017 -d testdb -utestuser -ppassword -c test_resource /usr/local/mongodb/bin/sys_resource.dmp/new/test_resource.bson 

停服務
./mongod -shutdown -dbpath=/home/data/mongodb/data
啓動
./mongod -f /usr/local/mongodb/bin/mongodb.conf

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章