由于目前项目上使用的数据库为,mongodb所以为了配合工作需要在自己电脑上安装mongodb。下面记录下安装的过程
mac os系统信息:
软件 OS X 10.8.2 (12C54)
第一步,下载所需要的文件
- curl -O http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-1.6.3.tgz
- tar xzf mongodb-osx-x86_64-1.6.3.tgz
- sudo mkdir /usr/local/mongodb
- sudo mv mongodb-osx-x86_64-1.6.3 /usr/local/mongodb
- sudo mkdir /usr/local/mongodb_data /var/log/mongodb
- sudo chown -R root /usr/local/mongodb
第二步,配置
- cat << EOF > /usr/local/mongod.conf
- #/usr/local/mongodb/mongod.conf
- # Store data alongside MongoDB instead of the default, /data/db/
- dbpath = /usr/local/mongodb_data
- # Only accept local connections
- bind_ip = 127.0.0.1
- EOF
第三步,配置lauchjob
创建lauch job,用来mongodb开机启动,关机停止,也设置一些日志输出
- cat << EOF >/Library/LaunchDaemons/org.mongodb.mongod.plist
- xml version="1.0" encoding="UTF-8"?>
- "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
- <plist version="1.0">
- <dict>
- <key>Labelkey>
- <string>org.mongodb.mongodstring>
- <key>ProgramArgumentskey>
- <array>
- <string>/usr/local/mongodb/bin/mongodstring>
- <string>runstring>
- <string>--configstring>
- <string>/usr/local/mongodb/mongod.confstring>
- array>
- <key>RunAtLoadkey>
- <true/>
- <key>KeepAlivekey>
- <true/>
- <key>WorkingDirectorykey>
- <string>/usr/local/mongodbstring>
- <key>StandardErrorPathkey>
- <string>/var/log/mongodb/output.logstring>
- <key>StandardOutPathkey>
- <string>/var/log/mongodb/output.logstring>
- dict>
- plist>
- EOF
第四步,加载lauch job
- sudo launchctl load /Library/LaunchDaemons/org.mongodb.mongod.plist
测试运行
访问
http://localhost:28017可以查看状态控制台
添加到path
添加
引用
/usr/local/mongodb/bin
到
引用
$PATH
可以在直接调用mongo console或者使用mongoexport等工具
添加路径可以修改对应的shell profile文件,来添加path也可以通过mac 的paths.d机制实现如下:
如下代码可以自动安装配置mongodb
- #!/bin/bash
- help()
- {
- cat << EOF
- Please Enter yes,as "$0 yes" to install
- This shell will install mongdb on your mac os,if you want to change install dir,
- change the var *Dir at this shell,and you should run this shell as root user,
- before you install Confirm seen as above information.after install you can use
- mongo to run mongdb shell.
- EOF
- }
- MongoDir=/usr/local/mongodb
- DataDir=/usr/local/mongodb_data
- LogDir=/var/log/mongodb
- DownloadDir=`pwd`
- checkdir()
- {
- dir=$1
- if [ ! -d $dir ];then
- mkdir $dir
- else
- echo "$dir exist"
- fi
- }
- download()
- {
- curl -O http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-1.6.3.tgz $DownloadDir
- echo "Downlad mongodb......"
- }
- install()
- {
- cd $DownloadDir
- tar xzf mongodb-osx-x86_64-1.6.3.tgz
- mv mongodb-osx-x86_64-1.6.3/* /usr/local/mongodb
- chown -R root /usr/local/mongodb
- }
- #uninstall mongodb search mongdb version to rm
- config()
- {
- cat > /usr/local/mongodb/mongod.conf << EOF
- # Store data alongside MongoDB instead of the default, /data/db/
- dbpath = /usr/local/mongodb_data
- # Only accept local connections
- bind_ip = 127.0.0.1
- EOF
- }
- lunch_job()
- {
- cat > /Library/LaunchDaemons/org.mongodb.mongod.plist << EOF
- "1.0" encoding="UTF-8"?>
- "-//Apple//DTD PLIST 1.0//EN"
- "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
"1.0" >Label org.mongodb.mongod ProgramArguments /usr/local/mongodb/bin/mongod run --config /usr/local/mongodb/mongod.conf RunAtLoad KeepAlive WorkingDirectory /usr/local/mongodb StandardErrorPath /var/log/mongodb/output.log StandardOutPath /var/log/mongodb/output.log - EOF
- launchctl load /Library/LaunchDaemons/org.mongodb.mongod.plist
- echo "/usr/local/mongodb/bin" > /etc/paths.d/mongodb
- }
- uninstall()
- {
- [ -d $MongoDir ] && rm -r $MongoDir
- [ -d $DataDir ] && rm -r $DataDir
- [ -d $LogDir ] && rm -r $LogDir
- [ -f /etc/paths.d/mongodb ] && rm /etc/paths.d/mongodb
- [ -f /Library/LaunchDaemons/org.mongodb.mongod.plist ] && rm /Library/LaunchDaemons/org.mongodb.mongod.plist
- }
- help
- echo "Please Enter \"yes|uninstall\" to install|uninstall mongodb,else exit install:"
- read judge
- case $judge in
- yes|YES)
- checkdir $MongoDir
- checkdir $DataDir
- checkdir $LogDir
- download
- install
- config
- #launchctl mongodb.list
- lunch_job
- echo "Install commplete"
- ;;
- uninstall|UNINSTALL)
- uninstall
- echo "uninstall commplete"
- ;;
- *)
- echo "exit installing.."
- exit 5
- ;;
- esac