您的位置:首页 > 数据库 > Redis

一文学会部署zookpeeper/redis/mongo/mysql 主从/集群

2018-07-11 17:56 681 查看

部署环境

阿里云 centos 7.4

对应软件版本

zookpeeper 3.4.12
redis 4.0.10
mongo 3.4.6
mysql 5.7.20

验证时间 2018.7.5

如果出现url无法下载的情况 重新找个下载地址就好了

准备工作

准备3台服务器

修改每台的hosts 我这里分别设置3台服务器的内网ip为域名test1,test2,test3 方便后面操作

vi /etc/hosts

为了省事没有建立对应的用户组和权限 生产环境记得加上

zookeeper 集群

部署方式

部署3个节点分别为 test1,test2,test3

安装依赖

yum install java-1.8.0-openjdk

安装zookeeper

wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.12/zookeeper-3.4.12.tar.gz tar -zxvf zookeeper-3.4.12.tar.gz
mv  zookeeper-3.4.12  /opt
cd /opt/zookeeper-3.4.12/conf
cp zoo_sample.cfg  zoo.cfg
#修改配置zoo.cfg  添加内容 记得修改hosts绑定ip
server.1=test1:2881:3881
server.2=test2:2882:3882
server.3=test3:2883:3883

# 其他节点跟重复以上步骤

# 在3台不同的机器上分别写入
mkdir /tmp/zookeeper
touch /tmp/zookeeper/myid
echo 1 > /tmp/zookeeper/myid
echo 2 > /tmp/zookeeper/myid
echo 3 > /tmp/zookeeper/myid

启动就好了

sh /opt/zookeeper-3.4.12/bin/zkServer.sh start

没有啥报错的话就说明已经搭建成功了 连上客户端试试吧

zk相关配置

tickTime:zookeeper中使用的基本时间单位, 毫秒值。
initLimit:这个配置项是用来配置 Zookeeper 接受follower 初始化连接时最长能忍受多少个 tickTime 时间间隔数。
syncLimit:这个配置标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度
dataDir和dataLogDir 不解释
clientPort:监听client连接的端口号,这里说的client就是连接到Zookeeper的代码程序。
server.{myid}={ip}:{leader服务器交换信息的端口}:{当leader服务器挂了后, 选举leader的端口}
maxClientCnxns:对于一个客户端的连接数限制,默认是60

redis

部署方式

部署3个分片分别为test1,test2,test3

安装依赖

yum install ruby
yum install rubygems
gem install redis

# ruby 的版本可能过低 需要升级一下
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh
rvm list known
rvm install 2.4.1

安装redis集群

wget http://download.redis.io/releases/redis-4.0.10.tar.gz 
tar -zxvf  redis-4.0.10.tar.gz
cd redis-4.0.10
make
mv redis-4.0.10 /opt

修改配置文件

port 6379
bind  [ip] #必须bind当前服务器ip 域名支持不好会报错
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

启动redis

./redis-server [conf-path]  &

启动集群

# 如果要添加复制节点添加 --replicas [复制数量] 以此类推
./redis-trib.rb create test1:6379 test2:6379 test3:6379

没有啥报错的话就说明已经搭建成功了 连上客户端试试吧

redis集群配置

cluster-enabled <yes/no>: 如果是yes,表示启用集群,否则以单例模式启动
cluster-config-file <filename>: 可选,这不是一个用户可编辑的配置文件,这个文件是Redis集群节点自动持久化每次配置的改变,为了在启动的时候重新读取它。
cluster-node-timeout <milliseconds>: 超时时间,集群节点不可用的最大时间。如果一个master节点不可到达超过了指定时间,则认为它失败了。注意,每一个在指定时间内不能到达大多数master节点的节点将停止接受查询请求。
cluster-slave-validity-factor <factor>: 如果设置为0,则一个slave将总是尝试故障转移一个master。如果设置为一个正数,那么最大失去连接的时间是node timeout乘以这个factor。
cluster-migration-barrier <count>: 一个master和slave保持连接的最小数量(即:最少与多少个slave保持连接),也就是说至少与其它多少slave保持连接的slave才有资格成为master。
cluster-require-full-coverage <yes/no>: 如果设置为yes,这也是默认值,如果key space没有达到百分之多少时停止接受写请求。如果设置为no,将仍然接受查询请求,即使它只是请求部分key

mongo

mongo相关服务 服务器划分

部署3个conf 2个conf 和shard1 shard2 2个分片每个分片有2个复制集

test1  test2  test3
conf   conf    conf
mongos mongos share2
share1 share2 share1

# 端口分配
mongos:20000
config:21000
shard1:27001
shard2:27002

修改一下环境变量 运行命令方便

vi /etc/profile
export MONGODB_HOME=/opt/mongodb-linux-x86_64-3.4.6
export PATH=$MONGODB_HOME/bin:$PATH
source /etc/profile

部署前准备

curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.6.tgz 
tar -zxvf mongodb-linux-x86_64-3.4.6.tgz

mv mongodb-linux-x86_64-3.4.6 /opt

#配置好目录

mkdir /tmp/mongo/log
mkdir /tmp/mongo/data/conf
mkdir /tmp/mongo/data/shard1
mkdir /tmp/mongo/data/shard2
mkdir /tmp/mongo/data/mongs

confg

# 创建配置文件 /opt/mongodb-linux-x86_64-3.4.6/conf/config.conf

systemLog:
destination: file
logAppend: true
path: /tmp/mongo/log/config.log

# Where and how to store data.
storage:
dbPath: /tmp/mongo/data/conf/
journal:
enabled: true
# how the process runs
processManagement:
fork: true
pidFilePath: /tmp/mongo/data/configsrv.pid

# network interfaces
net:
port: 21000
bindIp: test1  #修改为当前节点的ip

#operationProfilinw:
replication:
replSetName: config

sharding:
clusterRole: configsvr

运行conf 到3个不同节点

mongod --config /opt/mongodb-linux-x86_64-3.4.6/conf/config.conf

#初始化配置

mongo test1:21000
#config变量
config = {
_id : "config",
members : [
{_id : 0, host : "test1:21000" },
{_id : 1, host : "test2:21000" },
{_id : 2, host : "test3:21000" }
]
}

#初始化副本集
rs.initiate(config)
#查看分区状态
rs.status();

部署shard1

# 创建配置文件 /opt/mongodb-linux-x86_64-3.4.6/conf/shard1.conf

systemLog:
destination: file
logAppend: true
path: /tmp/mongo/log/shard1.log #log路径

# Where and how to store data.
storage:
dbPath: /tmp/mongo/data/shard1  #路径
journal:
enabled: true
wiredTiger:
engineConfig:
cacheSizeGB: 20

# how the process runs
processManagement:
fork: true
pidFilePath: /tmp/mongo/data/shard1.pid  #pid名称

# network interfaces
net:
port: 27001  # 不同分片相同主机的话记得改端口号
bindIp: test1 #ip不同要修改

#operationProfiling:
replication:
replSetName: shard1  #不同分片改下名字
sharding:
clusterRole: shardsvr

mongod  --config  /opt/mongodb-linux-x86_64-3.4.6/conf/shard1.conf

# init
mongo test1:27001
#使用admin数据库
use admin
#定义副本集配置
config = {
_id : "shard1",
members : [
{_id : 0, host : "test1:27001" },
{_id : 1, host : "test3:27001" }
]

}

#初始化副本集配置
rs.initiate(config);

#查看分区状态
rs.status();

部署share2

同上修改一下对应的配置 需要修改的地方都有注释了

mongod  --config  /opt/mongodb-linux-x86_64-3.4.6/conf/shard2.conf

部署mongos

systemLog:
destination: file
logAppend: true
path: /tmp/mongo/log/mongos.log
processManagement:
fork: true
#  pidFilePath: /tmp/mongo/data/mongos.pid

# network interfaces
net:
port: 20000
bindIp: test1
#监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
sharding:
configDB: config/test1:21000,test2:21000,test3:21000

#启动
mongos  --config  /opt/mongodb-linux-x86_64-3.4.6/conf/mongos.conf

mongo test1:20000
#使用admin数据库
use  admin
#串联路由服务器与分配副本集
sh.addShard("shard1/test1:27001,test3:27001")
sh.addShard("shard2/test2:27002,test3:27002")
#查看集群状态
sh.status()

没有啥报错的话就说明已经搭建成功了 连上客户端试试吧

mysql主从

部署方式

test1 主节点
test2 从节点

安装mysql

#使用yum方式最简单
wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm rpm -ivh mysql57-community-release-el7-8.noarch.rpm
yum -y install mysql-server

service mysqld restart

#查看初始化密码
grep "password" /var/log/mysqld.log

mysql -u root -p   密码

#然后需要重新设置密码 不然用不了

alter user 'root'@'localhost' identified by '[你的密码]';
flush privileges ;

配置主从复制

#修改配置文件 /etc/my.conf

[mysqld]
log-bin=mysql-bin  #开启binlog
server-id=1  #不同服务器要设置不一样

创建用户

复制用到的用户名和密码会以纯文本格式存储在主信息存储库文件或表中
所以创建个只要复制权限的账号非常重要

mysql -uroot -p [设置的密码]

CREATE USER 'replication'@'test1' IDENTIFIED BY 'mima';

GRANT REPLICATION SLAVE ON *.* TO 'replication'@'test1';

关联master

#在从节点登陆并执行

CHANGE MASTER TO MASTER_HOST='test1', MASTER_USER='replication', MASTER_PASSWORD='mima', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=0;

#开始复制
START SLAVE;

查看状态

show slave status\G

Slave_IO_State #从站的当前状态
Slave_IO_Running: Yes #读取主程序二进制日志的I/O线程是否正在运行
Slave_SQL_Running: Yes #执行读取主服务器中二进制日志事件的SQL线程是否正在运行。与I/O线程一样
Seconds_Behind_Master #是否为0,0就是已经同步了

如果状态不是yes 可能原因有以下 4 个方面:

1、网络不通
2、密码不对
3、MASTER_LOG_POS 不对 ps
4、mysql 的 auto.cnf server-uuid 一样
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息