redis安装和配置

安装

1,去官网(http://redis.cn/ )下载redis-5.0.4.tar.gz

2,安装

$ tar xzf redis-5.0.4.tar.gz
$ cd redis-5.0.4
$ make

3,进入到解压后的 src 目录,通过如下命令启动Redis:

$ src/redis-server

4,您可以使用内置的客户端与Redis进行交互:

$ src/redis-cli
redis> ping
PONG
redis> ping hello
"hello"

5,远程访问redis-server,需要指定redis-server所在主机的IP和端口号

$ src/redis-cli -h 192.168.x.x -p 6380

6,在客户端关闭redis server

$ 127.0.0.1:6379> SHUTDOWN

设置

配置文件所在的路径:解压redis-5.0.4.tar.gz后,的根目录下。

文件名:redis.conf

1,设置服务器端是否让服务器主机以外的客户端访问它

  • 只允许服务器主机内的客户端访问

    # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
    # JUST COMMENT THE FOLLOWING LINE.
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    bind 127.0.0.1
    
    # By default protected mode is enabled. You should disable it only if
    # you are sure you want clients from other hosts to connect to Redis
    # even if no authentication is configured, nor a specific set of interfaces
    # are explicitly listed using the "bind" directive.
    protected-mode yes
    
    
  • 允许服务器主机以外的客户端访问

    # IF YOU ARE SURE YOU WANT YOUR INSTANCE TO LISTEN TO ALL THE INTERFACES
    # JUST COMMENT THE FOLLOWING LINE.
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # bind 127.0.0.1
    
    
    # By default protected mode is enabled. You should disable it only if
    # you are sure you want clients from other hosts to connect to Redis
    # even if no authentication is configured, nor a specific set of interfaces
    # are explicitly listed using the "bind" directive.
    protected-mode no
    

进行上述设置后,在别的主机应该就能访问redis服务器了,但是如果还是访问不了,就需要看看防火墙的设置。一般在ubuntu下,进行上述设置后,在别的主机应该就能访问redis服务器了。

2,是否让redis服务器以守护进程的方式启动

  • 不让redis服务器以守护进程的方式启动

    # By default Redis does not run as a daemon. Use 'yes' if you need it.
    # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
    daemonize no
    
  • 让redis服务器以守护进程的方式启动,并且把守护进程的pid写入到文件:/var/run/redis.pid中

    # By default Redis does not run as a daemon. Use 'yes' if you need it.
    # Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
    daemonize yes
    

3,设置记录守护进程的pid的文件

# Creating a pid file is best effort: if Redis is not able to create it
# nothing bad happens, the server will start and run normally.
pidfile /var/run/redis_6379.pid

4,设置log的级别和log文件所在的目录。如果想把日志不写入日志文件,而打印到终端的话,

【logfile stdout】

# Specify the server verbosity level.
# This can be one of:
# debug (a lot of information, useful for development/testing)
# verbose (many rarely useful info, but not a mess like the debug level)
# notice (moderately verbose, what you want in production probably)
# warning (only very important / critical messages are logged)
loglevel notice

# Specify the log file name. Also the empty string can be used to force
# Redis to log on the standard output. Note that if you use standard
# output for logging but daemonize, logs will be sent to /dev/null
logfile "/目录/文件名"

5,设置数据库的个数

# Set the number of databases. The default database is DB 0, you can select
# a different one on a per-connection basis using SELECT <dbid> where
# dbid is a number between 0 and 'databases'-1
databases 16

在客户端,可以用【select 数据库标号】,来选定要操作的数据库,从0开始,默认是0号数据库。

6,设置持久化类型。

  • rdb方式:默认方式

    • 特点:
      • 把内存中数据直接存储在磁盘
      • 按照一定的频率做数据存储
    • 缺点:
      • 存储频率设定的高的话,效率就会低
      • 存储频率设定的低的话,同步就不及时
    • 优点:数据恢复快
  • aof方式:

    • 特点:把每个命令直接存储在磁盘

    • 缺点:数据恢复慢

    • 优点:

      • 数据存储到磁盘效率高,因为存的是命令,不是数据。
      • 占用磁盘空间小。
  • 设定rdb方式的存储频率

    #   In the example below the behaviour will be to save:
    #   after 900 sec (15 min) if at least 1 key changed
    #   after 300 sec (5 min) if at least 10 keys changed
    #   after 60 sec if at least 10000 keys changed
    #
    #   Note: you can disable saving completely by commenting out all "save" lines.
    #
    #   It is also possible to remove all the previously configured save
    #   points by adding a save directive with a single empty string argument
    #   like in the following example:
    #
    #   save ""
    
    save 900 1
    save 300 10
    save 60 10000
    
  • 设定rdb的文件名

    # The filename where to dump the DB
    dbfilename dump.rdb
    
  • 设定rdb的路径

    # The working directory.
    #
    # The DB will be written inside this directory, with the filename specified
    # above using the 'dbfilename' configuration directive.
    #
    # The Append Only File will also be created inside this directory.
    #
    # Note that you must specify a directory here, not a file name.
    dir ./
    
  • 启动AOF,并设置aof的文件名。注意:要把rdb方式disable掉。

    ############################## APPEND ONLY MODE ###############################
    
    # By default Redis asynchronously dumps the dataset on disk. This mode is
    # good enough in many applications, but an issue with the Redis process or
    # a power outage may result into a few minutes of writes lost (depending on
    # the configured save points).
    #
    # The Append Only File is an alternative persistence mode that provides
    # much better durability. For instance using the default data fsync policy
    # (see later in the config file) Redis can lose just one second of writes in a
    # dramatic event like a server power outage, or a single write if something
    # wrong with the Redis process itself happens, but the operating system is
    # still running correctly.
    #
    # AOF and RDB persistence can be enabled at the same time without problems.
    # If the AOF is enabled on startup Redis will load the AOF, that is the file
    # with the better durability guarantees.
    #
    # Please check http://redis.io/topics/persistence for more information.
    
    appendonly yes
    
    # The name of the append only file (default: "appendonly.aof")
    
    appendfilename "appendonly.aof
    
  • 设定AOF的存储频率

    # Redis supports three different modes:
    #
    # no: don't fsync, just let the OS flush the data when it wants. Faster.
    # always: fsync after every write to the append only log. Slow, Safest.
    # everysec: fsync only one time every second. Compromise.
    #
    # The default is "everysec", as that's usually the right compromise between
    # speed and data safety. It's up to you to understand if you can relax this to
    # "no" that will let the operating system flush the output buffer when
    # it wants, for better performances (but if you can live with the idea of
    # some data loss consider the default persistence mode that's snapshotting),
    # or on the contrary, use "always" that's very slow but a bit safer than
    # everysec.
    #
    # More details please check the following article:
    # http://antirez.com/post/redis-persistence-demystified.html
    #
    # If unsure, use "everysec".
    # appendfsync always
    appendfsync everysec
    # appendfsync no
    
    • appendfsync always:随时存储
    • appendfsync everyse:1秒存储一次
    • appendfsync no:不存储。

hiredis的安装

$ unzip hiredis-master.zip
$ cd hiredis-master
$ make
$ sudo make install

hiredis是用C语言操作redis数据库的API库

hiredis的下载地址:

https://github.com/redis/hiredis

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

posted @ 2019-08-27 18:42  小石王  阅读(289)  评论(0编辑  收藏  举报