美文网首页
在Mac上设置mongo的开机启动

在Mac上设置mongo的开机启动

作者: Kenis | 来源:发表于2018-06-25 11:37 被阅读455次

1.前往官网下载MongoDB压缩包


image

2.将下载好的压缩包解压,将解压出的文件夹下的内容全部复制到新的路径下。
cp -r mongodb-osx-x86_64-3.0.4 /usr/local/mongodb


3.在新建立的文件夹下建立data文件夹用来记录数据,log文件夹用来记录日志
cd /usr/local/mongodb
mkdir data
mkdir log


4.进入bin目录下,创建mongodb.conf配置文件
cd bin
vim mongodb.conf


5.编写配置文件
port=27017
dbpath=/usr/local/mongodb/data/
logpath=/usr/local/mongodb/log/mongodb.log
fork = true
port: 数据库服务使用端口
dbpath: 数据存放的文件位置
logpath: 日志文件的存放位置
fork: 后台守护进程运行


6.启动
在bin路径下,执行
./mongod -f mongodb.conf
-f 后面写要使用的配置文件
启动成功后会打印类似于这样的信息:
about to fork child process, waiting until server is ready for connections.
forked process: 779
child process started successfully, parent exiting
如果未启动成功,错误信息如下的话:
about to fork child process, waiting until server is ready for connections.
forked process: 760
ERROR: child process failed, exited with error number 1
一般情况下是权限问题,使用sodu操作来解决,
也可能是配置文件中路径写的有问题。


7.在bin目录下进入MongoDB控制台
./mongo


8.关闭MongoDB服务
在 ./mongo 进入控制台后,输入 use admin,然后输入 db.shutdownServer()


9.查看 mongo 用法
在bin目录下输入:
./mongo --help
内容显示如下
MongoDB shell version: 3.0.4
usage: ./mongo [options] [db address] [file names (ending in .js)]
db address can be:
foo foo database on local machine
192.169.0.5/foo foo database on 192.168.0.5 machine
192.169.0.5:9999/foo foo database on 192.168.0.5 machine on port 9999
Options:
--shell run the shell after executing files
--nodb don't connect to mongod on startup - no
'db address' arg expected
--norc will not run the ".mongorc.js" file on
start up
--quiet be less chatty
--port arg port to connect to
--host arg server to connect to
--eval arg evaluate javascript
-h [ --help ] show this usage information
--version show version information
--verbose increase verbosity
--ipv6 enable IPv6 support (disabled by default)

Authentication Options:
-u [ --username ] arg username for authentication
-p [ --password ] arg password for authentication
--authenticationDatabase arg user source (defaults to dbname)
--authenticationMechanism arg authentication mechanism
--gssapiServiceName arg (=mongodb) Service name to use when authenticating
using GSSAPI/Kerberos
--gssapiHostName arg Remote host name to use for purpose of
GSSAPI/Kerberos authentication

file names: a list of files to run. files have to end in .js and will exit after unless --shell is specified

  1. 连接数据库
    在bin目录下执行:
    ./mongo 127.0.0.1:27017/test
    ./mongo + 服务器IP:端口号/数据库名
    服务器IP我这写的是本机,端口号是刚才在配置文件中配置的那个,数据库名自己起
    回车后如果能看到向右的箭头,说明已经成功了
    11.查看日志
    tail -f log/mongodb.log
    log/mongod.log 为日志存放路径
    12.配置开机启动
    01.确保终端运行 somepath/mongod --config somepath/mongodb.conf 可正常启动
    somepath指用户特定路径,由用户自己决定
    02.创建一个plist文件
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
    "http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
    <plist version='1.0'>
    <dict>
    <key>Label</key>
    <string>org.mongodb.mongod</string>
    <key>ProgramArguments</key>
    <array>
    <string>/usr/local/mongodb/bin/mongod</string>
    <string>run</string>
    <string>--config</string>
    <string>/usr/local/mongodb/bin/mongodb.conf</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>WorkingDirectory</key>
    <string>/usr/local/mongodb</string>
    <key>StandardErrorPath</key>
    <string>/usr/local/mongodb/log/error.log</string>
    <key>StandardOutPath</key>
    <string>/usr/local/mongodb/log/output.log</string>
    </dict>
    </plist>
    放置在/Library/LaunchDaemons目录下,如果不在这个目录,就放到对应正确路径下
    03.执行命令,将mongo服务加入开机启动进程中
    launchctl load /Library/LaunchDaemons/org.mongodb.mongod.plist

篇尾小结:

  1. 使用cp命令拷贝文件,加上 -r参数 表示级联拷贝
  2. 在mongod工具后 使用--config 用于读取配置文件中的配置项
  3. 在执行启动mongo服务时加上sudo
    4.执行./mongo可进入mongo服务台
  4. tail命令配合-f参数可以用于查看日志

相关文章

网友评论

      本文标题:在Mac上设置mongo的开机启动

      本文链接:https://www.haomeiwen.com/subject/aigeyftx.html