Ubuntu16.04下安装Redis数据库
技术交流QQ群:1027579432,欢迎你的加入!
1.安装redis数据库
- 安装命令:sudo apt-get install redis-server
2.检查redis服务器系统进程
cdl@cdl-Inspiron-5421:~$ ps -aux | grep redis redis 13615 0.1 0.0 47204 3116 ? Ssl 20:36 0:00 /usr/bin/redis-server 127.0.0.1:6379 cdl 14275 0.0 0.0 21312 1020 pts/5 S+ 20:45 0:00 grep --color=auto redis
3.检查redis服务器占用端口
cdl@cdl-Inspiron-5421:~$ netstat -nlt | grep 6379 tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN
4.通过启动命令检查redis服务器状态
cdl@cdl-Inspiron-5421:~$ sudo /etc/init.d/redis-server status ● redis-server.service - Advanced key-value store Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled) Active: active (running) since 二 2020-05-19 20:36:26 CST; 14min ago Docs: http://redis.io/documentation, man:redis-server(1) Process: 13616 ExecStartPost=/bin/run-parts --verbose /etc/redis/redis-server.post-up.d (code=exited, status=0/SUCCESS) Process: 13614 ExecStart=/usr/bin/redis-server /etc/redis/redis.conf (code=exited, status=0/SUCCESS) Process: 13610 ExecStartPre=/bin/run-parts --verbose /etc/redis/redis-server.pre-up.d (code=exited, status=0/SUCCESS) Main PID: 13615 (redis-server) CGroup: /system.slice/redis-server.service └─13615 /usr/bin/redis-server 127.0.0.1:6379 5月 19 20:36:26 cdl-Inspiron-5421 systemd[1]: Starting Advanced key-value store... 5月 19 20:36:26 cdl-Inspiron-5421 run-parts[13610]: run-parts: executing /etc/redis/redis-server.pre-up.d/00_example 5月 19 20:36:26 cdl-Inspiron-5421 run-parts[13616]: run-parts: executing /etc/redis/redis-server.post-up.d/00_example 5月 19 20:36:26 cdl-Inspiron-5421 systemd[1]: Started Advanced key-value store.
5.通过命令行客户端访问redis
在本机输入redis-cli命令就可以启动客户端程序访问redis服务器,如下所示:
cdl@cdl-Inspiron-5421:~$ redis-cli 127.0.0.1:6379>
命令行的帮助,如下所示:
127.0.0.1:6379> help redis-cli 3.2.0 To get help about Redis commands type: "help @<group>" to get a list of commands in <group> "help <command>" for help on <command> "help <tab>" to get a list of possible help topics "quit" to exit To set redis-cli perferences: ":set hints" enable online hints ":set nohints" disable online hints Set your preferences in ~/.redisclirc
查看所有的key列表:
127.0.0.1:6379> keys * (empty list or set)
6.基本的redis客户端命令操作
- 增加一条记录key1:
127.0.0.1:6379> set key1 "Curry Coder" OK
- 打印记录:
127.0.0.1:6379> get key1 "Curry Coder"
- 增加一条数字记录:
127.0.0.1:6379> set key2 1 OK
- 让数字自增:
127.0.0.1:6379> INCR key2 (integer) 2 127.0.0.1:6379> INCR key2 (integer) 3 127.0.0.1:6379> get key2 "3"
- 增加一个列表记录key3:
# 从左边插入列表 127.0.0.1:6379> LPUSH key3 a (integer) 1 127.0.0.1:6379> LPUSH key3 b (integer) 2 # 从右边插入列表 127.0.0.1:6379> RPUSH key3 c (integer) 3 # 按从左到右顺序打印列表记录 127.0.0.1:6379> LRANGE key3 0 3 1) "b" 2) "a" 3) "c"
- 增加一个哈希记录表key4:
127.0.0.1:6379> HSET key4 name "Curry Coder" (integer) 1 # 在哈希表中插入,email的key和value值 127.0.0.1:6379> HSET key4 email "abc@gmail.com" (integer) 1 # 打印哈希表中name为key4的值 127.0.0.1:6379> HGET key4 name "Curry Coder" # 打印整个哈希表 127.0.0.1:6379> HGETALL key4 1) "name" 2) "Curry Coder" 3) "email" 4) "abc@gmail.com"
- 增加一条哈希表记录key5:
# 增加一条哈希表记录key5,一次插入多个key和value值 127.0.0.1:6379> HMSET key5 username Curry password 123456pp age 18 OK # 打印哈希表中username和age为key5的值 127.0.0.1:6379> HMGET key5 username age 1) "Curry" 2) "18" # 打印完整的哈希表记录 127.0.0.1:6379> HGETALL key5 1) "username" 2) "Curry" 3) "password" 4) "123456pp" 5) "age" 6) "18"
- 删除记录:
# 查看所有的key列表 127.0.0.1:6379> keys * 1) "key3" 2) "key2" 3) "key5" 4) "key1" 5) "key4" # 删除key1和key5 127.0.0.1:6379> del key1 (integer) 1 127.0.0.1:6379> del key5 (integer) 1 # 查看所有的key列表 127.0.0.1:6379> keys * 1) "key3" 2) "key2" 3) "key4"
7.修改redis的配置
使用redis的访问账户:默认情况下,访问redis服务器是不需要密码的,为了增加安全性需要设置redis服务器的访问密码。设置访问密码为redisredis。使用vim打开redis服务器的配置文件redis.conf。
cdl@cdl-Inspiron-5421:~$ sudo vim /etc/redis/redis.conf # 取消注释requirepass requirepass redisredis
让redis服务器被远程访问:默认情况下,redis服务器不运允许远程被访问,只运行本机访问,所以我们需要设置打开远程访问的功能。
cdl@cdl-Inspiron-5421:~$ sudo vim /etc/redis/redis.conf # 注释bind #bind 127.0.0.1
修改后,重启redis服务器:
cdl@cdl-Inspiron-5421:~$ sudo /etc/init.d/redis-server restart [ ok ] Restarting redis-server (via systemctl): redis-server.service.
未使用密码登录redis服务器:
cdl@cdl-Inspiron-5421:~$ redis-cli 127.0.0.1:6379> keys * (error) NOAUTH Authentication required.
使用密码登录redis服务器,输入密码redisredis:
cdl@cdl-Inspiron-5421:~$ redis-cli -a redisredis 127.0.0.1:6379> keys * 1) "key4" 2) "key2" 3) "key3"
检查redis服务器占用端口:网络监听从之前的 127.0.0.1:3306 变成 0 0.0.0.0:3306,表示redis已经允许远程登陆访问。
cdl@cdl-Inspiron-5421:~$ netstat -nlt | grep 6379 tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN tcp6 0 0 :::6379 :::* LISTEN
在远程的另一台Linux访问redis服务器:redis-cli -a redisredis -h 192.168.1.199(redis服务器IP地址)
8.卸载redis数据库
sudo apt-get purge --auto-remove redis-server