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

相關文章
相關標籤/搜索