mongodb针对不同操作系统有不同的安装包,直接解压就可以用将mongodb/bin加入到环境变量中方便使用
通过mongod -h可以查看使用说明,通常通过一个配置文件来启动,那样更方便和灵活
举例如下:
cat mongod.conf
dbpath = /Users/ranyuan/Desktop/mongodb/data //数据文件存放位置
logpath = /Users/ranyuan/Desktop/mongodb/mongod.log //日志位置
logappend = true //追加日志
port = 27017 //指定端口
fork = true //放到后台守护进程
auth = true //是否需要认证
bind_ip = 192.168.5.5 //指定IP,如果不指定,所有网卡的IP都可以访问
maxConns = 79 // 最大同时连接数,如果不指定默认是一百万
上面只举例了几个简单的参数,更多参数的使用请mongod -h来查看,写得很详细
1.根据参数文件来启动数据库
ranyuandeMacBook-Pro:mongodb ranyuan$ mongod -f /Users/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 4099
child process started successfully, parent exiting
说明启动成功了
可以通过如下查看进程:
ranyuandeMacBook-Pro:mongodb ranyuan$ ps -ef | grep mongod | grep -v grep
501 4099 1 0 7:06PM ?? 0:00.27 mongod -f mongod.conf
也可以查看端口情况:
ranyuandeMacBook-Pro:mongodb ranyuan$ netstat -an | grep 27017
tcp4 0 0 *.27017 *.* LISTEN
6e8a68a54da8d1ff stream 0 0 6e8a68a53c5c5f27 0 0 0 /tmp/mongodb-27017.sock
2.连接数据库
mongo --prot 27017
mongo 192.169.0.5:9999/foo
mongo 192.169.0.5:9999/foo -u root -p redhat --authenticationDatabase admin
详情请mongo -h查看帮助文档
3.关闭数据库
可以通过
ranyuandeMacBook-Pro:mongodb ranyuan$ mongo
MongoDB shell version: 2.6.4
connecting to: test
> use admin
switched to db admin
> db.shutdownServer()
2014-10-21T18:53:51.402+0800 DBClientCursor::init call() failed
server should be down...
2014-10-21T18:53:51.405+0800 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2014-10-21T18:53:51.406+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2014-10-21T18:53:51.406+0800 reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
通过mongod -h可以查看使用说明,通常通过一个配置文件来启动,那样更方便和灵活
举例如下:
cat mongod.conf
dbpath = /Users/ranyuan/Desktop/mongodb/data //数据文件存放位置
logpath = /Users/ranyuan/Desktop/mongodb/mongod.log //日志位置
logappend = true //追加日志
port = 27017 //指定端口
fork = true //放到后台守护进程
auth = true //是否需要认证
bind_ip = 192.168.5.5 //指定IP,如果不指定,所有网卡的IP都可以访问
maxConns = 79 // 最大同时连接数,如果不指定默认是一百万
上面只举例了几个简单的参数,更多参数的使用请mongod -h来查看,写得很详细
1.根据参数文件来启动数据库
ranyuandeMacBook-Pro:mongodb ranyuan$ mongod -f /Users/mongod.conf
about to fork child process, waiting until server is ready for connections.
forked process: 4099
child process started successfully, parent exiting
说明启动成功了
可以通过如下查看进程:
ranyuandeMacBook-Pro:mongodb ranyuan$ ps -ef | grep mongod | grep -v grep
501 4099 1 0 7:06PM ?? 0:00.27 mongod -f mongod.conf
也可以查看端口情况:
ranyuandeMacBook-Pro:mongodb ranyuan$ netstat -an | grep 27017
tcp4 0 0 *.27017 *.* LISTEN
6e8a68a54da8d1ff stream 0 0 6e8a68a53c5c5f27 0 0 0 /tmp/mongodb-27017.sock
2.连接数据库
mongo --prot 27017
mongo 192.169.0.5:9999/foo
mongo 192.169.0.5:9999/foo -u root -p redhat --authenticationDatabase admin
详情请mongo -h查看帮助文档
3.关闭数据库
可以通过
ranyuandeMacBook-Pro:mongodb ranyuan$ mongo
MongoDB shell version: 2.6.4
connecting to: test
> use admin
switched to db admin
> db.shutdownServer()
2014-10-21T18:53:51.402+0800 DBClientCursor::init call() failed
server should be down...
2014-10-21T18:53:51.405+0800 trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2014-10-21T18:53:51.406+0800 warning: Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2014-10-21T18:53:51.406+0800 reconnect 127.0.0.1:27017 (127.0.0.1) failed failed couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed