安装JDK
1. 在Linux操作系统目录下创建soft目录
[root@VM-24-10-centos /]# mkdir soft
2. 删除系统默认的JDK(如果有的话)
查找已经安装的JDK
yum list installed | grep jdk
删除安装的JDK
yum remove JDK的名称
3. 把下载好的JDK通过命令或文件上传工具上传到Linux系统的soft目录下,这里使用的Cyberduck工具
4. 解压上传的jdk-8u161-linux-x64.tar.gz文件
tar -zxvf jdk-8u161-linux-x64.tar.gz
5. 将解压的JDK拷贝到/usr/local目录下,并重新命名为jdk1.8
mv /soft/jdk1.8.0_161 /usr/local/jdk1.8
6. 配置环境变量JAVA_HOME
vim /etc/profile
配置内容如下:
JAVE_HOME=/usr/local/jdk1.8
PATH=$JAVE_HOME/bin:$PATH
export JAVE_HOME PATH
重新加载文件,使刚刚配置的内容生效
source /etc/profile
7. 验证是否生效
java -version
安装zookeeper
安装
下载地址https://zookeeper.apache.org/releases.html
下载最新稳定版3.6.3二进制版就可以了
将下载后的apache-zookeeper-3.6.3-bin.tar.gz上传到Linux服务器上,这里使用Cyberduck工具上传
解压
tar -zxvf apache-zookeeper-3.6.3-bin.tar.gz
移动到/usr/local/目录下
mv apache-zookeeper-3.6.3-bin /usr/local/
cd到如下目录
cd /usr/local/apache-zookeeper-3.6.3-bin/bin
启动Server
./zkServer.sh
报如下错误
[root@VM-24-10-centos bin]# ./zkServer
-bash: ./zkServer: 没有那个文件或目录
[root@VM-24-10-centos bin]# ./zkServer.sh
/usr/local/jdk1.8/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/apache-zookeeper-3.6.3-bin/bin/../conf/zoo.cfg
grep: /usr/local/apache-zookeeper-3.6.3-bin/bin/../conf/zoo.cfg: 没有那个文件或目录
grep: /usr/local/apache-zookeeper-3.6.3-bin/bin/../conf/zoo.cfg: 没有那个文件或目录
mkdir: 无法创建目录"": 没有那个文件或目录
Usage: ./zkServer.sh [--config <conf-dir>] {start|start-foreground|stop|version|restart|status|print-cmd}
需要一个zoo.cfg配置文件
cd /usr/local/apache-zookeeper-3.6.3-bin/conf
cp zoo_sample.cfg zoo.cfg
并在zoo.cfg文件中添加如下内容并修改dataDir数据存放的目录,为方便起见在一台服务器上部署3个zookeeper,需要提前配置好集群中都有哪些服务。
server.1=120.53.119.107:2887:3887
server.2=120.53.119.107:2888:3888
server.3=120.53.119.107:2889:3889
service.N =YYY: A:B
N:代表服务器编号(也就是myid里面的值),用于快速的选出leader,其实想在200ms中快速选出leader,大家都互相谦让,拿出这个编号,让最大的做leader(可以先这么理解)
YYY:服务器地址
A:表示 Flower 跟 Leader的通信端口,简称服务端内部通信的端口(默认2888)
B:表示选举端口,如果leader挂掉或者刚开始启动选举leader的时候,使用该端口进行通信选举(默认是3888)
重新命名一台服务
mv apache-zookeeper-3.6.3-bin zookeeper-server-01
这里方便起见数据存放目录,放在了如下位置
/usr/local/zookeeper-server-01/data
zoo.cfg文件有如下内容
# The number of milliseconds of each tick
# 心跳时间2s
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
# 集群启动时间限制在10 * 2000 = 20s
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# 同步数据的时候5 * 2000 = 10s,可以理解leader写完数据同步到其它节点的时间限制
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
# 数据存放的目录
dataDir=/usr/local/zookeeper-server-01/data
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# Be sure to read the maintenance section of the
# administrator guide before turning on autopurge.
#
# http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1
## Metrics Providers
#
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true
#集群中的服务列表,1、2、3代表的是服务的id,需要和myid文件(规定myid文件必须在dataDir目录下)中的内容一致。
#2887:数据同步端口,比如Leader写完数据,通过哪一个端口同步给其它节点。
#3887:选举端口,比如Leader挂了通过哪一个端口选举
server.1=120.53.119.107:2887:3887
server.2=120.53.119.107:2888:3888
server.3=120.53.119.107:2889:3889
由于规定myid文件必须在dataDir目录下,切换目录到如下位置
/usr/local/apache-zookeeper-3.6.3-bin/data
创建myid文件,内容写1
echo 1 > myid
需要部署3台,我们做如下操作
cp -R zookeeper-server-01/ zookeeper-server-02
cp -R zookeeper-server-01/ zookeeper-server-03
启动
./zkServer.sh start
如果报如下错误,看一下防火墙对应的端口是否没有放开
Client port found: 2181. Client address: localhost. Client SSL: false.
Error contacting service. It is probably not running.
如果报如下问题
2022-02-05 21:34:26,875 [myid:1] - ERROR [ListenerHandler-/120.53.119.107:3887:QuorumCnxManager$Listener$ListenerHandler@1093] - Exception while listening
java.net.BindException: 无法指定被请求的地址 (Bind failed)
at java.net.PlainSocketImpl.socketBind(Native Method)
at java.net.AbstractPlainSocketImpl.bind(AbstractPlainSocketImpl.java:387)
at java.net.ServerSocket.bind(ServerSocket.java:375)
at java.net.ServerSocket.bind(ServerSocket.java:329)
at org.apache.zookeeper.server.quorum.QuorumCnxManager$Listener$ListenerHandler.createNewServerSocket(QuorumCnxManager.java:1135)
at org.apache.zookeeper.server.quorum.QuorumCnxManager$Listener$ListenerHandler.acceptConnections(QuorumCnxManager.java:1064)
at org.apache.zookeeper.server.quorum.QuorumCnxManager$Listener$ListenerHandler.run(QuorumCnxManager.java:1033)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
改配置文件,添加如下配置
quorumListenOnAllIPs=true
查看zookeeper的运行状态,报如下错误
[root@VM-24-10-centos bin]# ./zkServer.sh status
/usr/local/jdk1.8/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-server-02/bin/../conf/zoo.cfg
Client port found: 2182. Client address: localhost. Client SSL: false.
Error contacting service. It is probably not running.
查看进程是否在
netstat -ntlp
发现进程都在
tcp6 0 0 :::2181 :::* LISTEN 3668/java
tcp6 0 0 :::2182 :::* LISTEN 3992/java
tcp6 0 0 :::2183 :::* LISTEN 4254/java
查看错误日志/usr/local/zookeeper-server-02/logs/zookeeper-root-server-VM-24-10-centos.out
tail -100f zookeeper-root-server-VM-24-10-centos.out
2022-02-06 09:00:45,505 [myid:2] - WARN [QuorumConnectionThread-[myid=2]-678:QuorumCnxManager@400] - Cannot open channel to 1 at election address /120.53.119.107:3887
java.net.SocketTimeoutException: connect timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at org.apache.zookeeper.server.quorum.QuorumCnxManager.initiateConnection(QuorumCnxManager.java:383)
at org.apache.zookeeper.server.quorum.QuorumCnxManager$QuorumConnectionReqThread.run(QuorumCnxManager.java:457)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
查看集群状态
[root@VM-24-10-centos local]# zookeeper-server-01/bin/zkServer.sh status
/usr/local/jdk1.8/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-server-01/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: leader
[root@VM-24-10-centos local]# zookeeper-server-02/bin/zkServer.sh status
/usr/local/jdk1.8/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-server-02/bin/../conf/zoo.cfg
Client port found: 2182. Client address: localhost. Client SSL: false.
Mode: follower
[root@VM-24-10-centos local]# zookeeper-server-03/bin/zkServer.sh status
/usr/local/jdk1.8/bin/java
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-server-03/bin/../conf/zoo.cfg
Client port found: 2183. Client address: localhost. Client SSL: false.
Mode: follower
可以通过jps(JDK自带的命令)查看Java的进程
[root@VM-24-10-centos ~]# jps
2292 QuorumPeerMain
1381 QuorumPeerMain
1847 QuorumPeerMain
20687 Jps
安装Redis
Redis英文网址:https://redis.io/
右击复制链接
https://download.redis.io/releases/redis-6.2.6.tar.gz
Linux安装wget命令
yum install wget
cd到自己喜欢的目录,这里是/soft。执行如下命令
cd /soft
wget https://download.redis.io/releases/redis-6.2.6.tar.gz
解压
tar -zxvf redis-6.2.6.tar.gz
解压后有个redis-6.2.6目录,cd进去ls一下
[root@VM-24-10-centos redis-6.2.6]# ls
00-RELEASENOTES COPYING MANIFESTO runtest-cluster src
BUGS deps README.md runtest-moduleapi tests
CONDUCT INSTALL redis.conf runtest-sentinel TLS.md
CONTRIBUTING Makefile runtest sentinel.conf utils
Redis是C写的,任何软件的安装应该先看README.md 文件,里面详细有安装步骤。按照README.md 文件的内容执行。
执行make命令
make
如果报如下错误,需要安装GCC
报错信息
cc: command not found
安装GCC
yum install gcc
安装完成,再清理一下
make distclean
再次执行make命令
cd src/目录下就能看到很多可执行程序了,执行 ./redis-server就能把Redis跑起来了
./redis-server
这样启动还是太麻烦而且在前台启动,我们希望能变成一个服务软件在后台启动,变成一个服务,安装到系统里面。
先安装到一个目录下,比如/usr/local/redis
[root@VM-24-10-centos redis-6.2.6]# make install PREFIX=/usr/local/redis
cd /usr/local/redis目录下,ls一下,会有很多编译好的安装命令,这样不会和源码里面的混合在一起
redis-benchmark redis-check-aof redis-check-rdb redis-cli redis-sentinel redis-server
我们期望使用service redis start启动起来变成一个服务,但现在还不支持。现需要修改/etc/profile配置文件,把上面的/usr/local/redis路径加到PATH环境变量里面。
export REDISE_HOME=/usr/local/redis
export PATH=$PATH:$REDISE_HOME/bin
source /etc/profile
echo $PATH
/usr/local/jdk1.8/bin:/usr/local/jdk1.8/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/redis/bin
这个时候就可以直接使用/usr/local/redis/bin里面的命令了,比如:redis-server
cd到Redis源码目录/soft/redis-6.2.6/utils。ls一下,执行install_server.sh 脚本
build-static-symbols.tcl hyperloglog speed-regression.tcl
cluster_fail_time.tcl install_server.sh srandmember
corrupt_rdb.c lru systemd-redis_multiple_servers@.service
create-cluster redis-copy.rb systemd-redis_server.service
generate-command-help.rb redis_init_script tracking_collisions.c
gen-test-certs.sh redis_init_script.tpl whatisdoing.sh
graphs redis-sha1.rb
hashtable releasetools
发现报如下错误. ```java [root@VM-24-10-centos utils]# ./install_server.sh Welcome to the redis service installer This script will help you easily set up a running redis server
This systems seems to use systemd. Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!
解决方案:
```java
vi ./install_server.sh
注释如下代码
#bail if this system is managed by systemd
#_pid_1_exe="$(readlink -f /proc/1/exe)"
#if [ "${_pid_1_exe##*/}" = systemd ]
#then
# echo "This systems seems to use systemd."
# echo "Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!"
# exit 1
#fi
重新执行./install_server.sh脚本
[root@VM-24-10-centos utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!
Please select the redis port for this instance: [6379]
需要填一个端口号,一个物理机器可以安装多个Redis,靠端口号来区分,使用默认[6379]即可,直接回车。
Please select the redis config file name [/etc/redis/6379.conf]
提示如下信息,需要配置文件的位置和名称,配置文件随着端口号的变化而变化,都会为每个Redis实例按照端口号生成相应的配置文件,直接一路回车使用默认即可
Please select the redis config file name [/etc/redis/6379.conf]
Selected default - /etc/redis/6379.conf
Please select the redis log file name [/var/log/redis_6379.log]
Selected default - /var/log/redis_6379.log
Please select the data directory for this instance [/var/lib/redis/6379]
上面需要选一个数据存放目录,Redis虽然是基于内存的,但内存数据掉电易失数据,所以需要持久化。回车使用默认的
Please select the redis executable path [/usr/local/redis/bin/redis-server]
需要填写可执行程序的路径,回车
Please select the redis executable path [/usr/local/redis/bin/redis-server]
Selected config:
Port : 6379
Config file : /etc/redis/6379.conf
Log file : /var/log/redis_6379.log
Data dir : /var/lib/redis/6379
Executable : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
按回车
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6379.conf => /etc/init.d/redis_6379
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
会在/etc/init.d/目录放一个redis_6379脚本,查看一下Redis的状态,redis_6379为脚本的名称
service redis_6379 status
可以通过./install_server.sh在一台机器上安装多个Redis服务。cd到Redis源码安装目录的utils下,/soft/redis-6.2.6/utils
cd /soft/redis-6.2.6/utils
./install_server.sh
提示如下信息
[root@VM-24-10-centos utils]# ./install_server.sh
Welcome to the redis service installer
This script will help you easily set up a running redis server
This systems seems to use systemd.
Please take a look at the provided example service unit files in this directory, and adapt and install them. Sorry!
Please select the redis port for this instance: [6379] 6380
6379已经安装了,给个6380,一路回车即可
Please select the redis port for this instance: [6379] 6380
Please select the redis config file name [/etc/redis/6380.conf]
Selected default - /etc/redis/6380.conf
Please select the redis log file name [/var/log/redis_6380.log]
Selected default - /var/log/redis_6380.log
Please select the data directory for this instance [/var/lib/redis/6380]
Selected default - /var/lib/redis/6380
Please select the redis executable path [/usr/local/redis/bin/redis-server]
Selected config:
Port : 6380
Config file : /etc/redis/6380.conf
Log file : /var/log/redis_6380.log
Data dir : /var/lib/redis/6380
Executable : /usr/local/redis/bin/redis-server
Cli Executable : /usr/local/redis/bin/redis-cli
Is this ok? Then press ENTER to go on or Ctrl-C to abort.
Copied /tmp/6380.conf => /etc/init.d/redis_6380
Installing service...
Successfully added to chkconfig!
Successfully added to runlevels 345!
Starting Redis server...
Installation successful!
[root@VM-24-10-centos utils]# ps -fe | grep redis
root 25896 1 0 22:56 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:6379
root 27579 1 0 23:08 ? 00:00:00 /usr/local/redis/bin/redis-server 127.0.0.1:6380
root 27850 25047 0 23:09 pts/3 00:00:00 grep --color=auto redis
可以通过如下命令启动、关闭、查看状态。redis_6379为具体的Redis服务
service redis_6379 start/stop/status