赞
踩
4.4后服务的和客户端需要分开安装
yum -y install libcurl openssl xz-libs
mkdir -p /usr/local/mongodb
server目录用于后面存放服务端
client目录用于后面存放客户端
#解压
tar -zxvf mongodb-linux-x86_64-rhel70-6.0.5.tgz
#移动到/usr/local/mongodb
mv mongodb-linux-x86_64-rhel70-6.0.5 /usr/local/mongodb/server
创建存放数据和日志的目录
cd /usr/local/mongodb
mkdir data logs
cd /usr/local/mongodb/server
bin/mongod --port=27017 --dbpath=../data --logpath=../logs/mongodb.log \
--bind_ip=0.0.0.0 --fork
启动后显示
about to fork child process, waiting until server is ready for connections.
forked process: 7887
child process started successfully, parent exiting
[root@vm_11 server]#
参数说明
–dbpath :数据文件存放的目录
–logpath :日志文件,注意是指定文件不是目录
–logappend :使用追加的方式记录日志
–port:设置端口,默认是27017
–bind_ip:默认只监听的网卡
–auth: 开启认证模式
–fork: 后台启动
查询进程
[root@vm_11 server]# ps -ef|grep mongod
root 7887 1 3 21:42 ? 00:00:01 bin/mongod --port=27017 --dbpath=../data --logpath=../logs/mongodb.log --bind_ip=0.0.0.0 --fork
root 7968 7276 0 21:43 pts/0 00:00:00 grep --color=auto mongod
[root@vm_11 server]#
停止服务
[root@vm_11 server]# ps -ef|grep mongod
root 7887 1 3 21:42 ? 00:00:01 bin/mongod --port=27017 --dbpath=../data --logpath=../logs/mongodb.log --bind_ip=0.0.0.0 --fork
root 7968 7276 0 21:43 pts/0 00:00:00 grep --color=auto mongod
[root@vm_11 server]# bin/mongod --port=27017 --dbpath=../data --logpath=../logs/mongodb.log --bind_ip=0.0.0.0 --shutdown
{"t":{"$date":"2023-05-11T13:52:30.599Z"},"s":"I", "c":"CONTROL", "id":20697, "ctx":"-","msg":"Renamed existing log file","attr":{"oldLogPath":"/usr/local/mongodb/server/../logs/mongodb.log","newLogPath":"/usr/local/mongodb/server/../logs/mongodb.log.2023-05-11T13-52-30"}}
Killing process with pid: 7887
再次查询进程
[root@vm_11 server]# ps -ef|grep mongod
root 8536 7276 0 21:54 pts/0 00:00:00 grep --color=auto mongod
[root@vm_11 server]#
官方配置文件说明文档:https://www.mongodb.com/docs/manual/reference/configuration-options/
#进入mongodb目录 cd /usr/local/mongodb #创建配置文件 vi mongo.conf #输入下面的内容 systemLog: destination: file path: ./logs/mongod.log # 日志路径,这里设置相对于当前文件的路径,也可以使用绝对路径 logAppend: true storage: dbPath: ./data # 数据目录 engine: wiredTiger #存储引擎 journal: #是否启用journal日志 enabled: true net: bindIp: 0.0.0.0 port: 27017 # port processManagement: fork: true security: authorization: enabled #是否开启安全校验,默认不开启,可填disabled(不开启)/enabled (开启)
注意:如果后面需要配置环境变量启动,那么这里日志和数据目录应该使用绝对路径
[root@vm_11 /]# cd /usr/local/mongodb/
[root@vm_11 mongodb]# ./server/bin/mongod -f mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 8815
child process started successfully, parent exiting
使用查询进程命令
ps -ef|grep mongod
然后执行 kill -9 进程号
#前面的启动方式是
./server/bin/mongod -f mongo.conf
#添加shutdown关闭
./server/bin/mongod -f mongo.conf --shutdown
db.shutdownServer
使用关闭服务的命令关闭正在运行的服务
修改/etc/profile
vi /etc/profile
#在文件末尾输入下面的内容,我服务端的目录是/usr/local/mongodb/server
export MONGODB_HOME=/usr/local/mongodb/server
PATH=$PATH:$MONGODB_HOME/bin
#保存后执行刷新命令使文件立马生效
source /etc/profile
通过前面的配置文件在任意启动,执行
[root@vm_11 /]# mongod -f /usr/local/mongodb/mongo.conf
about to fork child process, waiting until server is ready for connections.
forked process: 13387
child process started successfully, parent exiting
[root@vm_11 /]#
这里需要注意的,如果配置文件里的目录使用相对路径,用这种方式会启动报错,所以需要将前面配置文件里的日志和数据目录修改成绝对路径
tar -zxvf mongosh-1.8.2-linux-x64.tgz
mv mongosh-1.8.2-linux-x64 /usr/local/mongodb/client
[root@vm_11 mongodb]cd /usr/local/mongodb/client/bin
[root@vm_11 bin]./mongosh
Current Mongosh Log ID: 645cf1854d8f8ffc61d87251
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.8.2
Using MongoDB: 6.0.5
Using Mongosh: 1.8.2
...
#显示所有数据库
test> show dbs
admin 40.00 KiB
config 12.00 KiB
local 40.00 KiB
#退出客户端
test> quit
解压后的文件并在该目录下创建data和logs目录
打开cmd然后进入bin目录,执行
mongod.exe --port=27017 --dbpath=../data --logpath=../logs/mongodb.log
启动成功后会显示
{"t":{"$date":"2023-05-11T15:10:39.285Z"},"s":"I", "c":"CONTROL", "id":20697, "ctx":"-","msg":"Renamed existing log file","attr":{"oldLogPath":"D:\\sql\\mongodb\\mongodb-windows-x86_64-6.0.5\\bin\\../logs/mongodb.log","newLogPath":"D:\\sql\\mongodb\\mongodb-windows-x86_64-6.0.5\\bin\\../logs/mongodb.log.2023-05-11T15-10-39"}}
解压后的目录
打开新的cmd进入bin,执行
mongosh.exe mongodb://127.0.0.1:27017
帮助命令可以执行
mongosh.exe -h
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。