mogodb单机版安装

单机版安装

独立安装MongoDB

#下载
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-4.4.10.tgz
#解压
tar -xzvf mongodb-linux-x86_64-rhel70-4.4.10.tgz -C /usr/local/
cd /usr/local/

#改名
mv mongodb-linux-x86_64-rhel70-4.4.10 mongodb

mkdir -p /data/mongodb/data /data/mongodb/log

#进入mongodb目录,启动mongodb服务

[root@db25 bin]# ./mongod --port=27017 --dbpath=/data/mongodb/data --logpath=/data/mongodb/log/mongodb.log --bind_ip=0.0.0.0 --fork
about to fork child process, waiting until server is ready for connections.
forked process: 5954
child process started successfully, parent exiting


–dbpath :指定数据文件存放目录
–logpath :指定日志文件,注意是指定文件不是目录
–logappend :使用追加的方式记录日志
–port:指定端口,默认为27017
–bind_ip:默认只监听localhost网卡
–fork: 后台启动
–auth: 开启认证模式

#修改/etc/profile,添加环境变量,方便执行MongoDB命令
vi /etc/profile

export MONGODB_HOME=/usr/local/mongodb
PATH=$PATH:$MONGODB_HOME/bin

#重新加载环境变量
source /etc/profile

vi /usr/local/mongodb/conf/mongo.conf

systemLog:
  destination: file
  path: /data/mongodb/log/mongod.log # log path
  logAppend: true
storage:
  dbPath: /data/mongodb/data # data directory
  engine: wiredTiger  #存储引擎
  journal:            #是否启用journal日志
    enabled: true
net:
  bindIp: 0.0.0.0
  port: 27017 # port
processManagement:
  fork: true
#security:
#  authorization: enabled   #开启授权认证

启动MongoDB

cd /usr/local/mongodb/bin

./mongod -f /usr/local/mongodb/conf/mongo.conf

-f 选项表示将使用配置文件启动mongodb

  • 进入服务
[root@db25 bin]# ./mongo
MongoDB shell version v4.4.10
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("d75ec0f4-55da-4e69-8d73-a68235be9bcc") }
MongoDB server version: 4.4.10
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
        https://docs.mongodb.com/
Questions? Try the MongoDB Developer Community Forums
        https://community.mongodb.com
---
The server generated these startup warnings when booting: 
        2024-03-15T09:57:44.524+08:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
        2024-03-15T09:57:46.138+08:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
        2024-03-15T09:57:46.138+08:00: You are running this process as the root user, which is not recommended
        2024-03-15T09:57:46.139+08:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
        2024-03-15T09:57:46.139+08:00: /sys/kernel/mm/transparent_hugepage/defrag is 'always'. We suggest setting it to 'never'
        2024-03-15T09:57:46.139+08:00: Soft rlimits too low
        2024-03-15T09:57:46.139+08:00:         currentValue: 1024
        2024-03-15T09:57:46.139+08:00:         recommendedMinimum: 64000
---
---
        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
> show collections
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
> show users 
> exit
bye

关闭MongoDB服务

#方式一
mongod --port=27017 --dbpath=/mongodb/data --shutdown

#方式二:进入mongo shell

> use admin
switched to db admin
> db.shutdownServer()
server should be down...
> exit
bye
{"t":{"$date":"2024-03-15T02:24:02.479Z"},"s":"I",  "c":"QUERY",    "id":22791,   "ctx":"js","msg":"Failed to end logical session","attr":{"lsid":{"id":{"$uuid":"e9e75708-a303-49b1-b7d6-3da34d09877d"}},"error":{"code":9001,"codeName":"SocketException","errmsg":"socket exception [CONNECT_ERROR] server [couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused]"}}}
[root@db25 bin]# ps -ef |grep mongodb
root      4234 22355  0 10:24 pts/1    00:00:00 grep --color=auto mongodb
[root@db25 bin]#

#方式三:ps -ef |grep mongodb

kill -9 ID

安全模式

停止服务后

更新 mongo.conf开启验证

security:
  authorization: enabled   #开启授权认证

启动服务

[root@db25 bin]# ./mongo
MongoDB shell version v4.4.10
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("216f9d42-0898-450c-b898-289e705351e4") }
MongoDB server version: 4.4.10
> show users 
uncaught exception: Error: command usersInfo requires authentication :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1659:15
shellHelper.show@src/mongo/shell/utils.js:914:9
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1
> db.version()
4.4.10
> show tables
Warning: unable to run listCollections, attempting to approximate collection names by parsing connectionStatus
> show collections
Warning: unable to run listCollections, attempting to approximate collection names by parsing connectionStatus
> show dbs
> show databases
> db.auth("admin","luckserver")
Error: Authentication failed.
0
> use admin
switched to db admin
>  db.auth("admin","luckserver")
1
> show dbs
admin          0.000GB
config         0.000GB
local          0.000GB
test-20240315  0.000GB
> 

示例:
在这里插入图片描述

常用命令

#设置管理员用户名与密码需要切换到admin库
use admin
#创建管理员
db.createUser({user:“admin”,pwd:“luckserver”,roles:[“root”]})

#用户认证,返回1表示认证成功
test> use admin
switched to db admin
admin> db.auth(“admin”,“luckserver”)
{ ok: 1 }

  • mongo shell常用命令

命令 说明
show dbs | show databases 显示数据库列表

use 数据库表名 切换数据库,如果不存在创建数据库,刚创建的数据库要想show dbs看到,必须先要插入一些数据

db.dropDatabase() 删除数据库

show collections | show tables 显示当前数据库的集合

db.集合名.stats() 查看集合详情

db.集合.drop() 删除集合

show users 显示当前数据库的用户列表

show roles 显示当前数据库的角色列表

show profile 显示最近发生的操作

load(“xxx.js”) 执行一个js脚本文件

exit | quit() 退出当前shell

help 查看mongodb支持哪些命令

db.help() 查看当前数据库支持的方法

db.集合名.help() 显示集合的帮助信息

db.version() 查看数据库版本

参考:
https://blog.csdn.net/weixin_39085641/article/details/129199330

  • 5
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值