shell脚本
#!/bin/bash
#mail:xuel@anchnet.com
#function:auto install mongodb
[ $(id -u) != "0" ] && echo "Error: You must be root to run this script" && exit 1
logfile="/var/log/mongod_install.log"
softdir="/software"
installdir="/usr/local"
sys_version=$(rpm -q centos-release|cut -d- -f3)
clear
echo "##########################################"
echo "# Auto Install mongodb for centos6/7.x ##"
echo "# Press Ctrl + C to cancel ##"
echo "# Any key to continue ##"
echo "##########################################"
echo "(1) Install Mongodb-3.2"
echo "(2) Install Mongodb-3.4"
echo "(3) Install Mongodb-3.6"
echo "(4) EXIT"
read -p "Please input your choice:" NUM
if [ ${sys_version} == "6" ];then
case $NUM in
1)
mongodb_url="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.2.20.tgz"
software_version="mongodb-3.2"
;;
2)
mongodb_url="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.10.tgz"
software_version="mongodb-3.4"
;;
3)
mongodb_url="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.6.5.tgz"
software_version="mongodb-3.6"
;;
4)
echo -e "\033[41;37m You choice channel! \033[0m" && exit 0
;;
*)
echo -e "\033[41;37m Input Error! Place input{1|2|3|4} \033[0m" && exit 1
;;
esac
elif [ ${sys_version} == "7" ];then
case $NUM in
1)
mongodb_url="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.2.20.tgz"
software_version="mongodb-3.2"
;;
2)
mongodb_url="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.10.tgz"
software_version="mongodb-3.4"
;;
3)
mongodb_url="https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.5.tgz"
software_version="mongodb-3.6"
;;
4)
echo -e "\033[41;37m You choice channel! \033[0m" && exit 0
;;
*)
echo -e "\033[41;37m Input Error! Place input{1|2|3|4} \033[0m" && exit 1
;;
esac
else
echo "system must user centos6/7.x." >>${logfile} 2>&1
fi
sys_init() {
clear
echo -e "\033[42;5m initialization system... \033[0m"
sleep 2
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
if [ ${sys_version} == "6" ];then
/etc/init.d/iptables status >/dev/null
[ $? -eq 0 ] && iptables -I INPUT -p tcp --dport 27017 -j ACCEPT
[ $? -eq 0 ] && /etc/init.d/iptables save >${logfile} 2>&1
elif [ ${sys_version} == "7" ];then
systemctl stop firewalld && systemctl disable firewalld
else
echo "system must user centos6/7.x." >>${logfile} 2>&1
fi
yum -y install wget >/dev/null
setenforce 0
echo "sys_init complate!">> ${logfile}
}
download_software() {
clear
echo -e "\033[42;5m download software... \033[0m"
sleep 2
if [ ! -d ${softdir} ];then
mkdir ${softdir} && cd ${softdir}
else
cd ${softdir}
fi
for software_url in ${mongodb_url}
do
wget -c ${software_url} --tries=5
if [ $? -eq 0 ];then
for software in `ls`
do
tar zxf $software -C $installdir
done
else
echo "download software error!" >> ${logfile} 2>&1 && exit 1
fi
done
echo "download_software" >>${logfile}
}
install_software() {
clear
echo -e "\033[42;5m install server... \033[0m"
sleep 2
mongodbdir=$(ls ${installdir}|grep "mongodb-linux-x86_64")
ln -s ${installdir}/${mongodbdir} ${installdir}/mongodb
mkdir ${installdir}/mongodb/{conf,mongoData,mongoLog}
touch ${installdir}/mongodb/mongoLog/mongodb.log
echo "export PATH=\$PATH:${installdir}/mongodb/bin">/etc/profile.d/mongodb.sh
source /etc/profile.d/mongodb.sh
cat >${installdir}/mongodb/conf/mongodb.conf <<EOF
dbpath=${installdir}/mongodb/mongoData
logpath=${installdir}/mongodb/mongoLog/mongodb.log
logappend=true
journal=true
quiet=true
port=27017
pidfilepath=/var/run/mongod.pid
#replSet =RS
maxConns=20000
#httpinterface=true
fork=true
#auth=true
EOF
echo "install_software complate!" >>${logfile}
}
start_server() {
clear
echo -e "\033[42;5m configuration server... \033[0m"
if [ ${sys_version} == "6" ];then
cat >/etc/init.d/mongodb-server<<EOF
#!/bin/bash
#auth:kaliarch
# mongodb Startup script for mongodb processes
#
# chkconfig: - 90 10
# description: Mongodb provides fast memory based storage.
# processname: Mongodb
. /etc/rc.d/init.d/functions
bash_dir="/usr/local/mongodb"
mongod="\${bash_dir}/bin/mongod"
config="\${bash_dir}/conf/mongodb.conf"
getpid=\$(pidof mongod)
lockfile="\${bash_dir}/mongodb.lock"
pidfile="/var/run/mongod.pid"
#user=nobody
start() {
action $"Starting \$prog: " /bin/true
# Starting mongodb on port 27017 as deamon and user nobody
\$mongod -f \${config} >/dev/null
RETVAL=$?
[ \$RETVAL = 0 ] && touch \${lockfile}
return \$RETVAL
}
stop() {
if test "x\${getpid}" != x; then
action $"Stopping \$prog " /bin/true
killall mongod
fi
RETVAL=\$?
[ \$RETVAL = 0 ] && rm -rf \${lockfile} \${pidfile}
return \$RETVAL
}
case "\$1" in
start)
start
;;
stop)
stop
;;
status)
status -p \${pidfile} \${mongod}
RETVAL=\$?
;;
restart)
stop
start
;;
*)
echo $"Usage: \$0 {start|status|stop|restart}"
exit 1
esac
exit \${RETVAL}
EOF
cd /
chmod +x /etc/init.d/mongodb-server
chkconfig mongodb-server on
service mongodb-server start
elif [ ${sys_version} == "7" ];then
cat >/usr/lib/systemd/system/mongod.service<<EOF
[Unit]
Description=The Mongodb Server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/conf/mongodb.conf
ExecStop=/usr/local/mongodb/bin/mongod --shutdown --dbpath /usr/local/mongodb/mongoData
[Install]
WantedBy=multi-user.target
EOF
systemctl start mongod
systemctl enable mongod >>${logfile} 2>&1
else
echo "install occer error,please see ${logfile}" && exit 1
fi
}
check_server() {
clear
echo -e "\033[42;5m check server status... \033[0m"
server_port=$(netstat -lntup|grep mongod|wc -l)
server_proc=$(ps -ef |grep mongodb.conf|grep -v grep|wc -l)
if [ ${server_port} -gt 0 -a ${server_port} -gt 0 ];then
echo -e "\033[42;37m mongodb-server install successful! \033[0m"
echo -e "\033[42;37m version:${software_version} \033[0m"
echo -e "\033[42;37m bashpath:${installdir}/mongodb \033[0m"
else
echo "install occer error,please see ${logfile}" && exit 1
fi
}
main() {
sys_init
download_software
install_software
start_server
check_server
}
main
保存 install_mongo.sh
--.查看mongo是否启动
ps aux |grep mongo
--查看是否启动成功
--命令如下
ps -ef|grep mongo
systemctl start mongod.service
systemctl status mongod.service
设置为开机自启动
systemctl enable mongod.service
systemctl enable mongod //开机自启MongoDB
systemctl start mongod //启动MongoDB
systemctl status mongod //可以检查是否启动了MongoDB
netstat -an | grep 27017
netstat -ntulp | grep 27017
netstat -lntup | grep 27017
netstat -lanp | grep "27017"
find / -name mongodb.conf
cd /usr/local/mongodb-linux-x86_64-rhel70-3.6.5/conf/
vim mongodb.conf
#端口号
port = 27017
#数据目录
dbpath =/var/lib/mongodb/
#日志目录
logpath =/var/log/mongodb/mongo.log
#设置后台运行,守护进程
fork = true
#日志输出方式----是否追加日志
logappend = true
#开启认证(暂时不开启)
#auth = true
#最大同时连接数
maxConns=100
#不启用验证
noauth=true
#每次写入会记录一条操作日志(通过journal可以重新构造出写入的数据)。
#即使宕机,启动时wiredtiger会先将数据恢复到最近一次的checkpoint点,
#然后重放后续的journal日志来恢复。
journal=true
#存储引擎,有mmapv1、wiretiger、mongorocks
storageEngine=wiredTiger
#设置成全部ip可以访问,这样就可以在windows中去连虚拟机的MongoDB,
#也可以设置成某个网段或者某个ip1234567891011
bind_ip = 0.0.0.0
7.在shell中使用mongo来连接mongodb,通过mongodb相关命令来进行mongodb的管理
cd /usr/local/mongodb/bin
./mongo
MongoDB对用户的操作
-
创建用户
MongoDB创建用户必须进入到相关数据库下进行创建
>use admin
>db.createUser({
user: 'admin', // 用户名(自定义)
pwd: 'my_db1', // 密码(自定义)
roles:[{
role: 'root', // 使用超级用户角色
db: 'admin' // 指定数据库
}]})
[root@k8s-vanode1 bin]# systemctl start firewalld
[root@k8s-vanode1 bin]# firewall-cmd --add-port=27017/tcp --zone=public --permanent
success
[root@k8s-vanode1 bin]# firewall-cmd --reload
success
[root@k8s-vanode1 bin]# firewall-cmd --zone=public --list-ports
27017/tcp
[root@k8s-vanode1 bin]# systemctl stop firewalld
[root@k8s-vanode1 bin]#