由於目前項目上使用的數據庫爲,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"?>
- <!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/mongod.conf</string>
- </array>
- <key>RunAtLoad</key>
- <true/>
- <key>KeepAlive</key>
- <true/>
- <key>WorkingDirectory</key>
- <string>/usr/local/mongodb</string>
- <key>StandardErrorPath</key>
- <string>/var/log/mongodb/output.log</string>
- <key>StandardOutPath</key>
- <string>/var/log/mongodb/output.log</string>
- </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
- <?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/mongod.conf</string>
- </array>
- <key>RunAtLoad</key>
- <true/>
- <key>KeepAlive</key>
- <true/>
- <key>WorkingDirectory</key>
- <string>/usr/local/mongodb</string>
- <key>StandardErrorPath</key>
- <string>/var/log/mongodb/output.log</string>
- <key>StandardOutPath</key>
- <string>/var/log/mongodb/output.log</string>
- </dict>
- </plist>
- 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