1、环境:
虚拟机:VMware Workstation
系统:CentOS7.4 x86_64
2、工具:
我这边用的是Xshell6-6.0.0111r,WinSCP
原因就是方便
3、下载安装包
下载地址:https://www.mongodb.com/download-center#community
我下的这个
4、将下载好的tgz上传至/usr/local
5、解压
[root@localhost local]# tar zxvf mongodb-linux-x86_64-4.0.5.tgz
mongodb-linux-x86_64-4.0.5/README
mongodb-linux-x86_64-4.0.5/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-4.0.5/MPL-2
mongodb-linux-x86_64-4.0.5/LICENSE-Community.txt
mongodb-linux-x86_64-4.0.5/bin/mongodump
mongodb-linux-x86_64-4.0.5/bin/mongorestore
mongodb-linux-x86_64-4.0.5/bin/mongoexport
mongodb-linux-x86_64-4.0.5/bin/mongoimport
mongodb-linux-x86_64-4.0.5/bin/mongostat
mongodb-linux-x86_64-4.0.5/bin/mongotop
mongodb-linux-x86_64-4.0.5/bin/bsondump
mongodb-linux-x86_64-4.0.5/bin/mongofiles
mongodb-linux-x86_64-4.0.5/bin/mongoreplay
mongodb-linux-x86_64-4.0.5/bin/mongod
mongodb-linux-x86_64-4.0.5/bin/mongos
mongodb-linux-x86_64-4.0.5/bin/mongo
mongodb-linux-x86_64-4.0.5/bin/install_compass
[root@localhost local]#
6、将解压后的文件名改成mongodb
[root@localhost local]# mv mongodb-3333 mongodb
7、将mongodb/bin目录下可执行文件添加至PATH路径中
[root@localhost local]# export PATH=/usr/local/mongmodb/bin:$PATH
8、创建数据库目录data和日志目录log以及配置文件夹etc
[root@localhost local]# cd mongodb
[root@localhost mongodb]# mkdir data
[root@localhost mongodb]# mkdir log
9、启动mongodb
mongodb安装好后第一次进入是不需要密码的,也没有任何用户,通过shell命令可直接进入,cd到mongodb目录下的bin文件夹,执行命令./mongo即可
[root@localhost bin]# ./mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/log/mongodb --fork
about to fork child process, waiting until server is ready for connections.
forked process: 1334
child process started successfully, parent exiting
10、用命令连接mongodb(mongodb安装好后第一次进入是不需要密码的,也没有任何用户,通过shell命令可直接进入,cd到mongodb目录下的bin文件夹,执行命令./mongo即可)
[root@localhost bin]# pwd
/usr/local/mongodb/bin
[root@localhost bin]# ./mongo
MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("4ef4b1a1-b4b5-4f80-b94e-9502c375b2d4") }
MongoDB server version: 4.0.5
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2019-01-22T18:07:43.056+0800 I CONTROL [initandlisten]
2019-01-22T18:07:43.056+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten]
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten]
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten]
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten]
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten]
>
11、添加管理员账户
> use admin
switched to db admin
> db.createUser({user:"root",pwd:"root",roles:[{role:"userAdminAnyDatabase",db:"admin"}]});
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
添加完用户后可以使用show users或db.system.users.find()查看已有用户.
> show users
{
"_id" : "admin.root",
"user" : "root",
"db" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1",
"SCRAM-SHA-256"
]
}
> db.system.users.find()
{ "_id" : "admin.root", "user" : "root", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "GhjNktkYhkQdgE1EPCMjPw==", "storedKey" : "2U5KY4+7/E2TGoUnBTuXSLqYbXk=", "serverKey" : "eb2QhDT6GYrqNAC8LxUmLRk2L9c=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "Is3rydBrOA4ygJXy6/Gp3vFXlLcNK3ryEP7gUg==", "storedKey" : "Vd8FHBdcv04rdjvA7ebdgMfjN6JeG5or9yAsr3O44Z0=", "serverKey" : "PbpRcFRKegLp67KSneyRl39j4e8piMDv8Yar0t4IRjQ=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
> 1+1
2
ctrl+c退出脚本
12、将mongod路径添加到系统路径中,方便随处执行mongod命令
在/etc/profile文件中,添加 export PATH=$PATH:/usr/local/mongoDB/mongodbserver/bin
[root@localhost etc]# vi profile
在最后一行加上路径
export PATH=$PATH:/usr/local/mongodb/bin
翻页:ctrl+f ,换行:\+回车
:wq保存退出
13、刷新,重新加载配置文件
[root@localhost etc]# source /etc/profile
14、将mongo路径软链到/usr/bin路径下,方便随处执行mongo命令
[root@localhost etc]# ln -s /usr/local/mongoDB/mongodbserver/bin/mongo /usr/bin/mongo
15、测试是否成功,返回任意目录执行mongo
[root@localhost etc]# cd /
[root@localhost /]# mongo
MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("13bde100-a615-4cfd-a8d2-64e2b8673f17") }
MongoDB server version: 4.0.5
Server has startup warnings:
2019-01-22T18:07:43.056+0800 I CONTROL [initandlisten]
2019-01-22T18:07:43.056+0800 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten]
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** WARNING: This server is bound to localhost.
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** Remote systems will be unable to connect to this server.
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** Start the server with --bind_ip <address> to specify which IP
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** addresses it should serve responses from, or with --bind_ip_all to
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** bind to all interfaces. If this behavior is desired, start the
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** server with --bind_ip 127.0.0.1 to disable this warning.
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten]
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten]
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten]
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten] ** We suggest setting it to 'never'
2019-01-22T18:07:43.057+0800 I CONTROL [initandlisten]
> 1+1
2
关闭mongod服务,执行db.shutdownServer()
> db.shutdownServer()
shutdown command only works with the admin database; try 'use admin'
> use admin
switched to db admin
> db.shutdownServer()
server should be down...
2019-01-23T10:03:27.441+0800 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed
2019-01-23T10:03:27.444+0800 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed
16、MongoDB设置为系统服务并且设置开机启动(也可以用工具直接创建给权限,更方便)
16.1添加MongoDB系统服务
vim /etc/rc.d/init.d/mongod
16.2、将配置复制进去
start() {
/usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/log/mongodb --fork
}
stop() {
/usr/local/mongodb/bin/mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/log/mongodb --shutdown
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo
$"Usage: $0 {start|stop|restart}"
exit 1
esac
16.3、给足权限
chmod +x /etc/rc.d/init.d/mongod
16.4、启动mongodb
[root@localhost /]# service mongod start
about to fork child process, waiting until server is ready for connections.
forked process: 3674
child process started successfully, parent exiting
16.5、检测27017端口
[root@localhost ~]# netstat -tunlp |grep 27017
tcp 0 0 127.0.0.1:27017 0.0.0.0:* LISTEN 3674/mongod
16.6、关闭服务
[root@localhost ~]# service mongod stop
2019-01-23T10:14:19.802+0800 I CONTROL [main] log file "/usr/local/mongodb/log/mongodb" exists; moved to "/usr/local/mongodb/log/mongodb.2019-01-23T02-14-19".
killing process with pid: 3674
mongodb启动的时候我碰到的一个错误,下面说一下我的解决办法
[root@localhost bin]# ./mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/log/ --fork
about to fork child process, waiting until server is ready for connections.
forked process: 1266
ERROR: child process failed, exited with error number 1
To see additional information in this output, start without the "--fork" option.
用修复模式运行以下启动命令
[root@localhost bin]# ./mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/log/ --repair
2019-01-22T17:36:45.701+0800 F CONTROL [main] Failed global initialization: FileNotOpen: logpath "/usr/local/mongodb/log/" should name a file, not a directory.
log日志地址必须给到一个文件
去log文件夹下创建一个mongodb的文件
然后重新启动
[root@localhost bin]# ./mongod --dbpath=/usr/local/mongodb/data/ --logpath=/usr/local/mongodb/log/mongodb --fork
about to fork child process, waiting until server is ready for connections.
forked process: 1334
child process started successfully, parent exiting