系统设置
# 禁用打内存页面
echo "never" > /sys/kernel/mm/transparent_hugepage/enabled
echo "never" > /sys/kernel/mm/transparent_hugepage/defrag
软件部署
cd /usr/local/src
wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel62-4.1.11.tgz
tar zxvf mongodb-linux-x86_64-rhel62-4.1.11.tgz
mv mongodb-linux-x86_64-rhel62-4.1.11 /usr/local/mongodb
环境变量
echo -e ‘#Setting Mongodb
export Mongodb_HOME="/usr/local/mongodb/bin"
export PATH=$REDIS_HOME:$PATH‘ >> /etc/profile
source /etc/profile
配置文件
mkdir -p /usr/local/mongodb/{etc,data,logs}
echo -e "dbpath=/usr/local/mongodb/data
logpath=/usr/local/mongodb/logs/mongodb.log
bind_ip=0.0.0.0
port=27017
logappend=true
fork=true">> /usr/local/mongodb/etc/mongodb.conf
systemd服务
echo -e ‘[Unit]
Description=mongodb service add by liuchao
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
User=mongodba
Group=mongodba
ExecStart=/usr/local/mongodb/bin/mongod -f /usr/local/mongodb/etc/mongodb.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target‘>>/usr/lib/systemd/system/mongodb.service
systemctl daemon-reload
systemctl enable mongodb
systemctl start mongodb
启用认证
1.根据上面设置是无密码登录,我们登录陆mongo里面,设置管理员信息
> use admin
> db.createUser({
user:"admin",
pwd:"admin",
roles:[{role:"userAdminAnyDatabase", db:"admin"}]
})
2.修改配置文件启用认证,在配置文件增加如下参数
echo auth=true >> /opt/mongodb/etc/mongodb.conf
systemctl restart mongodb
3.在次登录mongo数据,为数据库创建用户
# 设置test1用的密码为test1,对test1数据库有读写权限
db.auth(‘admin‘,‘admin‘)
use test1
db.createUser({user:‘test1‘,pwd:‘123.com‘,roles:[{role:‘readWrite‘,db:‘mydb‘}]})
- 一定要切换到对应的数据库去创建用户, 否则在进行认证的时候, 会提示找不到用户
登录测试
mongo -u test1 -p ‘123.com‘ 192.168.100.102:27017/mydb
> db.createCollection("Hello")
> show collections
> db.Hello.drop()
用户管理
#修改用户密码,要修改哪个数据的,要先切换到那个数据库
use test001;
db.changeUserPassword("mike","mikepwd");
#删除用户,要先切换到那个数据库
use test001
db.dropUser(‘mike‘)