centos7安装mongodb(附shell安装)

tar -xzf mongodb-linux-x86_64-rhel70-3.2.6.tgz

mv mongodb-linux-x86_64-rhel70-3.2.6  ../mongodb

cd ~/ mongodb

mkdir db

mkdir log

cd bin

vi mongodb.conf

------------------

dbpath=/home/yy/mongodb/db

logpath=/home/yy/mongodb/log/mongodb.log

#bind_ip=192.168.137.0

port=27017

fork=true

nohttpinterface=true

#启用用户名密码

auth=true

wq---------

./mongod -f mogodb.conf

设置开机启动

sudo vi /etc/ rc.local   注意/etc/rc.d/rc.local也修改一下

-----

/home/yy/mongodb/bin/mongod --config /home/yy/mongodb/bin/mongodb.conf

wq------

chmod +x rc.local    //设置启动权限

chmod +x /etc/rc.d/rc.local

测试一下

pstree | grep mongo

telnet 192.168.137.0 27017

注意不支持telnet的话 需要先安装sudo yum install telnet

如果telnet不通,需要添 firewall-cmd --add-port=27017/tcp加防火墙设置

永久性设置防火墙加入参数permanent:

firewall-cmd --permanent --add-port=27017/tcp

查看端口有没有被占用

netstat -an | grep :27017

use CacheDB

#增加用户只有读写权限

db.createUser({user: "***", pwd: "***", roles: [ "readWrite", "dbOwner" ]}) 

#登录

db.auth("***","****")

#显示当前db状态

db.stats()

#当前db版本

db.version()

#删除当前使用数据库

db.dropDatabase()

#查看当前db的链接机器地址

db.getMongo()

#简单插入数据

db.user.insert({"fname":"zhoulf", "company":"navinfo"})

#循环插入数据

for (var i = 1; i <= 10; i++) db.user.save({ "fname" : "zhoulf"+i, "company" : "navinfo"+i });

#查询数据

db.user.find()

exit

创建超级用户:

use admin

db.createUser({user:"admin", pwd:"admin", roles:["root"]})

修改密码

use admin

db.changeUserPassword("username", "xxx")

附:安装shell

install_mongodb.sh

#!/bin/bash
Soft_dir="/home/wly"
File="mongodb-linux-x86_64-3.2.6.tgz"
#Redis_Version="3.0.2"
#sql_show="show dbs"
#sql_use_admin="use admin"
#sql_creat_admin="db.createUser({user:"admin", pwd:"admin", roles:["root"]})"
#sql_use_wly="use wly"
#sql_creat_wly="db.createUser({user: "wanliyun", pwd: "wanliyun", roles: [ "readWrite", "dbOwner" ]})"
root_or_not(){
if [ `id -u` != 0 ] ; then
    echo -e "\e[1;44m  <====no root,gun====> \e[0m"
    exit 1
fi
}
install_or_not(){
    read -p "zhuang Y; buzhuang N:" INSTALL_OR_NOT
    case $INSTALL_OR_NOT in
    Y|y)
        install_redis
    ;;
    N|n)
        echo -e "\e[0;44m <====SB====> \e[0m"
        exit 1
    ;;
    *)
        echo -e "\e[1;44m  Only Input  Y or N  \e[0m"
        install_or_not
    ;;
    esac
}
check_result(){
    if [ $1 != 0 ];then
        echo -e "\e[1,44m  <==== Error,Exit install redis ====> \e[0m"
        exit 1
    fi
}
install_redis(){
[ ! -d ${Soft_dir} ] && mkdir ${Soft_dir}
cd ${Soft_dir}
check_result  $?
tar -xzf $File
echo -e "\e[1;44m  <====1====> \e[0m"
#yum  install  wget - y
check_result  $?
mv mongodb-linux-x86_64-3.2.6 ./mongodb
echo -e "\e[1;44m  <====2====> \e[0m"
#wget  $Download_URL
check_result  $?
cd mongodb
#tar xf redis-stable.tar.gz
check_result  $?
mkdir db
echo -e "\e[1;44m  <====3====> \e[0m"
#cd  redis-stable
check_result  $?
mkdir log
echo -e "\e[1;44m  <====4====> \e[0m"
#make
check_result  $?
cp /home/wly/mongodb.conf /home/wly/mongodb/bin
echo -e "\e[1;44m  <====5n====> \e[0m"
#make  install
check_result  $?
/home/wly/mongodb/bin/mongod --config /home/wly/mongodb/bin/mongodb.conf
echo -e "\e[1;44m  <====6====> \e[0m"
#cd /home/wly/mongodb/bin
/home/wly/mongodb/bin/mongo 127.0.0.1:27017/CacheDB --quiet /home/wly/test.js
echo -e "\e[1;44m  <====creat user  dicom_file====> \e[0m"
#/home/wly/mongodb/bin/mongo 127.0.0.1:27017/ris_dcmfile --quiet /home/wly/test.js
echo -e "\e[1;44m  <====creat user  ris_dcmfile====> \e[0m"
#mkdir  -p /usr/local/redis/{etc,dump,logs}
check_result  $?
echo "auth=true">>/home/wly/mongodb/bin/mongodb.conf
#echo $sql_show|/home/wly/mongodb/bin/mongo --shell
#touch /usr/local/redis/etc/redis.conf
#echo "$sql_creat_wly"|/home/wly/mongodb/bin/mongo 127.0.0.1:27017/test --shell
#db.createUser({user: "wanliyun", pwd: "wanliyun", roles: [ "readWrite", "dbOwner" ]})
echo -e "\e[1;44m  <====7====> \e[0m"
check_result  $?
echo -e "\e[1;44m <======hhhhhhhhhhhhhhhhh=======> \e[0m"
}
root_or_not
install_or_not
 

 

test.js

db.createUser({user: "***", pwd: "***", roles: [ "readWrite", "dbOwner" ]})

你可能感兴趣的:(centos7安装mongodb(附shell安装))