Linux安装JDK,MySQL,Tomcat,Redis等软件

添加微信号GO_HOME_2020一起探讨Java
添加微信一起探讨Java

Linux安装软件

Linux版本: CentOS 7.4 64位
- *.rpm形式的二进制软件包: RPM(RedHat Packge Manager)是RedHat公司出的软件包管理器,使用它可以很容易地对rpm形式的软件包进行安装、升级、卸载、验证、查询等操作,安装简单,而卸载时也可以将软件安装在多处目录中的文件删除干净,因此推荐初学者尽可能使用rpm形式的软件包。
- .tar.gz/.tgz、.bz2形式的 二进制软件包: .tar.gz/*.bz2形式的二进制软件包是用tar工具来打包、用gzip/bzip2压缩的,安装时直接解包即可。对于解压后只有单一目录的软件,
卸载时用命令“rm -rf 软件目录名”;如果解压后文件分散在多处目录中,则必须一一手动删除(稍麻烦),想知道解压时向系统中安装了哪些
文件,可以用命令“tar ztvf .tar.gz”/“tar ytvf .bz2”获取清单。

1. 安装JDK

第一步: 查看系统中是否存在JDk

rpm -aq|grep -i jdk

[root@VM_46_94_centos local]# rpm -aq|grep -i jdk
[root@VM_46_94_centos local]# 

如果存在JDk则卸载JDK: rpm -ev jdk1.8-1.8.0_161-fcs.x86_64

如果有软件依赖jdk关系, 则需要卸载命令中添加–nodeps:
rpm -ev –nodeps jdk1.8-1.8.0_161-fcs.x86_64

第二步: 安装JDK

rpm -ivh jdk-8u161-linux-x64.rpm
默认安装在/usr/java目录下

[root@VM_46_94_centos local]# rpm -ivh jdk-8u161-linux-x64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:jdk1.8-2000:1.8.0_161-fcs        ################################# [100%]
Unpacking JAR files...
        tools.jar...
        plugin.jar...
        javaws.jar...
        deploy.jar...
        rt.jar...
        jsse.jar...
        charsets.jar...
        localedata.jar...

查看JDk安装目录: updatedb locate jdk

[root@VM_46_94_centos ~]# updatedb
[root@VM_46_94_centos ~]# locate jdk

第三步: 配置环境变量

vi /etc/profile

[root@VM_46_94_centos ~]# vim /etc/profile

在文件的最后添加这两行:

  • export JAVA_HOME=/usr/java/jdk1.8.0_161
  • export PATH= JAVAHOME/bin: PATH
unset i
unset -f pathmunge
export JAVA_HOME=/usr/java/jdk1.8.0_161
export PATH=$JAVA_HOME/bin:$PATH

第四步: 使配置的环境变量生效

source /etc/profile

[root@VM_46_94_centos ~]# source /etc/profile

第五步: 查看环境变量

env

[root@VM_46_94_centos ~]# env
XDG_SESSION_ID=309
HOSTNAME=VM_46_94_centos
TERM=xterm
SHELL=/bin/bash
HISTSIZE=3000
SSH_CLIENT=58.62.52.254 15346 22
SSH_TTY=/dev/pts/1
USER=root
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:
MAIL=/var/spool/mail/root
PATH=/usr/java/jdk1.8.0_161/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
PWD=/root
JAVA_HOME=/usr/java/jdk1.8.0_161
LANG=en_US.utf8
HISTCONTROL=ignoredups
SHLVL=1
HOME=/root
LOGNAME=root
SSH_CONNECTION=58.62.52.254 15346 10.186.46.94 22
LESSOPEN=||/usr/bin/lesspipe.sh %s
PROMPT_COMMAND=history -a; printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"
XDG_RUNTIME_DIR=/run/user/0
HISTTIMEFORMAT=%F %T 
_=/usr/bin/env
[root@VM_46_94_centos ~]# 

第六步: 和JDK版本

java -version

[root@VM_46_94_centos ~]# java -version
java version "1.8.0_161"
Java(TM) SE Runtime Environment (build 1.8.0_161-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.161-b12, mixed mode)
[root@VM_46_94_centos ~]# 

2. 安装MySQL

参考1
参考2
参考3
参考4

第一步: 解压MySQL压缩包并更名

tar -zxvf mysql-5.5.59-linux-glibc2.12-x86_64.tar.gz

[root@VM_46_94_centos local]# tar -zxvf mysql-5.5.59-linux-glibc2.12-x86_64.tar.gz

解压包更名: mv -i mysql-5.5.59-linux-glibc2.12-x86_64 mysql

[root@VM_46_94_centos local]# mv -i mysql-5.5.59-linux-glibc2.12-x86_64 mysql

第二步: 添加系统mysql组和mysql用户

执行命令:groupadd mysql 和 useradd -r -g mysql mysql

[root@VM_46_94_centos local]# groupadd mysql
[root@VM_46_94_centos local]# useradd -r -g mysql mysql

第三步: 安装数据库

  • 进入安装mysql软件目录:cd /usr/local/mysql
[root@VM_46_94_centos local]# cd /usr/local/mysql
[root@VM_46_94_centos mysql]# 
  • 修改当前目录拥有者为mysql用户: chown -R mysql:mysql ./
[root@VM_46_94_centos mysql]# chown -R mysql:mysql ./
  • 安装数据库: ./scripts/mysql_install_db –user=mysql
[root@VM_46_94_centos mysql]# ./scripts/mysql_install_db --user=mysql
Installing MySQL system tables...
180408 13:05:53 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
180408 13:05:53 [Note] ./bin/mysqld (mysqld 5.5.59) starting as process 7425 ...
OK
Filling help tables...
180408 13:05:55 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
180408 13:05:55 [Note] ./bin/mysqld (mysqld 5.5.59) starting as process 7433 ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h VM_46_94_centos password 'new-password'

Alternatively you can run:
./bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd ./mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

[root@VM_46_94_centos mysql]# 
  • 修改当前目录拥有者为root用户: chown -R root:root ./

[root@VM_46_94_centos mysql]# chown -R root:root ./

第四步: 复制启动程序

cp support-files/mysql.server /etc/init.d/mysql.server

[root@VM_46_94_centos mysql]# cp support-files/mysql.server /etc/init.d/mysql.server

第五步: 启动MySQL服务

/etc/init.d/mysql.server start 报错

[root@VM_46_94_centos mysql]# cp support-files/mysql.server /etc/init.d/mysql.server
[root@VM_46_94_centos mysql]# /etc/init.d/mysql.server start
Starting MySQL.180408 13:10:40 mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
 ERROR! The server quit without updating PID file (/var/lib/mysql/VM_46_94_centos.pid).
[root@VM_46_94_centos mysql]# 

解决: 修改配置文件 vim /etc/my.cnf

[root@VM_46_94_centos ~]# vim /etc/my.cnf

配置相应地方修改为如下:

[mysqld_safe]
log-error=/var/log/mysql/mariadb.log
pid-file=/var/run/mysql/mariadb.pid

再启动:成功

[root@VM_46_94_centos ~]# /etc/init.d/mysql.server start
Starting MySQL.Logging to '/var/log/mysql/mariadb.log'.
.. SUCCESS! 
[root@VM_46_94_centos ~]# 

查看进程: ps -ef |grep mysql

[root@VM_46_94_centos ~]# ps -ef |grep mysql
root     14136     1  0 13:41 pts/1    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/lib/mysql/VM_46_94_centos.pid
mysql    14287 14136  0 13:41 pts/1    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/var/lib/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/log/mysql/mariadb.log --pid-file=/var/lib/mysql/VM_46_94_centos.pid --socket=/var/lib/mysql/mysql.sock
root     16490  4284  0 13:43 pts/1    00:00:00 grep --color=auto mysql
[root@VM_46_94_centos ~]# 

第六步: 建立软链接,启动mysql

ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql

[root@VM_46_94_centos ~]#  ln -s /usr/local/mysql/bin/mysql /usr/local/bin/mysql
[root@VM_46_94_centos ~]# cd /usr/local/bin

这里报错

[root@VM_46_94_centos bin]# mysql
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

解决方案:先停止服务,然后修改配置
/etc/init.d/mysql.server stop

[root@VM_46_94_centos bin]# /etc/init.d/mysql.server stop
 ERROR! MySQL server PID file could not be found!
[root@VM_46_94_centos bin]# vim /etc/my.cnf
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock

将上面配置修改为:

[mysqld]
datadir=/var/lib/mysql
socket=/tmp/mysql.sock

第七步: 重新启动: 成功,修改密码

/etc/init.d/mysql.server

[root@VM_46_94_centos bin]# /etc/init.d/mysql.server start
Starting MySQL.Logging to '/var/log/mysql/mariadb.log'.
. SUCCESS! 
[root@VM_46_94_centos bin]# 

初始登录

[root@VM_46_94_centos bin]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.59 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

修改密码:

mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> update user set password=password("123456") where user='root';     
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> 

第八步: 修改数据库的字符集

//查看数据库的字符集情况
mysql> show variables like 'char%';
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8                             |
| character_set_connection | utf8                             |
| character_set_database   | latin1                           |
| character_set_filesystem | binary                           |
| character_set_results    | utf8                             |
| character_set_server     | latin1                           |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

//修改数据库字符集,其他的格式相同。
mysql> set character_set_database=utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'char%';    
+--------------------------+----------------------------------+
| Variable_name            | Value                            |
+--------------------------+----------------------------------+
| character_set_client     | utf8                             |
| character_set_connection | utf8                             |
| character_set_database   | utf8                             |
| character_set_filesystem | binary                           |
| character_set_results    | utf8                             |
| character_set_server     | latin1                           |
| character_set_system     | utf8                             |
| character_sets_dir       | /usr/local/mysql/share/charsets/ |
+--------------------------+----------------------------------+
8 rows in set (0.00 sec)

mysql> 

==注意==: 重启后,字符集并不好用。
需要修改配置文件/etc/my.cnf

[root@VM_46_94_centos ~]# vim /etc/my.cnf

//在[mysqld]下添加一段话,不能写错了,否则会启动不了
//character-set-server=utf8
[mysqld]
character-set-server=utf8
datadir=/var/lib/mysql
socket=/tmp/mysql.sock

第九步: 设置MySQL自动启动

命令说明
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld拷贝启动文件到/etc/init.d/下并重命令为mysqld
chmod 755 /etc/init.d/mysqld增加执行权限
chkconfig –list mysqld检查自启动项列表中没有mysqld这个启动项
chkconfig –add mysqld如果没有就添加mysqld
chkconfig mysqld on用这个命令设置开机启动
[root@VM_46_94_centos init.d]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@VM_46_94_centos init.d]# ls
DbSecuritySpt  functions  jexec  mysqld  mysql.server  netconsole  network  README  redisd  selinux
[root@VM_46_94_centos init.d]# chmod 755 /etc/init.d/mysqld
[root@VM_46_94_centos init.d]# chkconfig --list mysqld

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

service mysqld supports chkconfig, but is not referenced in any runlevel (run 'chkconfig --add mysqld')
[root@VM_46_94_centos init.d]# chkconfig --add mysqld
[root@VM_46_94_centos init.d]# chkconfig mysqld on
[root@VM_46_94_centos init.d]# chkconfig --list mysqld

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off
[root@VM_46_94_centos init.d]# 

第十步: MySQL授权允许远程连接

grant all privileges on . to ‘root’@’%’ identified by ‘123456’;

mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

mysql> 

3. 安装Tomcat

  1. Tomcat版本: apache-tomcat-8.5.29.tar.gz
  2. 下载地址: https://tomcat.apache.org/download-80.cgi#8.5.29
    下载Core中的tar.gz版本
  3. tomcat是一个绿色软件,不用安装,直接使用. 但JAVA_HOME一定配置

第一步: 解压tomcat

tar -zxvf apache-tomcat-8.5.29.tar.gz

[root@VM_46_94_centos local]# tar -zxvf apache-tomcat-8.5.29.tar.gz 

第二步: 启动tomcat

启动: ./startup.sh
关闭: ./shutdown.sh

[root@VM_46_94_centos apache-tomcat-8.5.29]# cd bin
[root@VM_46_94_centos bin]# ls
bootstrap.jar  catalina-tasks.xml            configtest.bat  digest.bat        setclasspath.sh  startup.bat      tomcat-native.tar.gz  version.bat
catalina.bat   commons-daemon.jar            configtest.sh   digest.sh         shutdown.bat     startup.sh       tool-wrapper.bat      version.sh
catalina.sh    commons-daemon-native.tar.gz  daemon.sh       setclasspath.bat  shutdown.sh      tomcat-juli.jar  tool-wrapper.sh
[root@VM_46_94_centos bin]# ./startup.sh 
Using CATALINA_BASE:   /usr/local/apache-tomcat-8.5.29
Using CATALINA_HOME:   /usr/local/apache-tomcat-8.5.29
Using CATALINA_TMPDIR: /usr/local/apache-tomcat-8.5.29/temp
Using JRE_HOME:        /usr/java/jdk1.8.0_161
Using CLASSPATH:       /usr/local/apache-tomcat-8.5.29/bin/bootstrap.jar:/usr/local/apache-tomcat-8.5.29/bin/tomcat-juli.jar
Tomcat started.
[root@VM_46_94_centos bin]# 

4. 安装Redis

第一步: 将redis-4.0.9.tar.gz安装包复制到/usr/local目录

[root@VM_46_94_centos ~]# cp /usr/java/redis-4.0.9.tar.gz /usr/local

第二步: 解压压缩包

tar -zxvf redis-4.0.9.tar.gz

[root@VM_46_94_centos ~]# cd /usr/local
[root@VM_46_94_centos local]# ls
bin  etc  games  include  lib  lib64  libexec  qcloud  redis-4.0.9.tar.gz  sbin  share  src
[root@VM_46_94_centos local]# tar -zxvf redis-4.0.9.tar.gz

第三步:yum安装gcc依赖

yum install gcc

[root@VM_46_94_centos local]# yum install gcc
Total download size: 45 M
Is this ok [y/d/N]: 遇到选择,输入y即可

第四步:跳转到redis解压目录下

cd redis-4.0.9

[root@VM_46_94_centos local]# cd redis-4.0.9/

第五步:编译安装

make MALLOC=libc

[root@VM_46_94_centos redis-4.0.9]# make MALLOC=libc

    CC siphash.o
    CC rax.o
    LINK redis-server
    INSTALL redis-sentinel
    CC redis-cli.o
    LINK redis-cli
    CC redis-benchmark.o
    LINK redis-benchmark
    INSTALL redis-check-rdb
    INSTALL redis-check-aof

Hint: It's a good idea to run 'make test' ;)

make[1]: Leaving directory `/usr/local/redis-4.0.9/src'

将/usr/local/redis-4.0.9/src目录下的文件加到/usr/local/bin目录: cd src && make install

[root@VM_46_94_centos redis-4.0.9]# 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
[root@VM_46_94_centos src]# 

第六步:测试是否安装成功

  1. 先切换到redis src目录下
[root@VM_46_94_centos redis-4.0.9]# cd src
  1. 直接启动redis: ./redis-server
[root@VM_46_94_centos src]# ./redis-server
3047:C 04 Apr 12:58:30.367 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3047:C 04 Apr 12:58:30.367 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=3047, just started
3047:C 04 Apr 12:58:30.367 # 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.9 (00000000/0) 64 bit
  .-`` .-```.  ```\/    _.,_ ''-._                                   
 (    '      ,       .-`  | `,    )     Running in standalone mode
 |`-._`-...-` __...-.``-._|'` _.-'|     Port: 6379
 |    `-._   `._    /     _.-'    |     PID: 3047
  `-._    `-._  `-./  _.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |           http://redis.io        
  `-._    `-._`-.__.-'_.-'    _.-'                                   
 |`-._`-._    `-.__.-'    _.-'_.-'|                                  
 |    `-._`-._        _.-'_.-'    |                                  
  `-._    `-._`-.__.-'_.-'    _.-'                                   
      `-._    `-.__.-'    _.-'                                       
          `-._        _.-'                                           
              `-.__.-'                                               

3047:M 04 Apr 12:58:30.369 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
3047:M 04 Apr 12:58:30.369 # Server initialized
3047:M 04 Apr 12:58:30.369 # 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.
3047:M 04 Apr 12:58:30.369 # 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.
3047:M 04 Apr 12:58:30.369 * Ready to accept connections

如上图:redis启动成功,但是这种启动方式需要一直打开窗口,不能进行其他操作,不太方便。
按 ctrl + c可以关闭窗口。

  1. 以后台进程方式启动redis(推荐)

    • 第一步: 复制备份redis.conf文件
[root@VM_46_94_centos redis-4.0.9]# mkdir /usr/java/redis
[root@VM_46_94_centos ~]# cp /usr/local/redis-4.0.9/redis.conf /usr/java/redis/
  • 第二步: 修改redis.conf文件
set nu 可出现文件行  :wq保存退出  :q!退出不保存
[root@VM_46_94_centos redis-4.0.9]# vim redis.conf
# 设置不仅限制本地访问 (69行)
bind 127.0.0.1 改为 # bind 127.0.0.1
# 禁用保护模式 (88行)
protected-mode yes 改为 protected-mode no 
# 设置后台启动redis,修改如下,默认为no  (136行)
daemonize no 改为 daemonize yes

注意: 如果在编辑时意外退出,可删除临时文件

[root@VM_46_94_centos redis-4.0.9]# rm .redis.conf.swp
rm: remove regular file ‘.redis.conf.swp’? y
  • 第三步:指定redis.conf文件启动: ./redis-server /usr/local/redis-4.0.9/redis.conf
[root@VM_46_94_centos src]# ./redis-server /usr/local/redis-4.0.9/redis.conf
3721:C 04 Apr 13:11:05.472 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
3721:C 04 Apr 13:11:05.472 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=3721, just started
3721:C 04 Apr 13:11:05.472 # Configuration loaded
  • 第四步:关闭redis进程:首先使用ps -aux | grep redis查看redis进程
[root@VM_46_94_centos src]# ps -aux | grep redis
root      3543  0.0  0.2 151368  5092 pts/1    T    13:07   0:00 vim redis.conf
root      3722  0.0  0.1 141784  1988 ?        Ssl  13:11   0:00 ./redis-server *:6379
root      3799  0.0  0.0 112660   972 pts/1    R+   13:12   0:00 grep --color=auto redis

使用kill命令杀死进程

[root@VM_46_94_centos src]# kill -9 3722

第七步 : 设置redis开机自启动

  1. 在/etc目录下新建redis目录
[root@VM_46_94_centos src]# mkdir /etc/redis
  1. 将/usr/local/redis-4.0.9/redis.conf 文件复制一份到/etc/redis目录下,并命名为6379.conf
[root@VM_46_94_centos redis]# cp /usr/local/redis-4.0.9/redis.conf /etc/redis/6379.conf
  1. 将redis的启动脚本复制一份放到/etc/init.d目录下
[root@VM_46_94_centos redis]# cp /usr/local/redis-4.0.9/utils/redis_init_script /etc/init.d/redisd
  1. 设置redis开机自启动, 先切换到/etc/init.d目录下, 然后执行自启命令
[root@VM_46_94_centos init.d]# chkconfig redisd on
service redisd does not support chkconfig

看结果是redisd不支持chkconfig

解决方法:

使用vim编辑redisd文件,在第一行加入如下两行注释,保存退出

[root@VM_46_94_centos init.d]# vim redisd
#!/bin/sh
# chkconfig:   2345 90 10
# description:  Redis is a persistent key-value database
#
# Simple Redis init.d script conceived to work on Linux systems
# as it does use of the /proc filesystem.

注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。

再次执行开机自启命令,成功

[root@VM_46_94_centos init.d]# chkconfig redisd on

现在可以直接已服务的形式启动和关闭redis了

关闭:
service redisd stop

[root@VM_46_94_centos ~]# service redisd stop
Stopping ...
Could not connect to Redis at 127.0.0.1:6379: Connection refused
Redis stopped

启动:service redisd start 
如果出现如下问题:

[root@VM_46_94_centos ~]# service redisd start
/var/run/redis_6379.pid exists, process is already running or crashed
  • 1:可用安装文件启动redis-server/etc/redis/6379.conf
  • 2:shutdown -r now 软重启让系统自动恢复下就行了
[root@VM_46_94_centos ~]# redis-server /etc/redis/6379.conf
4571:C 04 Apr 13:26:34.675 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
4571:C 04 Apr 13:26:34.675 # Redis version=4.0.9, bits=64, commit=00000000, modified=0, pid=4571, just started
4571:C 04 Apr 13:26:34.675 # Configuration loaded

第八步: 修改Redis密码

修改/etc/redis/6379.conf, /usr/local/redis-4.0.9/redis.conf

# 设置Redis登录密码(500行)
# requirepass foobared 改为 requirepass 123456
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值