Ubuntu 安装Redis

在Ubuntu中安装Redis有两种方式,html

我用的是Ubuntu自带的apt-getpython

什么的是apt-get:linux

高级包装工具(英语:Advanced Packaging Tools,简称:APT)是Debian[待宾]及其衍生发行版(如:ubuntu)的软件包管理器。redis

APT能够自动下载,配置,安装二进制或者源代码格式的软 件包,所以简化了 Unix系统上管理软件的过程,apt-get命令通常须要root权限执行,因此通常跟着sudo命令。shell

在 Ubuntu 系统安装 Redi 可使用如下命令:

$sudo apt-get update
$sudo apt-get install redis-server

启动 Redis

$ redis-server

查看 redis 是否启动?

$ redis-cli

以上命令将打开如下终端:

redis 127.0.0.1:6379>

127.0.0.1 是本机 IP ,6379 是 redis 服务端口。如今咱们输入 PING 命令。

redis 127.0.0.1:6379> ping
PONG

以上说明咱们已经成功安装了redis。

操做shell命令:数据库

hongdada@ubuntu:~$ sudo
usage: sudo -h | -K | -k | -V
usage: sudo -v [-AknS] [-g group] [-h host] [-p prompt] [-u user]
usage: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user] [-u user]
            [command]
usage: sudo [-AbEHknPS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p
            prompt] [-u user] [VAR=value] [-i|-s] [<command>]
usage: sudo -e [-AknS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p
            prompt] [-u user] file ...
hongdada@ubuntu:~$ sudo apt-get update
[sudo] hongdada 的密码: 
获取:1 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
命中:2 http://us.archive.ubuntu.com/ubuntu xenial InRelease           
获取:3 http://us.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
命中:4 http://archive.ubuntukylin.com:10006/ubuntukylin xenial InRelease
获取:5 http://us.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
获取:6 http://us.archive.ubuntu.com/ubuntu xenial/main Translation-zh_CN [74.8 kB]
获取:7 http://us.archive.ubuntu.com/ubuntu xenial/restricted Translation-zh_CN [1,652 B]
获取:8 http://us.archive.ubuntu.com/ubuntu xenial/universe Translation-zh_CN [172 kB]
获取:9 http://us.archive.ubuntu.com/ubuntu xenial/multiverse Translation-zh_CN [4,984 B]
已下载 559 kB,耗时 14秒 (38.0 kB/s)                                  
E: 没法得到锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用)
E: 没法锁定管理目录(/var/lib/dpkg/),是否有其余进程正占用它?
hongdada@ubuntu:~$ ^C
hongdada@ubuntu:~$ sodo rm /var/cache/apt/archives/lock
未找到 'sodo' 命令,您要输入的是不是:
 命令 'todo' 来自于包 'devtodo' (universe)
 命令 'solo' 来自于包 'sgt-puzzles' (universe)
 命令 'sudo' 来自于包 'sudo-ldap' (universe)
 命令 'sudo' 来自于包 'sudo' (main)
sodo:未找到命令
hongdada@ubuntu:~$ sudo rm /var/cache/apt/archives/lock
hongdada@ubuntu:~$ sudo rm /var/lib/dpkg/lock
hongdada@ubuntu:~$ sudo apt-get update
命中:1 http://archive.ubuntukylin.com:10006/ubuntukylin xenial InRelease
获取:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
命中:3 http://us.archive.ubuntu.com/ubuntu xenial InRelease
获取:4 http://us.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
获取:5 http://us.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
已下载 306 kB,耗时 8秒 (37.6 kB/s)                                   
正在读取软件包列表... 完成
hongdada@ubuntu:~$ sudo apt-get install redis-server
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
下列软件包是自动安装的而且如今不须要了:
  libpango1.0-0 libpangox-1.0-0
使用'sudo apt autoremove'来卸载它(它们)。
将会同时安装下列软件:
  libjemalloc1 redis-tools
建议安装:
  ruby-redis
下列【新】软件包将被安装:
  libjemalloc1 redis-server redis-tools
升级了 0 个软件包,新安装了 3 个软件包,要卸载 0 个软件包,有 27 个软件包未被升级。
须要下载 517 kB 的归档。
解压缩后会消耗 1,505 kB 的额外空间。
您但愿继续执行吗? [Y/n] Y
获取:1 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 libjemalloc1 amd64 3.6.0-9ubuntu1 [78.9 kB]
获取:2 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 redis-tools amd64 2:3.0.6-1 [95.3 kB]
获取:3 http://us.archive.ubuntu.com/ubuntu xenial/universe amd64 redis-server amd64 2:3.0.6-1 [343 kB]
已下载 517 kB,耗时 37秒 (13.9 kB/s)                                  
正在选中未选择的软件包 libjemalloc1。
(正在读取数据库 ... 系统当前共安装有 205743 个文件和目录。)
正准备解包 .../libjemalloc1_3.6.0-9ubuntu1_amd64.deb  ...
正在解包 libjemalloc1 (3.6.0-9ubuntu1) ...
正在选中未选择的软件包 redis-tools。
正准备解包 .../redis-tools_2%3a3.0.6-1_amd64.deb  ...
正在解包 redis-tools (2:3.0.6-1) ...
正在选中未选择的软件包 redis-server。
正准备解包 .../redis-server_2%3a3.0.6-1_amd64.deb  ...
正在解包 redis-server (2:3.0.6-1) ...
正在处理用于 libc-bin (2.23-0ubuntu5) 的触发器 ...
正在处理用于 man-db (2.7.5-1) 的触发器 ...
正在处理用于 systemd (229-4ubuntu16) 的触发器 ...
正在处理用于 ureadahead (0.100.0-19) 的触发器 ...
正在设置 libjemalloc1 (3.6.0-9ubuntu1) ...
正在设置 redis-tools (2:3.0.6-1) ...
正在设置 redis-server (2:3.0.6-1) ...
正在处理用于 libc-bin (2.23-0ubuntu5) 的触发器 ...
正在处理用于 systemd (229-4ubuntu16) 的触发器 ...
正在处理用于 ureadahead (0.100.0-19) 的触发器 ...
hongdada@ubuntu:~$ redis-server
5703:C 20 Feb 22:02:38.206 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
5703:M 20 Feb 22:02:38.206 * Increased maximum number of open files to 10032 (it was originally set to 1024).
                _._                                                  
           _.-``__ ''-._                                             
      _.-``    `.  `_.  ''-._           Redis 3.0.6 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 5703
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

5703:M 20 Feb 22:02:38.206 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
5703:M 20 Feb 22:02:38.206 # Server started, Redis version 3.0.6
5703:M 20 Feb 22:02:38.206 # 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.
5703:M 20 Feb 22:02:38.206 # 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.
5703:M 20 Feb 22:02:38.206 * The server is now ready to accept connections on port 6379

能够看出安装完成,其中有一个小的问题,在安装的过程当中遇到的小麻烦,进程被占用,ubuntu

E: 没法得到锁 /var/lib/dpkg/lock - open (11: 资源暂时不可用)
E: 没法锁定管理目录(/var/lib/dpkg/),是否有其余进程正占用它?
hongdada@ubuntu:~$ ^C
hongdada@ubuntu:~$ sodo rm /var/cache/apt/archives/lock
未找到 'sodo' 命令,您要输入的是不是:
 命令 'todo' 来自于包 'devtodo' (universe)
 命令 'solo' 来自于包 'sgt-puzzles' (universe)
 命令 'sudo' 来自于包 'sudo-ldap' (universe)
 命令 'sudo' 来自于包 'sudo' (main)
sodo:未找到命令
hongdada@ubuntu:~$ sudo rm /var/cache/apt/archives/lock
hongdada@ubuntu:~$ sudo rm /var/lib/dpkg/lock

vim的安装以及编辑redis.conf文件vim

hongdada@ubuntu:~$ cd /etc
hongdada@ubuntu:/etc$ cd /redis
bash: cd: /redis: 没有那个文件或目录
hongdada@ubuntu:/etc$ ls
acpi                    hosts                    profile
adduser.conf            hosts.allow              profile.d
alternatives            hosts.deny               protocols
anacrontab              hp                       pulse
apg.conf                ifplugd                  python
apm                     iftab                    python2.7
apparmor                ImageMagick-6            python3
apparmor.d              init                     python3.5
apport                  init.d                   rc0.d
appstream.conf          initramfs-tools          rc1.d
apt                     inputrc                  rc2.d
aptdaemon               insserv                  rc3.d
at-spi2                 insserv.conf             rc4.d
avahi                   insserv.conf.d           rc5.d
bash.bashrc             iproute2                 rc6.d
bash_completion         issue                    rc.local
bash_completion.d       issue.net                rcS.d
bindresvport.blacklist  kbd                      redis
binfmt.d                kernel                   resolvconf
bluetooth               kernel-img.conf          resolv.conf
brlapi.key              kerneloops.conf          rmt
brltty                  ldap                     rpc
brltty.conf             ld.so.cache              rsyslog.conf
ca-certificates         ld.so.conf               rsyslog.d
ca-certificates.conf    ld.so.conf.d             sane.d
calendar                legal                    securetty
chatscripts             libao.conf               security
compizconfig            libaudit.conf            selinux
console-setup           libnl-3                  sensors3.conf
cracklib                libpaper.d               sensors.d
cron.d                  libreoffice              services
cron.daily              lightdm                  sgml
cron.hourly             lintianrc                shadow
cron.monthly            locale.alias             shadow-
crontab                 locale.gen               shells
cron.weekly             localtime                signond.conf
cups                    logcheck                 signon-ui
cupshelpers             login.defs               skel
dbus-1                  logrotate.conf           speech-dispatcher
dconf                   logrotate.d              ssh
debconf.conf            lsb-release              ssl
debian_version          ltrace.conf              subgid
default                 machine-id               subgid-
deluser.conf            magic                    subuid
depmod.d                magic.mime               subuid-
dhcp                    mailcap                  sudoers
dictionaries-common     mailcap.order            sudoers.d
dnsmasq.d               manpath.config           sysctl.conf
doc-base                mime.types               sysctl.d
dpkg                    mke2fs.conf              systemd
drirc                   modprobe.d               terminfo
emacs                   modules                  thermald
environment             modules-load.d           thunderbird
firefox                 mtab                     timezone
fonts                   mtools.conf              tmpfiles.d
fstab                   nanorc                   tpvmlp.conf
fuse.conf               network                  ucf.conf
fwupd.conf              NetworkManager           udev
gai.conf                networks                 udisks2
gconf                   newt                     ufw
gdb                     nsswitch.conf            updatedb.conf
ghostscript             opt                      update-manager
gnome                   os-release               update-motd.d
gnome-app-install       pam.conf                 update-notifier
groff                   pam.d                    UPower
group                   papersize                upstart-xsessions
group-                  passwd                   usb_modeswitch.conf
grub.d                  passwd-                  usb_modeswitch.d
gshadow                 pcmcia                   vim
gshadow-                perl                     vmware-tools
gss                     pki                      vtrgb
gtk-2.0                 pm                       wgetrc
gtk-3.0                 pnm2ppa.conf             wpa_supplicant
guest-session           polkit-1                 X11
hdparm.conf             popularity-contest.conf  xdg
host.conf               ppp                      xml
hostname                presage.xml              zsh_command_not_found
hongdada@ubuntu:/etc$ cd /redis
bash: cd: /redis: 没有那个文件或目录
hongdada@ubuntu:/etc$ su
密码: 
root@ubuntu:/etc# cd /redis
bash: cd: /redis: 没有那个文件或目录
root@ubuntu:/etc# cd redis
root@ubuntu:/etc/redis# vim redis.conf
程序 'vim' 已包含在下列软件包中:
 * vim
 * vim-gnome
 * vim-tiny
 * vim-athena
 * vim-athena-py2
 * vim-gnome-py2
 * vim-gtk
 * vim-gtk-py2
 * vim-gtk3
 * vim-gtk3-py2
 * vim-nox
 * vim-nox-py2
请尝试:apt install <选定的软件包>
root@ubuntu:/etc/redis# apt-get update
命中:1 http://us.archive.ubuntu.com/ubuntu xenial InRelease                    
获取:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]    
命中:3 http://archive.ubuntukylin.com:10006/ubuntukylin xenial InRelease       
获取:4 http://us.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]   
获取:5 http://us.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB] 
获取:6 http://security.ubuntu.com/ubuntu xenial-security/main amd64 DEP-11 Metadata [68.0 kB]
获取:7 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 DEP-11 Metadata [306 kB]
获取:8 http://security.ubuntu.com/ubuntu xenial-security/main DEP-11 64x64 Icons [43.1 kB]
获取:9 http://us.archive.ubuntu.com/ubuntu xenial-updates/main DEP-11 64x64 Icons [189 kB]
获取:10 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 Packages [77.5 kB]
获取:11 http://us.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 DEP-11 Metadata [134 kB]
获取:12 http://us.archive.ubuntu.com/ubuntu xenial-updates/universe DEP-11 64x64 Icons [162 kB]
获取:13 http://security.ubuntu.com/ubuntu xenial-security/universe i386 Packages [73.1 kB]
获取:14 http://us.archive.ubuntu.com/ubuntu xenial-updates/multiverse amd64 DEP-11 Metadata [2,520 B]
获取:15 http://us.archive.ubuntu.com/ubuntu xenial-backports/main amd64 DEP-11 Metadata [3,328 B]
获取:16 http://security.ubuntu.com/ubuntu xenial-security/universe Translation-en [43.0 kB]
获取:17 http://security.ubuntu.com/ubuntu xenial-security/universe amd64 DEP-11 Metadata [32.1 kB]
获取:18 http://security.ubuntu.com/ubuntu xenial-security/universe DEP-11 64x64 Icons [37.0 kB]
已下载 1,476 kB,耗时 20秒 (73.0 kB/s)                                         
正在读取软件包列表... 完成
root@ubuntu:/etc/redis# apt-get install vim
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
下列软件包是自动安装的而且如今不须要了:
  libpango1.0-0 libpangox-1.0-0 linux-headers-4.4.0-21
  linux-headers-4.4.0-21-generic linux-image-4.4.0-21-generic
  linux-image-extra-4.4.0-21-generic
使用'apt autoremove'来卸载它(它们)。
将会同时安装下列软件:
  vim-runtime
建议安装:
  ctags vim-doc vim-scripts vim-gnome-py2 | vim-gtk-py2 | vim-gtk3-py2
  | vim-athena-py2 | vim-nox-py2
下列【新】软件包将被安装:
  vim vim-runtime
升级了 0 个软件包,新安装了 2 个软件包,要卸载 0 个软件包,有 7 个软件包未被升级。
须要下载 6,199 kB 的归档。
解压缩后会消耗 30.0 MB 的额外空间。
您但愿继续执行吗? [Y/n] Y
获取:1 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 vim-runtime all 2:7.4.1689-3ubuntu1.2 [5,164 kB]
获取:2 http://us.archive.ubuntu.com/ubuntu xenial-updates/main amd64 vim amd64 2:7.4.1689-3ubuntu1.2 [1,036 kB]
已下载 6,199 kB,耗时 1分 27秒 (70.9 kB/s)                                     
正在选中未选择的软件包 vim-runtime。
(正在读取数据库 ... 系统当前共安装有 238090 个文件和目录。)
正准备解包 .../vim-runtime_2%3a7.4.1689-3ubuntu1.2_all.deb  ...
正在添加 vim-runtime 致使 /usr/share/vim/vim74/doc/help.txt 转移到 /usr/share/vim/vim74/doc/help.txt.vim-tiny
正在添加 vim-runtime 致使 /usr/share/vim/vim74/doc/tags 转移到 /usr/share/vim/vim74/doc/tags.vim-tiny
正在解包 vim-runtime (2:7.4.1689-3ubuntu1.2) ...
正在选中未选择的软件包 vim。
正准备解包 .../vim_2%3a7.4.1689-3ubuntu1.2_amd64.deb  ...
正在解包 vim (2:7.4.1689-3ubuntu1.2) ...
正在处理用于 man-db (2.7.5-1) 的触发器 ...
正在设置 vim-runtime (2:7.4.1689-3ubuntu1.2) ...
正在设置 vim (2:7.4.1689-3ubuntu1.2) ...
update-alternatives: 使用 /usr/bin/vim.basic 来在自动模式中提供 /usr/bin/vim (vim)
update-alternatives: 使用 /usr/bin/vim.basic 来在自动模式中提供 /usr/bin/vimdiff (vimdiff)
update-alternatives: 使用 /usr/bin/vim.basic 来在自动模式中提供 /usr/bin/rvim (rvim)
update-alternatives: 使用 /usr/bin/vim.basic 来在自动模式中提供 /usr/bin/rview (rview)
update-alternatives: 使用 /usr/bin/vim.basic 来在自动模式中提供 /usr/bin/vi (vi)
update-alternatives: 使用 /usr/bin/vim.basic 来在自动模式中提供 /usr/bin/view (view)
update-alternatives: 使用 /usr/bin/vim.basic 来在自动模式中提供 /usr/bin/ex (ex)
root@ubuntu:/etc/redis# vim redis.conf
root@ubuntu:/etc/redis# ^C
root@ubuntu:/etc/redis# 
View Code

进入到vim编辑器中编辑redis.conf文件c#

#requirepass foobaredapi

取消注释,foobared替换成密码

修改好redis.conf文件以后,要重启redis服务,并用新密码尝试登入。

hongdada@ubuntu:~$ sudo service redis restart
[sudo] hongdada 的密码: 
对不起,请重试。
[sudo] hongdada 的密码: 
hongdada@ubuntu:~$ redis-server /etc/redis/redis.conf
21495:C 21 Feb 00:24:18.565 # Fatal error, can't open config file '/etc/redis/redis.conf'
hongdada@ubuntu:~$ su
密码: 
root@ubuntu:/home/hongdada# redis-server /etc/redis/redis.conf
root@ubuntu:/home/hongdada# redis-cli -h 127.0.0.1 -p 6379
127.0.0.1:6379> keys *
(error) NOAUTH Authentication required.
127.0.0.1:6379> auth hongda$123456
OK
127.0.0.1:6379> keys *
1) "age"
2) "name"
127.0.0.1:6379> 

打开6379的防火墙端口

因为LInux原始的防火墙工具iptables过于繁琐,因此ubuntu默认提供了一个基于iptable之上的防火墙工具ufw。

hongdada@ubuntu:~$ sudo apt-get install ufw
正在读取软件包列表... 完成
正在分析软件包的依赖关系树       
正在读取状态信息... 完成       
ufw 已是最新版 (0.35-0ubuntu2)。
ufw 已设置为手动安装。
下列软件包是自动安装的而且如今不须要了:
  libpango1.0-0 libpangox-1.0-0 linux-headers-4.4.0-21
  linux-headers-4.4.0-21-generic linux-image-4.4.0-21-generic
  linux-image-extra-4.4.0-21-generic
使用'sudo apt autoremove'来卸载它(它们)。
升级了 0 个软件包,新安装了 0 个软件包,要卸载 0 个软件包,有 7 个软件包未被升级。
hongdada@ubuntu:~$ sudo ufw enable
在系统启动时启用和激活防火墙
hongdada@ubuntu:~$ sudo ufw default deny
默认的 incoming 策略更改成 “deny”
(请相应地更新你的防火墙规则)
hongdada@ubuntu:~$ sudo ufw allow 6379/tcp
规则已添加
规则已添加 (v6)
hongdada@ubuntu:~$ sudo ufw allow from 192.168.1.222
规则已添加
hongdada@ubuntu:~$ sudo ufw status
状态: 激活

至                          动做          来自
-                          --          --
6379/tcp                   ALLOW       Anywhere                  
Anywhere                   ALLOW       192.168.1.222             
6379/tcp (v6)              ALLOW       Anywhere (v6)             

hongdada@ubuntu:~$ sudo ufw allow 80/tcp
规则已添加
规则已添加 (v6)
hongdada@ubuntu:~$ sudo ufw allow 8080/tcp
规则已添加
规则已添加 (v6)
hongdada@ubuntu:~$ sudo ufw allow 8000/tcp
规则已添加
规则已添加 (v6)

 

 

http://www.cnblogs.com/langtianya/p/5187681.html

http://blog.csdn.net/zkp0601/article/details/41349253

http://blog.csdn.net/zyz511919766/article/details/42268219

http://www.cnblogs.com/emanlee/archive/2011/11/10/2243930.html

http://wiki.ubuntu.org.cn/UFW%E9%98%B2%E7%81%AB%E5%A2%99%E7%AE%80%E5%8D%95%E8%AE%BE%E7%BD%AE

相关文章
相关标签/搜索