拷貝收藏私用,別無他意,原博客地址:
https://www.cnblogs.com/zuidongfeng/p/8032505.html
1、安裝redis
第一步:下載redis安裝包
wget http://download.redis.io/releases/redis-4.0.11.tar.gz
1
2
3
4
5
6
7
8
9
10
11
|
[root@localhost local]# wget http://download.redis.io/releases/redis-4.0.11.tar.gz
--2017-12-13 12:35:12-- http://download.redis.io/releases/redis-4.0.11.tar.gz
Resolving download.redis.io (download.redis.io)... 109.74.203.151
Connecting to download.redis.io (download.redis.io)|109.74.203.151|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1723533 (1.6M) [application/x-gzip]
Saving to: ‘redis-4.0.11.tar.gz’
100%[==========================================================================================================>] 1,723,533 608KB/s in 2.8s
2017-12-13 12:35:15 (608 KB/s) - ‘redis-4.0.11.tar.gz’ saved [1723533/1723533]
|
第二步:解壓壓縮包
tar -zxvf redis-4.0.11.tar.gz
1
|
[root@localhost local]# tar -zxvf redis-4.0.11.tar.gz
|
第三步:yum安裝gcc依賴
yum install gcc
1
|
[root@localhost local]# yum install gcc
|
1
|
遇到選擇,輸入y即可<
br
><
br
><
br
>
|
第四步:跳轉到redis解壓目錄下
cd redis-4.0.11
1
|
[root@localhost local]# cd redis-4.0.11
|
第五步:編譯安裝
make MALLOC=libc
1
|
[root@localhost redis-4.0.11]# make MALLOC=libc
|
將/usr/local/redis-4.0.11/src目錄下的文件加到/usr/local/bin目錄
cd src && make install
1
2
3
4
5
6
7
8
9
10
|
[root@localhost redis-4.0.11]# cd src && make install
CC Makefile.dep
Hint: It's a good idea to run 'make test' ;)
INSTALL install
INSTALL install
INSTALL install
INSTALL install
INSTALL install
|
第六步:測試是否安裝成功
先切換到redis src目錄下
1
|
[root@localhost redis-4.0.11]# cd src
|
1、直接啟動redis
./redis-server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
[root@localhost src]# ./redis-server
18685:C 13 Dec 12:56:12.507 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18685:C 13 Dec 12:56:12.507 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=18685, just started
18685:C 13 Dec 12:56:12.507 # Warning: no config file specified, using the default config. In order to specify a config file use ./redis-server /path/to/redis.conf
_._
_.-``__ ''-._
_.-`` `. `_. ''-._ Redis 4.0.11 (00000000/0) 64 bit
.-`` .-```. ```\/ _.,_ ''-._
( ' , .-` | `, ) Running in standalone mode
|`-._`-...-` __...-.``-._|'` _.-'| Port: 6379
| `-._ `._ / _.-' | PID: 18685
`-._ `-._ `-./ _.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' | http://redis.io
`-._ `-._`-.__.-'_.-' _.-'
|`-._`-._ `-.__.-' _.-'_.-'|
| `-._`-._ _.-'_.-' |
`-._ `-._`-.__.-'_.-' _.-'
`-._ `-.__.-' _.-'
`-._ _.-'
`-.__.-'
18685:M 13 Dec 12:56:12.508 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
18685:M 13 Dec 12:56:12.508 # Server initialized
18685:M 13 Dec 12:56:12.508 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
18685:M 13 Dec 12:56:12.508 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
18685:M 13 Dec 12:56:12.508 * Ready to accept connections
|
如上圖:redis啟動成功,但是這種啟動方式需要一直打開窗口,不能進行其他操作,不太方便。
按 ctrl + c可以關閉窗口
2、以后台進程方式啟動redis
第一步:修改redis.conf文件
[root@localhost src]# cd .. [root@localhost redis-4.0.11]# ls 00-RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest-sentinel src utils BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster sentinel.conf tests [root@localhost redis-4.0.11]# vim redis.conf
redis.conf 是redis的配置文件,啟動需要指定啟用該文件的配置方式啟動。該文件在 redis-4.0.11 目錄下面。(如果你沒有vim 就使用 vi 這個編輯命令。vim安裝方式 : yum install vim)
將
1
|
daemonize no
|
修改為
1
|
daemonize yes
|
第二步:指定redis.conf文件啟動,此時啟動要到src目錄下去啟動,
1
|
./redis-server ../redis.conf
|
1
2
3
4
|
[root@localhost src]# ./redis-server ../redis.conf
18713:C 13 Dec 13:07:41.109 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
18713:C 13 Dec 13:07:41.109 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=18713, just started
18713:C 13 Dec 13:07:41.109 # Configuration loaded
|
第三步:關閉redis進程
首先使用ps -aux | grep redis查看redis進程
1
2
3
|
[root@localhost src]# ps -aux | grep redis
root 18714 0.0 0.1 141752 2008 ? Ssl 13:07 0:00 ./redis-server 127.0.0.1:6379
root 18719 0.0 0.0 112644 968 pts/0 R+ 13:09 0:00 grep --color=auto redis
|
使用kill命令殺死進程
1
|
[root@localhost src]# kill -9 18714
|
第七步:設置redis開機自啟動
1、在/etc目錄下新建redis目錄
mkdir redis
1
|
[root@localhost etc]# mkdir redis
|
2、將/usr/local/redis-4.0.11/redis.conf 文件復制一份到/etc/redis目錄下,並命名為6379.conf
1
|
[root@localhost redis]# cp /usr/local/redis-4.0.11/redis.conf /etc/redis/6379.conf
|
3、將redis的啟動腳本復制一份放到/etc/init.d目錄下
1
|
[root@localhost init.d]# cp /usr/local/redis-4.0.11/utils/redis_init_script /etc/init.d/redisd
|
4、設置redis開機自啟動
先切換到/etc/init.d目錄下
然后執行自啟命令
1
2
|
[root@localhost init.d]# chkconfig redisd on
service redisd does not support chkconfig
|
看結果是redisd不支持chkconfig
解決方法:
使用vim編輯redisd文件,在第一行加入如下兩行注釋,保存退出
1
2
|
# chkconfig: 2345 90 10
# description: Redis is a persistent key-value database
|
注釋的意思是,redis服務必須在運行級2,3,4,5下被啟動或關閉,啟動的優先級是90,關閉的優先級是10。
再次執行開機自啟命令,成功
1
|
[root@localhost init.d]# chkconfig redisd on
|
現在可以直接已服務的形式啟動和關閉redis了
啟動:
service redisd start
1
2
3
4
5
|
[root@localhost ~]# service redisd start
Starting Redis server...
2288:C 13 Dec 13:51:38.087 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2288:C 13 Dec 13:51:38.087 # Redis version=4.0.11, bits=64, commit=00000000, modified=0, pid=2288, just started
2288:C 13 Dec 13:51:38.087 # Configuration loaded
|
關閉:
service redisd stop
1
2
3
|
[root@localhost ~]# service redisd stop
Stopping ...
Redis stopped
|
參考資料:
1、http://blog.csdn.net/zc474235918/article/details/50974483
2、http://blog.csdn.net/gxw19874/article/details/51992125
如果出現如下問題:
1
2
|
[root@localhost ~]# service redisd start
/var/run/redis_6379.pid exists, process is already running or crashed
|
到 /var/run/ 目錄下刪除 redis_6379.pid 文件即可
下面選看。但很重要
以下為設置redis登陸密碼,在服務器上裝完redis后因為沒有密碼。容易被攻擊。
博主一個國外的服務器因為沒有設置密碼被連續攻擊后,服務器公司直接給封ip一年。歇菜。
另一個阿里雲服務器被攻擊,直接redis端口對外開放被暫停3天。歇菜。mmp
redis在生產環境中通常都會設置密碼以保證一定的安全性,本篇blog就簡單記錄一下如何在redis中設置客戶端登錄密碼。
修改redis.conf,如果設置過自啟動則修改 /etc/redis/6379.conf
如果沒有設置則在redis的解壓目錄下修改
[root@wangcheng redis-4.0.11]# ls 00-RELEASENOTES CONTRIBUTING deps Makefile README.md runtest runtest-sentinel src utils BUGS COPYING INSTALL MANIFESTO redis.conf runtest-cluster sentinel.conf tests
RT,打開redis.conf文件,搜索requirepass關鍵字,如下圖:
關注標記的那一行,#requirepass foobared。設置密碼的方法就是去掉注釋的#,把foobared替換成自己的密碼即可,例如將密碼設置為123456:
修改完成后重啟redis,再次通過redis客戶端redis-cli登錄並操作可以發現會報一個身份認證錯誤:
這就說明我們已經成功的設置了密碼,所以通過客戶端連接的話必須加上密碼參數才能正常連接:
如上圖所示,加了-a參數之后即可正常連接並操作redis。
jedis設置密碼
當我們用Java客戶端連接redis時會遇到同樣的問題,下面看一段簡單的jedis連接redis的測試代碼:
所以在創建了Jedis的實例后再加上一行jedis.auth("123456");
即可,最后看一下運行結果:
spring-data-redis設置密碼
通常情況下在實際的java項目中我們會選擇Spring提供的spring-data-redis來操作redis,spring的封裝可以給我們提供很多便捷之處。那么spring-data-redis又是如何設置密碼的呢?
首先定義一個redis.properties配置文件,定義一組redis屬性供spring加載使用,其中就包含密碼(redis.password)
# Redis settings redis.host=192.168.145.10 redis.port=6379 redis.password=123456 redis.timeout=100000 redis.maxTotal=300 redis.maxIdle=100 redis.maxWaitMillis=1000 redis.testOnBorrow=true
或者如下:
spring.redis.database=0
spring.redis.host=192.168.145.10
spring.redis.port=6379
spring.redis.password=123456
下面可以忽略不理會。如果上面沒有設置成功,可采用下面方法
一. 如何初始化redis的密碼?
總共2個步驟:
a.在配置文件中有個參數: requirepass 這個就是配置redis訪問密碼的參數。
比如 requirepass test123
b.配置文件中參數生效需要重啟重啟redis 。
二.不重啟redis如何配置密碼?
a. 在配置文件中配置requirepass的密碼(當redis重啟時密碼依然有效)。
# requirepass foobared
如 修改成 :
requirepass test123
b. 進入redis重定義參數
查看當前的密碼:
[root@slaver251 redis-2.4.16]# service redisd start
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> config get requirepass
1) "requirepass"
2) (nil)
顯示密碼是空的,
然后設置密碼:
redis 127.0.0.1:6379> config set requirepass test123
OK
再次查詢密碼:
redis 127.0.0.1:6379> config get requirepass
(error) ERR operation not permitted
此時報錯了!
現在只需要密碼認證就可以了。
redis 127.0.0.1:6379> auth test123
OK
再次查詢密碼:
redis 127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "test123"
密碼已經得到修改。
當到了可以重啟redis的時候 由於配置參數已經修改 所以密碼會自動生效。
要是配置參數沒添加密碼 那么redis重啟 密碼將相當於沒有設置。
三.如何登錄有密碼的redis?
a.在登錄的時候 密碼就輸入
[root@slaver251 redis-2.4.16]# service redisd start
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "test123"
b.先登錄再驗證:
[root@slaver251 redis-2.4.16]# service redisd start
redis 127.0.0.1:6379>
redis 127.0.0.1:6379> auth test123
OK
redis 127.0.0.1:6379> config get requirepass
1) "requirepass"
2) "test123"
redis 127.0.0.1:6379>
四. master 有密碼,slave 如何配置?
當master 有密碼的時候 配置slave 的時候 相應的密碼參數也得相應的配置好。不然slave 是無法進行正常復制的。
相應的參數是:
#masterauth
比如:
masterauth mstpassword
可參考資料:http://blog.csdn.net/luozhonghua2014/article/details/54649295