linux下mongodb数据库安装配置使用

mongodb的安装配置

解压安装文件到安装目录

1
2
3
4
tar -C /usr/local mongodb-linux-x86_64-3.6.6.tgz 
mv /usr/local/mongodb-linux-x86_64-3.6.6 /usr/local/mongodb
mkdir -p /data/mongodb/db
mkdir -p /data/mongodb/logs

添加配置文件

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
cat  >>/etc/mongodb.conf << EOF
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /data/mongodb/logs/mongod.log #设置日志目录
# Where and how to store data.
storage:
dbPath: /data/mongodb/db #设置数据目录
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# how the process runs
processManagement:
fork: true # fork and run in background
pidFilePath: /data/mongodb/logs/mongod.pid # location of pidfile
timeZoneInfo: /usr/share/zoneinfo
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1 # Listen to local interface only, comment to listen on all interfaces.
security:
authorization: enabled
#operationProfiling:
#replication:
#sharding:
## Enterprise-Only Options
#auditLog:
#snmp:
EOF

启动mongo

1
/usr/local/mongodb/bin/mongod  -f /etc/mongodb.conf

登陆授权账户

1
2
3
4
5
6
7
8
9
10
/usr/local/mongodb/bin/mongo
#创建管理用户
>>use admin
>>db.createUser(
>> {
>> user: "admin",
>> pwd: "abcd1123",
>> roles: \[ { role: "root", db: "admin" } \]
>> }
>> )

给非admin库授权

一.管理员账户登陆

1
2

mongo mongodb://root@127.0.0.1:27017/admin

二.到需要授权的数据库下,使用use autest

1
2
3
4
5
6
7
8
use autest
db.createUser(
{
user: "dbtest",
pwd: "4321dbtest",
roles: \[ { role: "dbOwner", db: "autest" } \]
}
)

mongodb数据备份

1
2
3
4
5
6
#!/bin/bash
datetime=\`date '+%Y%m%d'\`
echo $datetime
/usr/local/mongodb/bin/mongodump -o /data/mongoback/${datetime}
cd /data/mongoback/
zip -9 -r -m ${datetime}.zip ${datetime}

添加启动脚本

Mongo安装完成后一般都是通过命令行或脚本形式启动,下面提供了设置为服务的脚本,具体如下

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
#!/bin/sh

### BEGIN INIT INFO
# Provides: mongodb
# Required-Start:
# Required-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: mongodb
# Description: mongo db server
### END INIT INFO

EXE_FILE=/usr/local/mongodb/bin/mongod
CONFIG_FILE=/etc/mongodb.conf

. /lib/lsb/init-functions
MONGOPID=\`ps -ef| grep mongod| grep -v grep| awk '{print $2}'\`
test -x $EXE_FILE || exit 0

case "$1" in
start)
ulimit -n 3000
log\_begin\_msg "Starting MongoDB server"
$EXE\_FILE --config $CONFIG\_FILE
log\_end\_msg 0
;;
stop)
log\_begin\_msg "Stopping MongoDB server"
if \[ ! -z "$MONGOPID" \]; then
kill -15 $MONGOPID
fi
log\_end\_msg 0
;;
status)
ps -aux| grep mongod
;;
*)
log\_success\_msg "Usage: /etc/init.d/mongodb {start|stop|status}"
exit 1
esac

mongodb简单的CRUD操作

查看数据库

1
2
show dbs#显示系统所以库
show collections#显示当前库的所有集合

mogno当中的集合也就是mysql当中的tables,数据保存在collections当中

插入数据

向user集合里面插入一条数据

1
db.users.insert({username:"smith"})

修改数据

1
db.users.update({username:"smith"},{$set:{country:"China"}})

删除数据

1
2
3
db.users.remove({username:"China"})
db.user.remove({})#干掉user集合内所有内容(清空集合)
db.user.drop()#干掉user集合(删除集合)

查找数据

1
2
3
db.users.count()#查看集合内有多少条数据
db.users.find()
db.users.find({username:"smith"})