安裝使用mongodb

來源:互聯網
上載者:User

標籤:mongodb安裝與簡單使用

安裝

下載安裝包
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.5.tgz
解壓
tar axf mongodb-linux-x86_64-2.6.5.tgz -C /opt/

ln -s /opt/mongodb-linux-x86_64-2.6.5/ /opt/mongodb
建立日誌、資料存放目錄
mkdir -pv /opt/mongodb/{data,etc,logs}

建立設定檔

vim /opt/mongodb/etc/mongodb.conf

# 記錄檔位置logpath=/opt/mongodb/logs/mongodb.log# 以追加方式寫入日誌logappend=true# 是否以守護進程方式運行fork = true# 預設27017port = 27017# 資料庫檔案位置dbpath=/opt/mongodb/data/# 啟用定期記錄CPU利用率和 I/O 等待#cpu = true# 是否以安全認證方式運行,預設是不認證的非安全方式#noauth = trueauth = true# 詳細記錄輸出#verbose = true# Inspect all client data for validity on receipt (useful for# developing drivers)用於開發驅動程式時驗證用戶端請求#objcheck = true# Enable db quota management# 啟用資料庫配額管理#quota = true# 設定oplog記錄等級# Set oplogging level where n is#   0=off (default)#   1=W#   3=both#   7=W+some reads#diaglog=0# Diagnostic/debugging option 動態調試項#nocursors = true# Ignore query hints 忽略查詢提示#nohints = true# 禁用http介面,預設為localhost:28017#nohttpinterface = true# 關閉伺服器端指令碼,這將極大的限制功能# Turns off server-side scripting.  This will result in greatly limited# functionality#noscripting = true# 關閉掃描表,任何查詢將會是掃描失敗# Turns off table scans.  Any query that would do a table scan fails.#notablescan = true# 關閉資料檔案預分配# Disable data file preallocation.#noprealloc = true# 為新資料庫指定.ns檔案的大小,單位:MB# Specify .ns file size for new databases.# nssize = # Replication Options 複製選項# in replicated mongo databases, specify the replica set name here#replSet=setname# maximum size in megabytes for replication operation log#oplogSize=1024# path to a key file storing authentication info for connections# between replica set members#指定儲存身分識別驗證資訊的密鑰檔案的路徑#keyFile=/path/to/keyfile

添加mongodb使用者
useradd mongodb
echo ‘123456! ‘|passwd mongodb --stdin

編寫管理指令碼

vim /etc/init.d/mongodb

#!/bin/sh## mongodb - this script starts and stops the mongodb daemon## chkconfig: - 85 15# description: MongoDB is a non-relational database storage system.# processname: mongodb# config: /opt/mongodb/etc/mongodb.conf# pidfile: /opt/mongodb/mongodb.pidPATH=/opt/mongodb/bin:/sbin:/bin:/usr/sbin:/usr/binNAME=mongodbtest -x $DAEMON || exit 0set -ecase "$1" in  start)        echo -n "Starting MongoDB... "        #/opt/mongodb/bin/mongod --port 27017 --fork --dbpath=/opt/mongodb/data/ --config=/opt/mongodb/etc/mongodb.conf --logpath=/opt/mongodb/logs/mongodb.log --logappend        su - mongodb -c "/opt/mongodb/bin/mongod --config=/opt/mongodb/etc/mongodb.conf"        ;;  stop)        echo -n "Stopping MongoDB... "        /opt/mongodb/bin/mongod --shutdown --dbpath=/opt/mongodb/data/        ;;  restart)        $0 stop        $0 start        ;;      *)            N=/etc/init.d/$NAME            echo "Usage: $N {start|stop}" >&2            exit 1            ;;    esac    exit 0

修改許可權
chown -R mongodb.mongodb /opt/mongodb-linux-x86_64-2.6.5
啟動
chmod a+x /etc/init.d/mongodb
/etc/init.d/mongodb start
修改環境變數
vim /etc/profile
#mongodb
export PATH=$PATH:/opt/mongodb/bin

mongodb常用命令串連mongodb

mongo 127.0.0.1:27017/admin
該串連方式是串連127.0.0.1上27017連接埠mongodb中的admin庫

協助資訊

help
\help
db.mycoll.help()

庫表基本操作

查看所有資料庫
show dbs
切換資料庫
use DBNAME
查看所有表
show tables
查看當前使用的資料庫
db.getName()
db
顯示當前db狀態
db.stats()
查看當前db的連結機器地址
db.getMongo()

使用者管理

查看當前庫所有使用者
show users
查看許可權列表
show roles
添加管理使用者

db.createUser({user:"python",pwd:"python",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})

建立讀寫權限普通使用者
use 庫名

db.createUser({user:"name",pwd:"password",roles:[{ role:" readWrite",db:”庫名”}]})

使用者認證,密碼設定
db.auth("userName", "123123")
刪除使用者
db.removeUser("userName")

啟動關閉

用指令碼
service mongodb start
service mongodb stop
使用命令
/opt/mongodb/bin/mongod --config=/opt/mongodb/etc/mongodb.conf
/opt/mongodb/bin/mongod --shutdown --dbpath=/opt/mongodb/data/

彙總(表)管理查看協助資訊

db.help
得到當前db的所有聚集集合
db.getCollectionNames()
建立一個聚集集合(table)
show tables;
db.createCollection(‘test‘,{size:20,capped:5,max:100})
得到指定名稱的聚集集合(table)
db.getCollection("test");
顯示當前db所有叢集索引的狀態
db.printCollectionStats()

查詢資料

db.彙總名(表名).find()
db.tb.find()
db.彙總名(表名).find({條件},{鍵})
db.tb.find({},{_id:0,_class:1})
_id:0中_id:是鍵,0表示不顯示,非零表示顯示
按條件查詢
比較符:$gt----大於,$gte----大於等於,$lt----小於,$lte----小於等於,$eq----等於,$ne----不等於
db.tb.find({num:{$eq:4}},{_id:0,_class:1,num:4})
db.tb.find({num:{$gt:3,$lt:7}},{_id:0,_class:1,num:4})
Regex
測試失敗。。。。。。

開放防火牆

vim /etc/sysconfig/iptables
-A INPUT -m state --state NEW -m tcp -p tcp --dport 27017 -j ACCEPT
service iptables reload

安裝使用mongodb

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.