rokevin
移动
前端
语言
  • 基础

    • Linux
    • 实施
    • 版本构建
  • 应用

    • WEB服务器
    • 数据库
  • 资讯

    • 工具
    • 部署
开放平台
产品设计
  • 人工智能
  • 云计算
计算机
其它
GitHub
移动
前端
语言
  • 基础

    • Linux
    • 实施
    • 版本构建
  • 应用

    • WEB服务器
    • 数据库
  • 资讯

    • 工具
    • 部署
开放平台
产品设计
  • 人工智能
  • 云计算
计算机
其它
GitHub
  • 安装

安装

下载安装包

curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.2.12.tgz

解压

tar -zxvf mongodb-linux-x86_64-3.2.12.tgz

移动到指定位置

mv  mongodb-linux-x86_64-3.2.12/ /usr/local/mongodb

在/usr/local/mongodb下创建文件夹

mkdir -p ./data/db
mkdir  ./logs

在/usr/local/mongodb/bin下新建配置

vi mongodb.conf

#数据文件存放目录
dbpath = /usr/local/mongodb/data/db
#日志文件存放目录
logpath = /usr/local/mongodb/logs/mongodb.log
#端口
port = 27017
#以守护程序的方式启用,即在后台运行
fork = true
nohttpinterface = true
auth=true
bind_ip=0.0.0.0

环境变量配置

vi ~/.bashrc

export MONGODB_HOME=/usr/local/mongodb

export PATH=$MONGODB_HOME/bin:$PATH

保存后,重启系统配置

source /etc/profile

启动

在/usr/local/mongodb/bin下

mongod -f mongodb.conf 
或
./mongod -f mongodb.conf
或
mongod -f mongodb.conf --repair

关闭

mongod -f ./mongodb.conf --shutdown  
或
./mongod -f ./mongodb.conf --shutdown

正确关闭mongodb的方法

warning:千万不能使用kill -9 <pid>,因为MongoDB使用mmap方式进行数据文件管理,也就是说写操作基本是在内存中进行,写操作会被每隔60秒(syncdelay设定)的flush到磁盘里。如果在这60秒内flush处于停止事情我们进行kill -9那么从上次flush之后的写入数据将会全部丢失。 如果在flush操作进行时执行kill -9则会造成文件混乱,可能导致数据全丢了,启动时加了repair也无法恢复。


官方文档:https://docs.mongodb.org/manual/tutorial/manage-mongodb-processes/

Stop mongod Processes
In a clean shutdown a mongod completes all pending operations, flushes all data to data files, and closes all data files. Other shutdowns are unclean and can compromise the validity of the data files.

To ensure a clean shutdown, always shutdown mongod instances using one of the following methods:

Use shutdownServer()
Shut down the mongod from the mongo shell using the db.shutdownServer() method as follows:

use admin
db.shutdownServer()
Calling the same method from a init script accomplishes the same result.

For systems with authorization enabled, users may only issue db.shutdownServer() when authenticated to the admin database or via the localhost interface on systems without authentication enabled.

Use --shutdown
From the Linux command line, shut down the mongod using the --shutdown option in the following command:

mongod --shutdown
Use CTRL-C
When running the mongod instance in interactive mode (i.e. without --fork), issue Control-C to perform a clean shutdown.

Use kill
From the Linux command line, shut down a specific mongod instance using the following command:

kill <mongod process ID>
WARNING

Never use kill -9 (i.e. SIGKILL) to terminate a mongod instance.

开启端口

firewall-cmd --zone=public --add-port=27017/tcp --permanent
查看端口
firewall-cmd --permanent --query-port=27017/tcp
重启防火墙
firewall-cmd --reload

MongoDB后台管理 Shell

$ cd /usr/local/mongodb/bin
$ ./mongo
MongoDB shell version: 3.0.6
connecting to: test
Welcome to the MongoDB shell.
For more comprehensive documentation, see
        http://docs.mongodb.org/
Questions? Try the support group
        http://groups.google.com/group/mongodb-user

创建用户

   切换到admin库,创建用户管理员:
   use admin
   db.createUser({user:"root",pwd:"root123456",roles:["userAdminAnyDatabase"]})
   db.auth('root','root123456')
   以用户管理员身份登录,并切换数据库,创建数据库用户:
   切换到test数据库
   use test
   创建用户名、密码、角色
   db.createUser({user:"username",pwd:"@user123456*",roles:[{role:"readWrite",db:"securitydata"}]})
   设置mongodb配置中的auth为true(/etc/mongod.conf):
   security:
     authorization: enabled
   验证mongodb数据库权限。
   db.auth('user','@user123456*')

查看进程是否运行

ps aux|grep mongo

mongodb 远程连接客户端

https://www.mongodb.com/

mongodb://用户名:密码@地址:端口/数据库名

卸载mongodb

2、删除安装的包

yum erase $(rpm -qa | grep mongodb-org)

3、删除数据及日志

rm -r /var/log/mongodb

rm -r /var/lib/mongo

最近更新:: 2025/10/22 15:36
Contributors: luokaiwen