Mac os 安裝mongdb

 由於目前項目上使用的數據庫爲,mongodb所以爲了配合工作需要在自己電腦上安裝mongodb。下面記錄下安裝的過程

 

mac os系統信息:

 

軟件  OS X 10.8.2 (12C54)

 

第一步,下載所需要的文件

  1. curl -O http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-1.6.3.tgz    
  2. tar xzf mongodb-osx-x86_64-1.6.3.tgz
  3. sudo mkdir /usr/local/mongodb    
  4. sudo mv mongodb-osx-x86_64-1.6.3 /usr/local/mongodb    
  5. sudo mkdir /usr/local/mongodb_data /var/log/mongodb    
  6. sudo chown -R root /usr/local/mongodb  

第二步,配置

  1. cat << EOF > /usr/local/mongod.conf 
  2. #/usr/local/mongodb/mongod.conf 
  3.  
  4. # Store data alongside MongoDB instead of the default, /data/db/ 
  5. dbpath = /usr/local/mongodb_data 
  6.  
  7. # Only accept local connections 
  8. bind_ip = 127.0.0.1 
  9. EOF 

第三步,配置lauchjob

創建lauch job,用來mongodb開機啓動,關機停止,也設置一些日誌輸出 

 

  1. cat << EOF >/Library/LaunchDaemons/org.mongodb.mongod.plist 
  2. <?xml version="1.0" encoding="UTF-8"?> 
  3. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" 
  4.   "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
  5. <plist version="1.0"> 
  6. <dict> 
  7.   <key>Label</key> 
  8.   <string>org.mongodb.mongod</string> 
  9.   <key>ProgramArguments</key> 
  10.   <array> 
  11.     <string>/usr/local/mongodb/bin/mongod</string> 
  12.     <string>run</string> 
  13.     <string>--config</string> 
  14.     <string>/usr/local/mongodb/mongod.conf</string> 
  15.   </array> 
  16.   <key>RunAtLoad</key> 
  17.   <true/> 
  18.   <key>KeepAlive</key> 
  19.   <true/> 
  20.   <key>WorkingDirectory</key> 
  21.   <string>/usr/local/mongodb</string> 
  22.   <key>StandardErrorPath</key> 
  23.   <string>/var/log/mongodb/output.log</string> 
  24.   <key>StandardOutPath</key> 
  25.   <string>/var/log/mongodb/output.log</string> 
  26. </dict> 
  27. </plist> 
  28. EOF 

第四步,加載lauch job 

  1. 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機制實現如下: 

 
  1. sudo sh -c 'echo "/usr/local/mongodb/bin" > /etc/paths.d/mongodb'   

 如下代碼可以自動安裝配置mongodb

 

 

  1. #!/bin/bash 
  2.  
  3. help() 
  4. cat << EOF 
  5. Please Enter yes,as "$0 yes" to install  
  6. This shell will install mongdb on your mac os,if you want to change install dir, 
  7. change the var *Dir at this shell,and you should run this shell as root user, 
  8. before you install Confirm seen as above information.after install you can use 
  9. mongo to run mongdb shell. 
  10. EOF 
  11.  
  12.  
  13. MongoDir=/usr/local/mongodb 
  14. DataDir=/usr/local/mongodb_data 
  15. LogDir=/var/log/mongodb 
  16. DownloadDir=`pwd` 
  17. checkdir() 
  18.     dir=$1 
  19.     if [ ! -d $dir ];then 
  20.         mkdir $dir 
  21.     else 
  22.         echo "$dir exist" 
  23.     fi 
  24.  
  25.  
  26. download() 
  27. curl -O http://fastdl.mongodb.org/osx/mongodb-osx-x86_64-1.6.3.tgz $DownloadDir 
  28. echo "Downlad mongodb......" 
  29.  
  30. install() 
  31. cd $DownloadDir 
  32. tar xzf mongodb-osx-x86_64-1.6.3.tgz 
  33. mv mongodb-osx-x86_64-1.6.3/* /usr/local/mongodb 
  34. chown -R root /usr/local/mongodb 
  35. #uninstall mongodb search mongdb version to rm  
  36.  
  37. config() 
  38. cat > /usr/local/mongodb/mongod.conf << EOF  
  39.   
  40. # Store data alongside MongoDB instead of the default, /data/db/  
  41. dbpath = /usr/local/mongodb_data  
  42.   
  43. # Only accept local connections  
  44. bind_ip = 127.0.0.1  
  45. EOF 
  46.  
  47. lunch_job() 
  48. cat > /Library/LaunchDaemons/org.mongodb.mongod.plist  << EOF 
  49. <?xml version="1.0" encoding="UTF-8"?> 
  50.  
  51. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" 
  52.   "http://www.apple.com/DTDs/PropertyList-1.0.dtd"
  53. <plist version="1.0"
  54. <dict> 
  55.   <key>Label</key> 
  56.   <string>org.mongodb.mongod</string> 
  57.   <key>ProgramArguments</key> 
  58.   <array> 
  59.     <string>/usr/local/mongodb/bin/mongod</string> 
  60.     <string>run</string> 
  61.     <string>--config</string> 
  62.     <string>/usr/local/mongodb/mongod.conf</string> 
  63.   </array> 
  64.   <key>RunAtLoad</key> 
  65.   <true/> 
  66.   <key>KeepAlive</key> 
  67.   <true/> 
  68.   <key>WorkingDirectory</key> 
  69.   <string>/usr/local/mongodb</string> 
  70.   <key>StandardErrorPath</key> 
  71.   <string>/var/log/mongodb/output.log</string> 
  72.   <key>StandardOutPath</key> 
  73.   <string>/var/log/mongodb/output.log</string> 
  74. </dict> 
  75. </plist> 
  76. EOF 
  77. launchctl load /Library/LaunchDaemons/org.mongodb.mongod.plist  
  78. echo "/usr/local/mongodb/bin" > /etc/paths.d/mongodb 
  79.  
  80. uninstall() 
  81. [ -d $MongoDir ] && rm -r $MongoDir 
  82. [ -d $DataDir ] && rm -r $DataDir 
  83. [ -d $LogDir ] && rm -r $LogDir 
  84. [ -f /etc/paths.d/mongodb ] && rm /etc/paths.d/mongodb  
  85. [ -f /Library/LaunchDaemons/org.mongodb.mongod.plist ] && rm /Library/LaunchDaemons/org.mongodb.mongod.plist  
  86.  
  87. help 
  88.  
  89. echo "Please Enter \"yes|uninstall\" to install|uninstall mongodb,else exit install:" 
  90. read judge 
  91. case $judge in 
  92.     yes|YES) 
  93.         checkdir $MongoDir 
  94.         checkdir $DataDir 
  95.         checkdir $LogDir 
  96.         download 
  97.         install 
  98.         config 
  99. #launchctl mongodb.list      
  100.         lunch_job 
  101.         echo "Install commplete" 
  102.         ;; 
  103.     uninstall|UNINSTALL) 
  104.         uninstall 
  105.         echo "uninstall commplete" 
  106.         ;; 
  107.     *) 
  108.         echo "exit installing.." 
  109.         exit 5 
  110.         ;; 
  111. esac 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章