部署文档
作者 |
刘畅 |
时间 |
2021-09-22 |
目录
1 架构说明
操作系统Ubuntu-Server-18.04.x-LTS
主机名 |
IP |
软件 |
1-81 |
172.16.1.81 |
redis-5.0.13(主)、mongodb-3.6.23(主)、mysql-8.0.26 |
1-82 |
172.16.1.82 |
redis-5.0.13(主)、mongodb-3.6.23(从) |
1-83 |
172.16.1.83 |
redis-5.0.13(主)、mongodb-3.6.23(延迟从库) |
1-84 |
172.16.1.84 |
redis-5.0.13(从)、rabbitmq-3.8.22(集群) |
1-85 |
172.16.1.85 |
redis-5.0.13(从)、rabbitmq-3.8.22(集群) |
1-86 |
172.16.1.86 |
redis-5.0.13(从)、rabbitmq-3.8.22(集群) |
注: 此部署文档为测试环境,为了节省机器,复用了些集群,生产环境各节点机器独立
服务器优化配置
# sudo su
- root
(1)
所有节点时间保持一致
# ln
-sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#
apt-get install ntpdate -y
#
if ! crontab -l | grep "ntpdate" &>/dev/null; then
(echo
"*/5 * * * * /usr/sbin/ntpdate ntp.aliyun.com >/dev/null 2>&1";
crontab -l) | crontab
fi
(2)
使用sudo命令时加载/etc/profile中的环境变量
# echo
'alias sudo="sudo env PATH=$PATH"' >> /etc/profile
# source
/etc/profile
(3) 解决ssh连接速度慢的问题
#
cat >> /etc/ssh/sshd_config <<
EOF
GSSAPIAuthentication no
UseDNS no
EOF
#
systemctl restart sshd.service
(4) 系统内核优化
cat >> /etc/sysctl.conf <<
EOF
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range
= 1024 65000
net.ipv4.tcp_syncookies
= 1
net.ipv4.tcp_max_tw_buckets
= 20480
net.ipv4.tcp_max_syn_backlog
= 20480
net.core.netdev_max_backlog
= 262144
net.ipv4.tcp_fin_timeout
= 20
vm.swappiness = 0
EOF
#
sysctl -p
(5)
设置最打开文件描述符数量
参考文档: https://askubuntu.com/questions/1049058/how-to-increase-max-open-files-limit-on-ubuntu-18-04
# cat >> /etc/security/limits.conf <<
EOF
* soft nproc 65535
*
hard nproc 65535
*
soft nofile 65535
*
hard nofile 65535
root
soft nproc 65535
root
hard nproc 65535
root
soft nofile 65535
root hard nofile 65535
EOF
# su
-
# ulimit -n
(6) 创建lc用户
# sudo useradd lc -m -s /bin/bash
# echo "lc:123456"|sudo chpasswd
# sudo usermod -aG sudo lc
# su - lc
2 安装mysql
在172.16.1.81节点上操作
2.1 下载软件包
# wget https://mirrors.tuna.tsinghua.edu.cn/mysql/downloads/MySQL-8.0/mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
2.2 安装
1 前期准备
(1) 安装依赖包
#
apt-get install libaio1 -y
(2) 解压安装包
#
tar -xf mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
#
mv mysql-8.0.26-linux-glibc2.12-x86_64/ /opt/mysql/
(3) 创建mysql
用户
#
groupadd -g 1600 mysql
#
useradd -g mysql -u 1600 -M -s /sbin/nologin mysql
#
id mysql
uid=1600(mysql) gid=1600(mysql) 组=1600(mysql)
(4) 创建存放数据库文件的目录并赋予mysql安装目录的属主和属组都是mysql
#
mkdir -p /opt/mysql/data/
# chown -R mysql.mysql /opt/mysql/
# cat > /etc/my.cnf << EOF
[client]
port
= 3306
default-character-set
= utf8mb4
socket
= /opt/mysql/data/mysql.sock
[mysql]
no-auto-rehash
[mysqld]
user
= mysql
port
= 3306
basedir
= /opt/mysql
datadir
= /opt/mysql/data
socket
= /opt/mysql/data/mysql.sock
bind-address
= 0.0.0.0
pid-file
= /opt/mysql/data/mysqld.pid
character-set-server
= utf8mb4
collation-server
= utf8mb4_0900_ai_ci
log-error
= /opt/mysql/data/mysqld.log
slow_query_log
= ON
long_query_time
= 2
slow_query_log_file
= /opt/mysql/data/mysql-slow.log
max_connections
= 1024
open_files_limit
= 65535
innodb_buffer_pool_size
= 2G
innodb_flush_log_at_trx_commit
= 2
innodb_log_file_size
= 256M
transaction_isolation
= READ-COMMITTE
default-storage-engine
= innodb
innodb_file_per_table
= on
innodb_flush_method
= O_DIRECT
interactive_timeout
= 1800
wait_timeout
= 1800
innodb_open_files=1500
innodb_io_capacity=5000
innodb_buffer_pool_instances=4
innodb_autoextend_increment=64
innodb_purge_threads=1
innodb_purge_batch_size=150
innodb_write_io_threads=4
innodb_read_io_threads=4
innodb_concurrency_tickets=2500
explicit_defaults_for_timestamp
= 1
skip-name-resolve
lower_case_table_names
= 1
server-id
= 1
skip-log-bin
[mysqldump]
quick
max_allowed_packet = 32M
EOF
3 初始化mysql 数据库
# /opt/mysql/bin/mysqld --initialize --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data
# cat
/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL
Server 8.0.26
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
Type=forking
PIDFile=/opt/mysql/data/mysqld.pid
TimeoutSec=0
PermissionsStartOnly=true
#
ExecStartPre=/usr/local/mysql/bin/mysqld_pre_systemd
ExecStart=/opt/mysql/bin/mysqld
--defaults-file=/etc/my.cnf --daemonize --pid-file=/opt/mysql/data/mysqld.pid
$MYSQLD_OPTS
EnvironmentFile=-/etc/sysconfig/mysql
LimitNOFILE
= 65535
Restart=on-failure
RestartPreventExitStatus=1
PrivateTmp=false
# systemctl daemon-reload
#
systemctl start mysqld.service
#
systemctl enable mysqld.service
#
cat >> /etc/profile << EOF
export
PATH=/opt/mysql/bin:$PATH
EOF
# source /etc/profile
6 修改数据库密码
# grep 'temporary password'
/opt/mysql/data/mysqld.log
……[Note] [MY-010454] [Server] A temporary password is
generated for root@localhost: dHZkG>feg6+A
# mysql -uroot -p'dHZkG>feg6+A'
mysql>
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
mysql>
update mysql.user set host='127.0.0.1' where user='root';
mysql>
flush privileges;
mysql>
exit;
# mysql
-h127.0.0.1 -uroot -p'123456'
3 安装rabbitmq
172.16.1.84-86节点
3.1 安装erlang
在172.16.1.84节点上操作
1 下载erlang软件包
# wget http://erlang.org/download/otp_src_24.0.tar.gz
2 安装依赖包
# apt-get update
# apt-get
install build-essential libncurses5-dev libssl-dev -y
3 编译
# tar -xzf otp_src_24.0.tar.gz
#
cd otp_src_24.0/
# ./configure
--prefix=/opt/erlang --without-javac
# make &&
make install
验证
# /opt/erlang/bin/erl
Erlang/OTP 24 [erts-12.0] [source] [64-bit] [smp:1:1]
[ds:1:1:10] [async-threads:1] [jit]
Eshell
V12.0 (abort with ^G)
1>
10+20
.
30
2>
按两下Ctrl + c退出。
4 将编译好的erlang拷贝到172.16.1.85-86两台机器上
# scp -rp /opt/erlang
root@172.16.1.85:/opt/
# scp -rp /opt/erlang
root@172.16.1.86:/opt/
5 配置环境变量
在172.16.1.84-86节点上操作
# echo 'export
PATH=/opt/erlang/bin:$PATH' >> /etc/profile
# source /etc/profile
3.2 部署rabbitmq
在172.16.1.84节点上操作
1 下载rabbitmq软件包
# wget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.8.22/rabbitmq-server-generic-unix-3.8.22.tar.xz
2 配置rabbitmq配置文件
(1)
解压包
#
tar -xf rabbitmq-server-generic-unix-3.8.22.tar.xz
#
mv rabbitmq_server-3.8.22/ /opt/rabbitmq/
(2)
rabbitmq-server启动脚本中添加erlang环境变量,解决脚本找不到erl命令
# vim
/opt/rabbitmq/sbin/rabbitmq-server +8
export
PATH=$PATH:/opt/erlang/bin
(3) 添加rabbitmq配置文件
#
echo '[{rabbit,[{tcp_listeners,[5672]}]}].' >
/opt/rabbitmq/etc/rabbitmq/rabbitmq.config
3 将解压的rabbitmq软件包拷贝到172.16.1.85-86两台机器上
# scp -rp /opt/rabbitmq
root@172.16.1.85:/opt/
# scp -rp /opt/rabbitmq
root@172.16.1.86:/opt/
4 启动rabbitmq
分别在172.16.1.84-86节点上操作
(1)
添加环境变量
#
cat >> /etc/profile << EOF
export
RABBITMQ_HOME=/opt/rabbitmq
export
PATH=/opt/rabbitmq/sbin:$PATH
EOF
#
source /etc/profile
(2)
创建用户并赋权
#
useradd rabbitmq
# mkdir
-p /home/rabbitmq/
#
chown -R rabbitmq.rabbitmq /home/rabbitmq/
#
chown -R rabbitmq.rabbitmq /opt/rabbitmq/
(3)
加入systemd管理
# cat rabbitmq-server.service
[Unit]
Description=RabbitMQ
broker
After=syslog.target
network.target
[Service]
Type=simple
User=rabbitmq
Group=rabbitmq
#
UMask=0027
NotifyAccess=all
TimeoutStartSec=600
SuccessExitStatus=127
#
To override LimitNOFILE, create the following file:
#
#
/etc/systemd/system/rabbitmq-server.service.d/limits.conf
#
#
with the following content:
#
#
[Service]
#
LimitNOFILE=65536
LimitNOFILE=32768
#
Note: systemd on CentOS 7 complains about in-line comments,
#
so only append them here
#
#
Restart:
#
The following setting will automatically restart RabbitMQ
#
in the event of a failure. systemd service restarts are not a
#
replacement for service monitoring. Please see
#
https://www.rabbitmq.com/monitoring.html
Restart=on-failure
RestartSec=10
WorkingDirectory=/opt/rabbitmq
ExecStart=/opt/rabbitmq/sbin/rabbitmq-server
ExecStop=/opt/rabbitmq/sbin/rabbitmqctl
shutdown
#
See rabbitmq/rabbitmq-server-release#51
SuccessExitStatus=69
Restart=on-failure
[Install]
WantedBy=multi-user.target
(4)
启动
#
systemctl daemon-reload
#
systemctl start rabbitmq-server.service
#
systemctl enable rabbitmq-server.service
(5)
开启rabbitmq web管理界面
# /opt/rabbitmq/sbin/rabbitmq-plugins
enable rabbitmq_management
#
systemctl restart rabbitmq-server.service
(6)
验证
可以通过端口进行访问,出现了rabbitmq UI界面,说明rabbitmq搭建成功。
http://172.16.1.84-86:15672
5 rabbitmq集群配置
分别在172.16.1.84-86节点上操作
(1)
修改hosts解析
#
cat >>
/etc/hosts << EOF
172.16.1.84
1-84
172.16.1.85
1-85
172.16.1.86
1-86
EOF
(2)
停止RabbitMQ服务
#
systemctl stop rabbitmq-server.service
(3)
设置Erlang Cookie
设置不同节点间同一认证的Erlang Cookie,这里将1-84的Cookie传给1-85、1-86两台服务器
在172.16.1.84节点上操作
# scp
-p /home/rabbitmq/.erlang.cookie
root@1-85:/home/rabbitmq/
# scp
-p /home/rabbitmq/.erlang.cookie root@1-86:/home/rabbitmq/
注:
cookie在所有节点上必须完全一样,且权限必须为400,属主和属组为rabbitmq,同步时一定要注意。erlang是
通过主机名来连接服务,必须保证各个主机名之间可以ping通。可以通过编辑/etc/hosts来手工添加主机名和IP
对应关系。如果主机名ping不通,rabbitmq服务启动会失败。
(4)
设置好cookie后重启rabbitmq
#
systemctl restart rabbitmq-server.service
(5)
查看单节点的集群状态(任意一台都可,这里以172.16.1.84节点为例)
#
cp -a /home/rabbitmq/.erlang.cookie ./
#
chown root.root /root/.erlang.cookie
# rabbitmqctl cluster_status
注: 因为.erlang.cookie在/home/rabbitmq/目录下,要查看rabbitmq集群状态时要使用下面的命令拷贝
.erlang.cookie到当前用户的家目录下,并更改属主属组为当前用户。
#
cp -a
/home/rabbitmq/.erlang.cookie <当前用户的家目录>
# chown -R
<当前用户名>:<当前用户名> <当前用户的家目录>/.erlang.cookie
(6)
加入集群
这里将1-85和1-86加入到1-84当中,按照顺序执行先1-85再1-86
1) 在1-85上操作
#
rabbitmqctl stop_app
#
rabbitmqctl join_cluster rabbit@1-84
#
rabbitmqctl start_app
2) 在1-86上操作
#
rabbitmqctl stop_app
#
rabbitmqctl join_cluster rabbit@1-84
#
rabbitmqctl start_app
(7)
查看集群状态,任意一个节点有可以,这里以1-84为例子
#
rabbitmqctl cluster_status
注: 集群状态正常
(8)
创建用户并授权(在任意节点都可)
# 创建用户
rabbitmqctl
add_user admin '123456'
# 添加用户[管理员]标签
rabbitmqctl
set_user_tags admin administrator
# 为用户授权可以访问的[virtual /]和操作类型[配置、读、写]
rabbitmqctl
set_permissions -p / admin '.*' '.*' '.*'
#
查看所有用户
#
rabbitmqctl list_users
(9)
在UI界面中查看集群状态
http://<RabbitmqIP>:15672/
4 安装redis
172.16.1.81-86节点
4.1 下载软件包
# wget https://download.redis.io/releases/redis-5.0.13.tar.gz
4.2 编译部署
在172.16.1.81节点上操作
1 安装依赖包
# apt-get update
# apt-get install build-essential libncurses5-dev
libssl-dev -y
2 编译
# tar -xzf redis-5.0.13.tar.gz
#
cd redis-5.0.13/
# make
#
make install PREFIX=/opt/redis
# mkdir -p
/opt/redis/{cfg,logs,data}
# cp -a /root/redis-5.0.13/redis.conf
/opt/redis/cfg/
3 修改配置文件
# cat > /opt/redis/cfg/redis.conf <<
EOF
bind 172.16.1.81
protected-mode
yes
port
6379
tcp-backlog
511
timeout
0
tcp-keepalive
300
daemonize
yes
supervised
no
pidfile
/opt/redis/cfg/redis_6379.pid
loglevel
notice
logfile
/opt/redis/logs/redis_6379.log
databases
16
always-show-logo
yes
stop-writes-on-bgsave-error
yes
rdbcompression
yes
rdbchecksum
yes
dbfilename
dump.rdb
dir
/opt/redis/data
masterauth
123456
replica-serve-stale-data
yes
replica-read-only
no
repl-diskless-sync
no
repl-diskless-sync-delay
5
repl-disable-tcp-nodelay
no
replica-priority
100
requirepass
123456
maxclients
10000
lazyfree-lazy-eviction
no
lazyfree-lazy-expire
no
lazyfree-lazy-server-del
no
replica-lazy-flush
no
appendonly
yes
appendfilename
"appendonly.aof"
appendfsync
everysec
no-appendfsync-on-rewrite
no
auto-aof-rewrite-percentage
100
auto-aof-rewrite-min-size
64mb
aof-load-truncated
yes
aof-use-rdb-preamble
yes
lua-time-limit
5000
cluster-enabled
yes
cluster-config-file
/opt/redis/cfg/nodes.conf
cluster-node-timeout
15000
slowlog-log-slower-than
10000
slowlog-max-len
128
latency-monitor-threshold
0
notify-keyspace-events
""
hash-max-ziplist-entries
512
hash-max-ziplist-value
64
list-max-ziplist-size
-2
list-compress-depth
0
set-max-intset-entries
512
zset-max-ziplist-entries
128
zset-max-ziplist-value
64
hll-sparse-max-bytes
3000
stream-node-max-bytes
4096
stream-node-max-entries
100
activerehashing
yes
client-output-buffer-limit
normal 0 0 0
client-output-buffer-limit
replica 256mb 64mb 60
client-output-buffer-limit
pubsub 32mb 8mb 60
hz
10
dynamic-hz
yes
aof-rewrite-incremental-fsync
yes
rdb-save-incremental-fsync yes
EOF
4 将redis加入systemd管理
# cat > /lib/systemd/system/redis.service <<
EOF
[Unit]
Description=Redis
persistent key-value database
After=network.target
After=network-online.target
Wants=network-online.target
[Service]
Type=forking
ExecStart=/opt/redis/bin/redis-server
/opt/redis/cfg/redis.conf
#
ExecStop=/usr/libexec/redis-shutdown
User=redis
Group=redis
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
5 将redis软件包拷贝到172.16.1.82-86节点上
# scp -rp
/opt/redis
root@172.16.1.82:/opt/
# scp -rp
/opt/redis
root@172.16.1.83:/opt/
# scp -rp
/opt/redis
root@172.16.1.84:/opt/
# scp -rp
/opt/redis
root@172.16.1.85:/opt/
# scp -rp
/opt/redis
root@172.16.1.86:/opt/
#
scp -p /lib/systemd/system/redis.service
root@172.16.1.82:/lib/systemd/system/
#
scp -p /lib/systemd/system/redis.service
root@172.16.1.83:/lib/systemd/system/
#
scp -p /lib/systemd/system/redis.service
root@172.16.1.84:/lib/systemd/system/
#
scp -p /lib/systemd/system/redis.service
root@172.16.1.85:/lib/systemd/system/
# scp -p /lib/systemd/system/redis.service
root@172.16.1.86:/lib/systemd/system/
4.3 启动redis
分别在172.16.1.81-86节点上操作
1 安装集群所需要的依赖包
# apt-get
update
#
apt-get install ruby rubygems -y
# gem install
redis
2 将redis命令加入环境变量
# echo 'export PATH=/opt/redis/bin:$PATH' >>
/etc/profile
# source /etc/profile
3 修改redis绑定的ip地址为当前的ip地址
# vim /opt/redis/cfg/redis.conf
bind
172.16.1.81 # 此配置修改为当前的主机的ip地址
4 启动redis服务
# useradd
redis
# chown
-R redis:redis /opt/redis/
#
systemctl daemon-reload
#
systemctl restart
redis
#
systemctl enable redis
# systemctl status redis
5 查看redis启动日志
(1)
警告项解决办法
#
cat /opt/redis/logs/redis_6379.log
……
看到依次从上到下有三个错误。
解决办法如下
#
cat >> /etc/sysctl.conf << EOF
net.core.somaxconn
= 20480
vm.overcommit_memory
= 1
EOF
#
sysctl -p
#
echo never > /sys/kernel/mm/transparent_hugepage/enabled
# 加入开机自启,但是Ubuntu已经弃用了/etc/rc.local配置文件,按照如下方法自己添加
#
cat >> /lib/systemd/system/rc-local.service << EOF
[Install]
WantedBy=multi-user.target
Alias=rc-local.service
EOF
#
systemctl daemon-reload
#
systemctl enable rc-local
#
cat > /etc/rc.local << EOF
#!/bin/bash
echo
never > /sys/kernel/mm/transparent_hugepage/enabled
EOF
#
chmod +x /etc/rc.local
(2)
重启redis
#
systemctl restart redis
(3)
再次查看日志
#
cat /opt/redis/logs/redis_6379.log
redis警告解决
6 创建集群
在172.16.1.81节点上操作
# redis-cli -a 123456 --cluster create
172.16.1.81:6379
172.16.1.82:6379
172.16.1.83:6379
172.16.1.84:6379
172.16.1.85:6379
172.16.1.86:6379
--cluster-replicas
1
输入yes,然后回车,集群创建完毕。创建完成后默认前三个节点81-83为主节点,84-86为
从节点。
7 查看集群信息(访问任意一个节点都可以)
# redis-cli -h 172.16.1.81 -a 123456 -c -p
6379
172.16.1.81:6379>
cluster info
172.16.1.81:6379> cluster nodes
5 安装mongodb
172.16.1.81-83节点
5.1 下载软件包
# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.6.23.tgz
5.2 安装
在172.16.1.81节点上操作
1 解压包
# tar -xzf
mongodb-linux-x86_64-3.6.23.tgz
#
mv mongodb-linux-x86_64-3.6.23/ /opt/mongodb/
#
mkdir -p /opt/mongodb/{data,conf,log}
# touch /opt/mongodb/log/mongodb.log
2 修改配置文件
# cat > /opt/mongodb/conf/mongodb.conf <<
EOF
dbpath=/opt/mongodb/data
#数据库文件位置
bind_ip=172.16.1.81
#绑定地址,默认127.0.0.1,只能通过本地连接。
port=27017
#端口,默认27017,MongoDB的默认服务TCP端口,监听客户端连接。
journal=true
#启用日志文件,默认启用。
logpath=/opt/mongodb/log/mongodb.log
#日志文件位置,该日志文件必须存在,否则会报错
logappend=true
#以追加方式写入日志。
quiet=true
#这个选项可以过滤掉一些无用的日志信息,若需要调试使用请设置为false。
fork=true
#以守护进程方式运行
EOF
3 将mongodb加入systemd管理
# cat > /lib/systemd/system/mongodb.service <<
EOF
[Unit]
Description=mongodb
3.6.23
After=network.target
[Service]
Type=forking
#PIDFile=/opt/mongodb/mongodb.pid
ExecStart=/opt/mongodb/bin/mongod
-f /opt/mongodb/conf/mongodb.conf
ExecReload=/bin/kill
-s HUP $MAINPID
#ExecStop=/bin/kill
-s QUIT $MAINPID
PrivateTmp=true
User=mongodb
Group=mongodb
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
4 分别拷贝安装包到另外两个节点
# scp -rp
/opt/mongodb root@172.16.1.82:/opt/
# scp -rp
/opt/mongodb root@172.16.1.83:/opt/
#
scp -p /lib/systemd/system/mongodb.service
root@172.16.1.82:/lib/systemd/system/
# scp -p /lib/systemd/system/mongodb.service
root@172.16.1.83:/lib/systemd/system/
5 启动mongodb
在172.16.1.81-83节点上操作
(1)
添加环境变量
#
echo 'export
PATH=/opt/mongodb/bin:$PATH' >> /etc/profile
#
source /etc/profile
(2)
授权
# useradd
mongodb
# chown
-R mongodb:mongodb
/opt/mongodb/
(3)
修改mongodb绑定的ip地址为本地ip地址
# vim /opt/mongodb/conf/mongodb.conf
bind_ip=172.16.1.81 # 指定各自服务的的ip地址
(4)
启动
#
systemctl daemon-reload
#
systemctl restart
mongodb.service
#
systemctl enable mongodb.service
# systemctl status mongodb.service
6 开启mongodb认证
在172.16.1.81-83节点上操作
(1) 创建管理用户root为root权限
#
mongo --host 172.16.1.81 --port 27017
>
use admin
>
db.createUser({user: "root",pwd: "123456",roles: [ { role: "root", db: "admin" }
]})
> exit
(2)
开启认证
#
cat >> /opt/mongodb/conf/mongodb.conf <<
EOF
#开启认证
auth = true
EOF
(3)
认证登录
#
systemctl restart mongodb.service
# mongo --host 172.16.1.81 --port 27017 -uroot
-p'123456' admin
5.3 主库操作
在172.16.1.81节操作
1 修改配置文件
# cat >> /opt/mongodb/conf/mongodb.conf <<
EOF
############主从复制配置####################################
# master节点
master
= true
#集群的私钥的完整路径,只对于Replica Set架构有效,
#auth = true 配置此项。
keyFile
= /opt/mongodb/conf/mongodb-keyfile
#主节点设置oplog的大小,主节点操作记录存储到local的oplog中,用于从库拉取。
#单位mb,新的操作会覆盖旧的操作。
oplogSize = 40960
EOF
2 创建集群私钥并发送到另外两个节点
# openssl rand -base64 741 > /opt/mongodb/conf/mongodb-keyfile
#
chmod 600 /opt/mongodb/conf/mongodb-keyfile
# chown
mongodb:mongodb
/opt/mongodb/conf/mongodb-keyfile
#
scp
-p /opt/mongodb/conf/mongodb-keyfile
root@172.16.1.82:/opt/mongodb/conf/
#
scp
-p /opt/mongodb/conf/mongodb-keyfile
root@172.16.1.83:/opt/mongodb/conf/
#
ssh -p 22 root@172.16.1.82 "chown mongodb:mongodb
/opt/mongodb/conf/mongodb-keyfile"
#
ssh -p 22 root@172.16.1.83 "chown mongodb:mongodb
/opt/mongodb/conf/mongodb-keyfile"
3 重启mongodb
# systemctl restart mongodb.service
5.4 从库操作
在172.16.1.82节点上操作
1 修改配置文件
# cat >> /opt/mongodb/conf/mongodb.conf <<
EOF
##################从库配置###########################
#开启从服务器
slave
= true
# 主数据库端口
source
= 172.16.1.81:27017
#集群的私钥的完整路径,只对于Replica Set架构有效,
#auth = true 配置此项。
keyFile
= /opt/mongodb/conf/mongodb-keyfile
#延迟复制,单位为秒
#slavedelay
= 1800
#当发现从服务器的数据不是最新时,向主服务器请求同步数据
autoresync = true
EOF
2 重启mongodb
# systemctl restart mongodb.service
# systemctl
status mongodb.service
#
mongo --host 172.16.1.82 --port 27017 -uroot -p'123456' admin
>
rs.secondaryOk()
> show dbs
5.5 延迟从库操作
在172.16.1.83节点上操作
1 修改配置文件
# cat >> /opt/mongodb/conf/mongodb.conf <<
EOF
##################从库配置###########################
#开启从服务器
slave
= true
# 主数据库端口
source
= 172.16.1.81:27017
#集群的私钥的完整路径,只对于Replica Set架构有效,
#auth = true 配置此项。
keyFile
= /opt/mongodb/conf/mongodb-keyfile
#延迟复制,单位为秒
slavedelay
= 1800
#当发现从服务器的数据不是最新时,向主服务器请求同步数据
autoresync = true
EOF
2 重启mongodb
# systemctl restart mongodb.service
# systemctl
status mongodb.service
#
mongo --host 172.16.1.83 --port 27017 -uroot -p'123456' admin
>
rs.secondaryOk()
> show dbs
5.6 测试
1 在172.16.1.81主库上创建测试库和数据
# mongo --host 172.16.1.81 --port 27017 -uroot
-p'123456' admin
>
use master_slave_test
>
function add(){var i =
0;for(;i<7;i++){db.persons.insert({"name":"master_slave_test"+i})}}
>
add()
> db.persons.find()
2 在172.16.1.82从库上查看
# mongo --host 172.16.1.82 --port 27017 -uroot
-p'123456' admin
>
use master_slave_test
>
rs.secondaryOk()
> db.persons.find()
3 同172.16.1.82节点操作步骤相同,30分钟后在172.16.1.83延迟从库上查看test数据也同步过来了。