下载安装包
官网 https://www.mongodb.com/
版本 https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.0.2.tgz
解压、配置mongod.conf
解压,重命名
tar -zxvf mongodb-linux-x86_64-rhel70-4.0.2.tgz -C /usr/local
cd /usr/local
mv mongodb-linux-x86_64-rhel70-4.0.2 mongodb
配置MONGODB_HOME
vim ~/.bash_profile
export MONGODB_HOME=/usr/local/mongodb
export PATH=$PATH:$MONGODB_HOME/bin
使修改生效
source ~/.bash_profile
新建目录和文件
mkdir -p /usr/local/mongodb/log
mkdir -p /usr/local/mongodb/data
mkdir -p /usr/local/mongodb/conf
touch /usr/local/mongodb/conf/mongod.conf
配置 mongod.conf
# 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: /usr/local/mongodb/log/mongod.log
# where and how to store data.
storage:
dbPath: /usr/local/mongodb/data
journal:
enabled: true
# how the process runs
processManagement:
fork: true
pidFilePath: /usr/local/mongodb/mongod.pid
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
启动
./bin/mongod --config conf/mongod.conf
登陆测试
./bin/mongo --host 127.0.0.1
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:27017/
MongoDB server version: 4.0.2
Server has startup warnings:
2018-09-02T05:48:49.889-0400 I CONTROL [initandlisten]
2018-09-02T05:48:49.889-0400 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-09-02T05:48:49.889-0400 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2018-09-02T05:48:49.889-0400 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2018-09-02T05:48:49.889-0400 I CONTROL [initandlisten]
2018-09-02T05:48:49.890-0400 I CONTROL [initandlisten]
2018-09-02T05:48:49.890-0400 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2018-09-02T05:48:49.890-0400 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-09-02T05:48:49.890-0400 I CONTROL [initandlisten]
2018-09-02T05:48:49.890-0400 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2018-09-02T05:48:49.890-0400 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2018-09-02T05:48:49.890-0400 I CONTROL [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
>
基本操作
- 建立用户
use admin
db.createUser(
{
user: "dba",
pwd: "123456",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
- 建立数据库
use web
db.items.insert({"name":"web"})
- 给新创建的库加入用户
use web
db.createUser(
{
user: "web",
pwd: "123456",
roles: [ { role: "readWrite", db: "web" } ]
}
)
- 关闭数据库
use admin
db.shutdownServer()
- 常用命令
use dbname # 使用哪个数据库
db.auth("user","pwd") # 验证
show dbs # 显示所有数据库
show users # 显示当前数据库的所有用户
db.dropDatabase() # 删除数据库
db.shutdownServer() # 关闭数据库
- 备份恢复数据库
备份
mongodump -d <数据库名><文件夹目录>
恢复
mongorestore -d <数据库名><文件夹目录>
系统句柄调优(根据实际情况而定)
vim /etc/security/limits.conf
mongod soft nofile 64000
mongod hard nofile 64000
mongod soft nproc 32000
mongod hard nproc 32000
登录认证
上面的配置是没有密码认证的,实际环境中是要加登录验证的
修改mongod.conf 添加下面的内容:
security:
authorization: enabled
重启mongodb,再次登录(我们以前面配置的dba账户为例(dba账户配置的密码为123456))
首先连接mongo,在进行认证
# ./bin/mongo
MongoDB shell version v4.0.1
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.1
> use admin
switched to db admin
> db.auth("dba","123456")
1
>