系統環境
Redhat 7
01.修改hosts文件
[mongodb@ty1 apps]$ cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.65.130 ty1
02.安裝相關包
yum install -y cyrus-sasl cyrus-sasl-plain cyrus-sasl-gssapi krb5-libs lm_sensors-libs net-snmp-agent-libs net-snmp openssl openssl-devel rpm-libs tcp_wrappers-libs
03.關閉防火牆與安全認證
systemctl disable firewalld
vi /etc/selinux/config
SELINUX=disabled
04.優化內核參數
echo "vm.zone_relaim_mode=0" >> /etc/sysctl.conf
sysctl -p
禁止Transparent Huge Pages(THP)
服務器重啓後立即生效辦法:
vi /etc/rc.local
添加以下內容:
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
echo never > /sys/kernel/mm/transparent_hugepage/enabled
fi
if test -f /sys/kernel/mm/transparent_hugepage/defrag; then
echo never > /sys/kernel/mm/transparent_hugepage/defrag
fi
05.創建mongodb目錄與用戶權限
[root@ty1 soft]# mkdir -p /mongodb
[root@ty1 soft]#
[root@ty1 soft]# mkdir -p /mongodb/{data,logs,apps}
[root@ty1 soft]# /usr/sbin/groupadd -g 10001 mongodb
[root@ty1 soft]# /usr/sbin/useradd -u 10001 -g mongodb mongodb
[root@ty1 soft]# id mongodb
uid=10001(mongodb) gid=10001(mongodb) groups=10001(mongodb)
[root@ty1 soft]# passwd mongodb
Changing password for user mongodb.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.
[root@ty1 soft]# chown -R mongodb:mongodb /mongodb
[root@ty1 soft]# chown -R 775 /mongodb
06.優化lunux讀取性能,不使用atime屬性
vi /etc/fstab
在mongodb的掛載點,那一行,default後面加上atime即可,如 default,noatime
然後卸載掛載點,重新掛載
掛載後然後輸入mount命令,然後看輸出結果中是否有noatime即可
07.修改最大連接數
vi /etc/security/limits.conf
mongodb hard nofile 25600
mongodb soft nofile 25600
mongodb hard nproc 25600
mongodb soft nproc 25600
08.下載安裝mongodb
https://www.mongodb.com/try/download/community
su -mongodb
cd /mongodb/apps
tar zxvf mongodb-linux-x86_64-rhel70-4.2.8.tgz
ln -s /mongodb/apps/mongodb-linux-x86_64-rhel70-4.2.8 /mongodb/apps/mongodb
vi ~/.bash_profile
PATH=$PATH:$HOME/.local/bin:/mongodb/apps/mongodb/bin:$HOME/bin
source ~/.bash_profile
驗證環境變量是否生效
which mongod
09.編輯配置文件
vi /mongodb/apps/mongodb/bin/mongodb.conf
dbpath=/mongodb/data
logpath=/mongodb/logs/mongodb.log
port=27017
fork=true
auth=true
#noauth=true
#verbose=true
#vvvv=true
journal=true
maxConns=500
logappend=true
directoryperdb=true
#pidfilepath=/var/run/mongo.pid
#cpu=true
#nohttpinterface=false
#notablescan=false
#profile=0
#slowms=200
#quiet=true
#syncdelay=60
bind_ip=127.0.0.1,192.168.65.130
10.啓動
[mongodb@ty1 apps]$ mongod -f /mongodb/apps/mongodb/bin/mongodb.conf
about to fork child process, waiting until server is ready for connections.
forked process: 57768
child process started successfully, parent exiting
[mongodb@ty1 apps]$ ps -ef|grep mongo
root 57447 2539 0 09:13 pts/1 00:00:00 su - mongodb
mongodb 57448 57447 0 09:13 pts/1 00:00:00 -bash
mongodb 57768 1 23 09:24 ? 00:00:02 mongod -f /mongodb/apps/mongdb/bin/mongodb.conf
mongodb 57804 57448 0 09:24 pts/1 00:00:00 ps -ef
mongodb 57805 57448 0 09:24 pts/1 00:00:00 grep --color=auto mongo
or
mongod --config /mongodb/apps/mongodb/bin/mongodb.conf
11.連接方式
1.mongo 默認連接的127.0.0.1
2.mongo -part 27017
3.mongo 192.168.65.130:27017
12.停止進程
kill -2 `ps -ef|grep mongod|grep -v grep |awk '{print $2}'`
or
kill -2 3333
or
mongo -port 27017
use admin
db.shutdownServer();
如果提示沒權限,則需要創建管理員用戶,然後重新嘗試
通過 db.shutdownServer() 關閉服務時需要使用本地連接纔行,進去後要先use admin,然後認證用戶後再執行。
認證用戶:db.auth('username','pwd')
13.其他命令
1.創建用戶命令:
db.createUser(
{
user:"root",
pwd:"123456",
roles:[{role:"root",db:"admin"}]
}
);
2.關閉服務
進去後先use admin
db.shutdownServer()
3.刪除用戶
db.dropUser('ty')
4.看運行狀態
db.runCommand({"serverStatus":1})
更多命令可以通過db.help()來查看