• Mongodb 安装脚本(附服务器自启动)


    shell脚本

    1. #!/bin/bash
    2. #mail:xuel@anchnet.com
    3. #function:auto install mongodb
    4. [ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
    5. logfile="/var/log/mongod_install.log"
    6. softdir="/software"
    7. installdir="/usr/local"
    8. sys_version=$(rpm -q centos-release|cut -d- -f3)
    9. clear
    10. echo "##########################################"
    11. echo "# Auto Install mongodb for centos6/7.x ##"
    12. echo "# Press Ctrl + C to cancel ##"
    13. echo "# Any key to continue ##"
    14. echo "##########################################"
    15. echo "(1) Install Mongodb-3.2"
    16. echo "(2) Install Mongodb-3.4"
    17. echo "(3) Install Mongodb-3.6"
    18. echo "(4) EXIT"
    19. read -p "Please input your choice:" NUM
    20. if [ ${sys_version} == "6" ];then
    21. case $NUM in
    22. 1)
    23. mongodb_url="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.20.tgz"
    24. software_version="mongodb-3.2"
    25. ;;
    26. 2)
    27. mongodb_url="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.10.tgz"
    28. software_version="mongodb-3.4"
    29. ;;
    30. 3)
    31. mongodb_url="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.6.5.tgz"
    32. software_version="mongodb-3.6"
    33. ;;
    34. 4)
    35. echo -e "\033[41;37m You choice channel! \033[0m" && exit 0
    36. ;;
    37. *)
    38. echo -e "\033[41;37m Input Error! Place input{1|2|3|4} \033[0m" && exit 1
    39. ;;
    40. esac
    41. elif [ ${sys_version} == "7" ];then
    42. case $NUM in
    43. 1)
    44. mongodb_url="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.2.20.tgz"
    45. software_version="mongodb-3.2"
    46. ;;
    47. 2)
    48. mongodb_url="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.10.tgz"
    49. software_version="mongodb-3.4"
    50. ;;
    51. 3)
    52. mongodb_url="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.5.tgz"
    53. software_version="mongodb-3.6"
    54. ;;
    55. 4)
    56. echo -e "\033[41;37m You choice channel! \033[0m" && exit 0
    57. ;;
    58. *)
    59. echo -e "\033[41;37m Input Error! Place input{1|2|3|4} \033[0m" && exit 1
    60. ;;
    61. esac
    62. else
    63. echo "system must user centos6/7.x." >>${logfile} 2>&1
    64. fi
    65. sys_init() {
    66. clear
    67. echo -e "\033[42;5m initialization system... \033[0m"
    68. sleep 2
    69. sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
    70. if [ ${sys_version} == "6" ];then
    71. /etc/init.d/iptables status >/dev/null
    72. [ $? -eq 0 ] && iptables -I INPUT -p tcp --dport 27017 -j ACCEPT
    73. [ $? -eq 0 ] && /etc/init.d/iptables save >${logfile} 2>&1
    74. elif [ ${sys_version} == "7" ];then
    75. systemctl stop firewalld && systemctl disable firewalld
    76. else
    77. echo "system must user centos6/7.x." >>${logfile} 2>&1
    78. fi
    79. yum -y install wget >/dev/null
    80. setenforce 0
    81. echo "sys_init complate!">> ${logfile}
    82. }
    83. download_software() {
    84. clear
    85. echo -e "\033[42;5m download software... \033[0m"
    86. sleep 2
    87. if [ ! -d ${softdir} ];then
    88. mkdir ${softdir} && cd ${softdir}
    89. else
    90. cd ${softdir}
    91. fi
    92. for software_url in ${mongodb_url}
    93. do
    94. wget -c ${software_url} --tries=5
    95. if [ $? -eq 0 ];then
    96. for software in `ls`
    97. do
    98. tar zxf $software -C $installdir
    99. done
    100. else
    101. echo "download software error!" >> ${logfile} 2>&1 && exit 1
    102. fi
    103. done
    104. echo "download_software" >>${logfile}
    105. }
    106. install_software() {
    107. clear
    108. echo -e "\033[42;5m install server... \033[0m"
    109. sleep 2
    110. mongodbdir=$(ls ${installdir}|grep "mongodb-linux-x86_64")
    111. ln -s ${installdir}/${mongodbdir} ${installdir}/mongodb
    112. mkdir ${installdir}/mongodb/{conf,mongoData,mongoLog}
    113. touch ${installdir}/mongodb/mongoLog/mongodb.log
    114. echo "export PATH=\$PATH:${installdir}/mongodb/bin">/etc/profile.d/mongodb.sh
    115. source /etc/profile.d/mongodb.sh
    116. cat >${installdir}/mongodb/conf/mongodb.conf <<EOF
    117. dbpath=${installdir}/mongodb/mongoData
    118. logpath=${installdir}/mongodb/mongoLog/mongodb.log
    119. logappend=true
    120. journal=true
    121. quiet=true
    122. port=27017
    123. pidfilepath=/var/run/mongod.pid
    124. #replSet =RS
    125. maxConns=20000
    126. #httpinterface=true
    127. fork=true
    128. #auth=true
    129. EOF
    130. echo "install_software complate!" >>${logfile}
    131. }
    132. start_server() {
    133. clear
    134. echo -e "\033[42;5m configuration server... \033[0m"
    135. if [ ${sys_version} == "6" ];then
    136. cat >/etc/init.d/mongodb-server<<EOF
    137. #!/bin/bash
    138. #auth:kaliarch
    139. # mongodb Startup script for mongodb processes
    140. #
    141. # chkconfig: - 90 10
    142. # description: Mongodb provides fast memory based storage.
    143. # processname: Mongodb
    144. . /etc/rc.d/init.d/functions
    145. bash_dir="/usr/local/mongodb"
    146. mongod="\${bash_dir}/bin/mongod"
    147. config="\${bash_dir}/conf/mongodb.conf"
    148. getpid=\$(pidof mongod)
    149. lockfile="\${bash_dir}/mongodb.lock"
    150. pidfile="/var/run/mongod.pid"
    151. #user=nobody
    152. start() {
    153. action $"Starting \$prog: " /bin/true
    154. # Starting mongodb on port 27017 as deamon and user nobody
    155. \$mongod -f \${config} >/dev/null
    156. RETVAL=$?
    157. [ \$RETVAL = 0 ] && touch \${lockfile}
    158. return \$RETVAL
    159. }
    160. stop() {
    161. if test "x\${getpid}" != x; then
    162. action $"Stopping \$prog " /bin/true
    163. killall mongod
    164. fi
    165. RETVAL=\$?
    166. [ \$RETVAL = 0 ] && rm -rf \${lockfile} \${pidfile}
    167. return \$RETVAL
    168. }
    169. case "\$1" in
    170. start)
    171. start
    172. ;;
    173. stop)
    174. stop
    175. ;;
    176. status)
    177. status -p \${pidfile} \${mongod}
    178. RETVAL=\$?
    179. ;;
    180. restart)
    181. stop
    182. start
    183. ;;
    184. *)
    185. echo $"Usage: \$0 {start|status|stop|restart}"
    186. exit 1
    187. esac
    188. exit \${RETVAL}
    189. EOF
    190. cd /
    191. chmod +x /etc/init.d/mongodb-server
    192. chkconfig mongodb-server on
    193. service mongodb-server start
    194. elif [ ${sys_version} == "7" ];then
    195. cat >/usr/lib/systemd/system/mongod.service<<EOF
    196. [Unit]
    197. Description=The Mongodb Server
    198. After=network.target remote-fs.target nss-lookup.target
    199. [Service]
    200. Type=forking
    201. ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf
    202. ExecStop=/usr/local/mongodb/bin/mongod --shutdown --dbpath /usr/local/mongodb/mongoData
    203. [Install]
    204. WantedBy=multi-user.target
    205. EOF
    206. systemctl start mongod
    207. systemctl enable mongod >>${logfile} 2>&1
    208. else
    209. echo "install occer error,please see ${logfile}" && exit 1
    210. fi
    211. }
    212. check_server() {
    213. clear
    214. echo -e "\033[42;5m check server status... \033[0m"
    215. server_port=$(netstat -lntup|grep mongod|wc -l)
    216. server_proc=$(ps -ef |grep mongodb.conf|grep -v grep|wc -l)
    217. if [ ${server_port} -gt 0 -a ${server_port} -gt 0 ];then
    218. echo -e "\033[42;37m mongodb-server install successful! \033[0m"
    219. echo -e "\033[42;37m version:${software_version} \033[0m"
    220. echo -e "\033[42;37m bashpath:${installdir}/mongodb \033[0m"
    221. else
    222. echo "install occer error,please see ${logfile}" && exit 1
    223. fi
    224. }
    225. main() {
    226. sys_init
    227. download_software
    228. install_software
    229. start_server
    230. check_server
    231. }
    232. main

    保存 install_mongo.sh

    1. --.查看mongo是否启动
    2. ps aux |grep mongo
    3. --查看是否启动成功
    4. --命令如下
    5. ps -ef|grep mongo

    1. systemctl start mongod.service
    2. systemctl status mongod.service
    3. 设置为开机自启动
    4. systemctl enable mongod.service
    5. systemctl enable mongod //开机自启MongoDB
    6. systemctl start mongod //启动MongoDB
    7. systemctl status mongod //可以检查是否启动了MongoDB

    1. netstat -an | grep 27017
    2. netstat -ntulp | grep 27017
    3. netstat -lntup | grep 27017
    4. netstat -lanp | grep "27017"

    find / -name mongodb.conf

    cd /usr/local/mongodb-linux-x86_64-rhel70-3.6.5/conf/
    

    vim mongodb.conf

    1. #端口号
    2. port = 27017
    3. #数据目录
    4. dbpath =/var/lib/mongodb/
    5. #日志目录
    6. logpath =/var/log/mongodb/mongo.log
    7. #设置后台运行,守护进程
    8. fork = true
    9. #日志输出方式----是否追加日志
    10. logappend = true
    11. #开启认证(暂时不开启)
    12. #auth = true
    13. #最大同时连接数
    14. maxConns=100
    15. #不启用验证
    16. noauth=true
    17. #每次写入会记录一条操作日志(通过journal可以重新构造出写入的数据)。
    18. #即使宕机,启动时wiredtiger会先将数据恢复到最近一次的checkpoint点,
    19. #然后重放后续的journal日志来恢复。
    20. journal=true
    21. #存储引擎,有mmapv1、wiretiger、mongorocks
    22. storageEngine=wiredTiger
    23. #设置成全部ip可以访问,这样就可以在windows中去连虚拟机的MongoDB,
    24. #也可以设置成某个网段或者某个ip1234567891011
    25. bind_ip = 0.0.0.0

    7.在shell中使用mongo来连接mongodb,通过mongodb相关命令来进行mongodb的管理cd /usr/local/mongodb/bin./mongo

     

    MongoDB对用户的操作

    • 创建用户

    MongoDB创建用户必须进入到相关数据库下进行创建

    1. >use admin
    2. >db.createUser({
    3. user: 'admin', // 用户名(自定义)
    4. pwd: 'my_db1', // 密码(自定义)
    5. roles:[{
    6. role: 'root', // 使用超级用户角色
    7. db: 'admin' // 指定数据库
    8. }]})

    1. [root@k8s-vanode1 bin]# systemctl start firewalld
    2. [root@k8s-vanode1 bin]# firewall-cmd --add-port=27017/tcp --zone=public --permanent
    3. success
    4. [root@k8s-vanode1 bin]# firewall-cmd --reload
    5. success
    6. [root@k8s-vanode1 bin]# firewall-cmd --zone=public --list-ports
    7. 27017/tcp
    8. [root@k8s-vanode1 bin]# systemctl stop firewalld
    9. [root@k8s-vanode1 bin]#

  • 相关阅读:
    SpringBoot之Dockerfile
    【教3妹学算法-每日3题(1)】我的日程安排表 I
    【智能可视化---01】揭示Python AI中Matplotlib的魅力,提升数据洞察力!探索AI 中的Matplotlib,这一篇就够了!
    https想访问本地部署的http://localhost接口
    2022操作系统实验李丁丁
    命令执行漏洞
    【Leetcode】剑指Offer 29:顺时针打印矩阵
    牛客小白月赛60-D-游戏购买!
    Tableau 合集2:Table Extension通过python做词云图
    协议和分层次
  • 原文地址:https://blog.csdn.net/u011458344/article/details/132739766