Use Redis database storage SessionID on Ubuntu and implement session sharing

Source: Internet
Author: User
Tags redis server

Ubuntu used on Redis Database storage SessionID and implementation Session shared

First part: Installation and configuration Redis Database

installation Redis database server. See also:http://grainier.net/how-to-install-redis-in-ubuntu/

Redis official website: https://redis.io/

$ wget http://download.redis.io/releases/redis-3.2.8.tar.gz

$ tar xzf redis-3.2.8.tar.gz

$ CD redis-3.2.8

$ make

$ make Test

$ cat/sys/kernel/mm/transparent_hugepage/enabled// display current THP enabled status

$ sudo su switches to root and then # echo never >/sys/kernel/mm/transparent_hugepage/enabled toggle THP State. Can add it to your/etc/rc.local on order to retain the setting after a reboot.

$ sudo make install// Copy the compiled executable file to the appropriate place to complete the installation.



The Redis main file functions as follows:

Redis-server:daemon boot program for Redis server

REDIS-CLI:Redis Command-line Operations tool. Of course, you can also use telnet to operate on its plain text protocol.

Redis-benchmark:redis Performance testing tool to test the read and write performance of Redis in your system and in your configuration

Redis-stat:redis Status Detection Tool to detect Redis current status parameters and delay status

Use Help:

$ CD Utils

$ sudo./install_server.sh

set the appropriate parameters ( You can change the default port6379 to another value, such as 6535).

port:6535

Config File:/etc/redis/6535.conf

Log File:/var/log/redis_6535.log

Data dir:/var/lib/redis/6535

Executable:/usr/local/bin/redis-server

Cli executable:/USR/LOCAL/BIN/REDIS-CLI



command to use:



$ sudo service redis_6535 start

$ sudo service redis_6535 stop

$ sudo service redis_6535 restart

$ redis-server--help



Connect via client Redis Server:

$ redis-cli-p 6535// login

$ select 1// Select Database

$ keys *// list all keys

$ get key_name// Get value

Redius security settings:https://www.digitalocean.com/community/tutorials/ how-to-secure-your-redis-installation-on-ubuntu-14-04



Part II: Website Code ( placed in server-side main file www.js)

var session= require (' express-session '),

redisstore= require (' Connect-redis ') (session),// UseRedisDatabase StorageSessionInformation

redis= require ("Redis"),// Use Radis Client Node_redis



//================ set Redis database storage share session==================

rsclient= redis.createclient ({// create redis Client Instance

Host: ' 127.0.0.1 ',

port:6535,

db:1});



//==============Setsession (This article should be placed inPassportbefore)==============

Varsessionmiddleware = Session ({

store:new redisstore ({client:rsclient}),// setting willSessionstored inRedisdatabase Inside

Secret: ' Putyoursecrethere ',// Encrypt string , ' Putyoursecrethere ' can be changed to another string

Resave:true,

Saveuninitialized:true

});

Use Redis database storage SessionID on Ubuntu and implement session sharing

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.