宝塔
mongodb
安装v4.4.6
面板上无法启动.service mongodb start
命令行启动提示如下,打开配置文件找到fork
改成false
或直接注销,再次执行启动没有提示信息。
1 | There doesn't seem to be a server running with dbpath: /www/server/mongodb/data |
mongod
服务未启动,启动客户端mongo
报错:
1 | [root@test bin]# ./mongo |
bindIp:
0.0.0.0
不能是127.0.0.1
服务器上测试,一个窗口开启mongod:./mongod --dbpath /www/server/mongodb/data/
,
另一个窗口开启mongo:./mongo
是可以的,但这样肯定是不正常的。
sudo /etc/init.d/mongodb restart
等各种方式都无法正常启动
######最后发现是mongodb使用的端口
27017
未开放。宝塔卸载mongodb
,删除/www/server/mongodb
目录。阿里云开放入端口27017
,源0.0.0.0/0
,重新安装自动开启。
windows
命令行开启mongodb
1 | D:\workplace\mongodb\bin> |
[root@paijinhua new.pjinhua.com]# sudo service mongdb start
Redirecting to /bin/systemctl start mongdb.service
Failed to start mongdb.service: Unit not found.
[root@paijinhua new.pjinhua.com]# /bin/systemctl start mongdb.service
Failed to start mongdb.service: Unit not found.
[root@paijinhua new.pjinhua.com]# /etc/init.d/mongodb start
about to fork child process, waiting until server is ready for connections.
forked process: 32345
ERROR: child process failed, exited with 1
To see additional information in this output, start without the “–fork” option.
1 | >删除日志、`lock`、`sock`文件 |
rm -rf /www/server/mongodb/log/*
rm -rf /www/server/mongodb/data/mongod.lock
rm -rf /tmp/mongodb-17027.sock
1 | >`repair`修复 |
[root@xxx bin]# ./mongod –repair
{“t”:{“$date”:”2022-02-16T10:47:07.737+08:00”},”s”:”I”, “c”:”CONTROL”, “id”:23285, “ctx”:”main”,”msg”:”Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify –sslDisabledProtocols ‘none’”}
{“t”:{“$date”:”2022-02-16T10:47:07.742+08:00”},”s”:”W”, “c”:”ASIO”, “id”:22601, “ctx”:”main”,”msg”:”No TransportLayer configured during NetworkInterface startup”}
{“t”:{“$date”:”2022-02-16T10:47:07.742+08:00”},”s”:”I”, “c”:”NETWORK”, “id”:4648601, “ctx”:”main”,”msg”:”Implicit TCP FastOpen unavailable. If TCP FastOpen is required, set tcpFastOpenServer, tcpFastOpenClient, and tcpFastOpenQueueSize.”}
{“t”:{“$date”:”2022-02-16T10:47:07.742+08:00”},”s”:”I”, “c”:”STORAGE”, “id”:4615611, “ctx”:”initandlisten”,”msg”:”MongoDB starting”,”attr”:{“pid”:1136,”port”:27017,”dbPath”:”/data/db”,”architecture”:”64-bit”,”host”:”paijinhua”}}
{“t”:{“$date”:”2022-02-16T10:47:07.742+08:00”},”s”:”I”, “c”:”CONTROL”, “id”:23403, “ctx”:”initandlisten”,”msg”:”Build Info”,”attr”:{“buildInfo”:{“version”:”4.4.6”,”gitVersion”:”72e66213c2c3eab37d9358d5e78ad7f5c1d0d0d7”,”openSSLVersion”:”OpenSSL 1.0.1e-fips 11 Feb 2013”,”modules”:[],”allocator”:”tcmalloc”,”environment”:{“distmod”:”rhel70”,”distarch”:”x86_64”,”target_arch”:”x86_64”}}}}
{“t”:{“$date”:”2022-02-16T10:47:07.742+08:00”},”s”:”I”, “c”:”CONTROL”, “id”:51765, “ctx”:”initandlisten”,”msg”:”Operating System”,”attr”:{“os”:{“name”:”CentOS Linux release 7.6.1810 (Core) “,”version”:”Kernel 3.10.0-1127.19.1.el7.x86_64”}}}
{“t”:{“$date”:”2022-02-16T10:47:07.742+08:00”},”s”:”I”, “c”:”CONTROL”, “id”:21951, “ctx”:”initandlisten”,”msg”:”Options set by command line”,”attr”:{“options”:{“repair”:true}}}
{“t”:{“$date”:”2022-02-16T10:47:07.743+08:00”},”s”:”E”, “c”:”STORAGE”, “id”:20557, “ctx”:”initandlisten”,”msg”:”DBException in initAndListen, terminating”,”attr”:{“error”:”NonExistentPath: Data directory /data/db not found. Create the missing directory or specify another path using (1) the –dbpath command line option, or (2) by adding the ‘storage.dbPath’ option in the configuration file.”}}
{“t”:{“$date”:”2022-02-16T10:47:07.743+08:00”},”s”:”I”, “c”:”REPL”, “id”:4784900, “ctx”:”initandlisten”,”msg”:”Stepping down the ReplicationCoordinator for shutdown”,”attr”:{“waitTimeMillis”:10000}}
{“t”:{“$date”:”2022-02-16T10:47:07.743+08:00”},”s”:”I”, “c”:”COMMAND”, “id”:4784901, “ctx”:”initandlisten”,”msg”:”Shutting down the MirrorMaestro”}
{“t”:{“$date”:”2022-02-16T10:47:07.743+08:00”},”s”:”I”, “c”:”SHARDING”, “id”:4784902, “ctx”:”initandlisten”,”msg”:”Shutting down the WaitForMajorityService”}
{“t”:{“$date”:”2022-02-16T10:47:07.743+08:00”},”s”:”I”, “c”:”NETWORK”, “id”:4784905, “ctx”:”initandlisten”,”msg”:”Shutting down the global connection pool”}
{“t”:{“$date”:”2022-02-16T10:47:07.743+08:00”},”s”:”I”, “c”:”STORAGE”, “id”:4784906, “ctx”:”initandlisten”,”msg”:”Shutting down the FlowControlTicketholder”}
{“t”:{“$date”:”2022-02-16T10:47:07.743+08:00”},”s”:”I”, “c”:”-“, “id”:20520, “ctx”:”initandlisten”,”msg”:”Stopping further Flow Control ticket acquisitions.”}
{“t”:{“$date”:”2022-02-16T10:47:07.743+08:00”},”s”:”I”, “c”:”NETWORK”, “id”:4784918, “ctx”:”initandlisten”,”msg”:”Shutting down the ReplicaSetMonitor”}
{“t”:{“$date”:”2022-02-16T10:47:07.743+08:00”},”s”:”I”, “c”:”SHARDING”, “id”:4784921, “ctx”:”initandlisten”,”msg”:”Shutting down the MigrationUtilExecutor”}
{“t”:{“$date”:”2022-02-16T10:47:07.743+08:00”},”s”:”I”, “c”:”CONTROL”, “id”:4784925, “ctx”:”initandlisten”,”msg”:”Shutting down free monitoring”}
{“t”:{“$date”:”2022-02-16T10:47:07.743+08:00”},”s”:”I”, “c”:”STORAGE”, “id”:4784927, “ctx”:”initandlisten”,”msg”:”Shutting down the HealthLog”}
{“t”:{“$date”:”2022-02-16T10:47:07.743+08:00”},”s”:”I”, “c”:”STORAGE”, “id”:4784929, “ctx”:”initandlisten”,”msg”:”Acquiring the global lock for shutdown”}
{“t”:{“$date”:”2022-02-16T10:47:07.743+08:00”},”s”:”I”, “c”:”-“, “id”:4784931, “ctx”:”initandlisten”,”msg”:”Dropping the scope cache for shutdown”}
{“t”:{“$date”:”2022-02-16T10:47:07.743+08:00”},”s”:”I”, “c”:”FTDC”, “id”:4784926, “ctx”:”initandlisten”,”msg”:”Shutting down full-time data capture”}
{“t”:{“$date”:”2022-02-16T10:47:07.743+08:00”},”s”:”I”, “c”:”CONTROL”, “id”:20565, “ctx”:”initandlisten”,”msg”:”Now exiting”}
{“t”:{“$date”:”2022-02-16T10:47:07.743+08:00”},”s”:”I”, “c”:”CONTROL”, “id”:23138, “ctx”:”initandlisten”,”msg”:”Shutting down”,”attr”:{“exitCode”:100}}
1 | >重启 |
[root@xxx bin]# sudo ./mongod –config /www/server/mongodb/config.conf
about to fork child process, waiting until server is ready for connections.
forked process: 1602
child process started successfully, parent exiting
1 | > |
/www/server/mongodb/bin/mongo 127.0.0.1:17027/admin -u root -p xxx
#/www/server/mongodb/bin/mongo -host 127.0.0.1 -port 17027 -u root -p xxx
1 |
|
sudo /www/server/mongodb/bin/mongod –config /www/server/mongodb/config.conf #以配置文件启动mongodb
/www/server/mongodb/bin/mongo 127.0.0.1:17027 #命令行客户端进入
show users; #查看用户
2022-06-07T20:56:01.579+0800 E QUERY [js] Error: not authorized on admin to execute command { usersInfo: 1.0, lsid: { id: UUID(“3d7fea38-ec01-462a-bd5f-6c4a51b9396f”) }, $db: “admin” } :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1763:1
shellHelper.show@src/mongo/shell/utils.js:859:9
shellHelper@src/mongo/shell/utils.js:766:15
@(shellhelp2):1:1
db.createUser({user:”root”,pwd:”p20jh21admin-mongodb”,roles:[{role:”root”,db:”admin”]});
…
…
db.getUsers() #查看用户
2022-06-07T21:00:43.059+0800 E QUERY [js] Error: not authorized on admin to execute command { usersInfo: 1.0, lsid: { id: UUID(“3d7fea38-ec01-462a-bd5f-6c4a51b9396f”) }, $db: “admin” } :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.getUsers@src/mongo/shell/db.js:1763:1
@(shell):1:1
use admin #切换admin库
switched to db admin
db.createUser({user:”root”,pwd:”p20jh21admin-mongodb”,roles:[{role:”root”,db:”admin”]}); #创建用户命令写一行,没有提示成功
…
…
db.auth(‘root’,’xxx’) #验证用户登录
Error: Authentication failed.
0
use admin #切换admin库
switched to db admin
db.createUser(
… {
… user:”admin”,
… pwd:”p20jh21admin-mongodb”,
… roles:[{role:”root”,db:”admin”}]
… }
… );
#创建用户提示成功
Successfully added user: {
“user” : “admin”,
“roles” : [
{
“role” : “root”,
“db” : “admin”
}
]
}
use pjh #切换pjh库,创建的用户在php代码中连接mongodb库报错没权限
switched to db pjh
db.createUser(
… {
… user:”pjh”,
… pwd:”xxx”,
… roles:[{role:”readWrite”,db:”pjh”}]
… }
… );
2022-06-07T21:09:38.275+0800 E QUERY [js] Error: couldn’t add user: command createUser requires authentication :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.createUser@src/mongo/shell/db.js:1491:15
@(shell):1:1
exit
show users;
{
“_id” : “admin.admin”,
“userId” : UUID(“d89befe5-6a0b-4feb-9156-c2d31880349b”),
“user” : “admin”,
“db” : “admin”,
“roles” : [
{
“role” : “root”,
“db” : “admin”
}
],
“mechanisms” : [
“SCRAM-SHA-1”,
“SCRAM-SHA-256”
]
}
db.getUsers()
[
{
“_id” : “admin.admin”,
“userId” : UUID(“d89befe5-6a0b-4feb-9156-c2d31880349b”),
“user” : “admin”,
“db” : “admin”,
“roles” : [
{
“role” : “root”,
“db” : “admin”
}
],
“mechanisms” : [
“SCRAM-SHA-1”,
“SCRAM-SHA-256”
]
}
]
use paijinhua
switched to db paijinhua
db.getUsers()
[
{
“_id” : “paijinhua.paijinhua”,
“userId” : UUID(“3c2e6b38-885b-45e4-9498-918ef71bd2d0”),
“user” : “paijinhua”,
“db” : “paijinhua”,
“roles” : [
{
“role” : “readWrite”,
“db” : “paijinhua”
}
],
“mechanisms” : [
“SCRAM-SHA-1”,
“SCRAM-SHA-256”
]
}
]
use admin #切换到admin库创建代码连接mongodb的用户并赋予权限,一定要在admin库创建,在dbname中创建的,代码连接报错没权限。
db.createUser(
{
user:”username”,
pwd:”password”,
roles:[{role:”readWrite”,db:”dbname”},{role:”read”,db:”admin”}]
}
)
___
本人声明: 此文只作为自己日后工作学习中遇到类似问题方便快速回忆解决问题的笔记,仅供参考!
版权声明: 本文首发于 すせなの筆記 转载无需联系本人,但要注明来源本站!