PHP Operation Redis (i) PHP connection to Redis with Redis password authentication, specifying a REDIS database

Source: Internet
Author: User

200 Redis instances on a server, and it crashes. The only way to do this is to make different types of data belong to different applications and separate from each other.

So, is there a way for Redis to keep different application data separate from each other and stored on the same instance? is equivalent to the MySQL database, different application data is stored under different databases.

Under Redis, a database is identified by an integer index, not by a database name. By default, one client connects to database 0. The following parameters in the Redis configuration file control the total number of databases:

1 Databases

You can switch to a different database by using the following command

12 Redis> Select 2 OK

Subsequently, all commands will use database 3, knowing that you explicitly switch to another database.

Each database has its own space, and there is no need to worry about key conflicts.

Under different databases, the same key is taken to the respective value.

The FLUSHDB command clears the data and clears only the data under the current database, without affecting other databases.

The Flushall command clears the data for this instance. Take extra care before executing this command.

The number of databases is configurable and is 16 by default. Modify the Databases directive under redis.conf:

1 Databases

Redis does not provide any way to correlate the identities of different databases. Therefore, you need to keep track of what data is stored under which database.

As a result, the above 200-instance scenario can be stored using a different database instead of having so many instances open.

Reprint please specify from Operation Survival time: http://www.ttlsa.com/html/4010.html

---

In practical programming, Redis shard operations, referencing the Jedis jar package

    1. Import Redis.clients.jedis.JedisShardInfo;
    2. Jedisshardinfo shard = new Jedisshardinfo (Node.gethost (), Node.getport (), Node.getname ());
    3. Shard.setpassword (Node.getpassword ());
    4. Shard.setdb (Dbindex);
    5. Shards.add (shard);

Here the Shard.setdb (2) corresponds to the input "select 2" in the console Select Database 2 (starting from database 0)

<?php
$redis = new Redis ();
$redis->connect (' 127.0.0.1 ', 6379); Connect to Redis
$redis->auth (' mypasswords123sdfeak '); Password verification
$redis->select (2);//Select Database 2
$redis->set ("TestKey", "Hello Redis"); Set Test key
echo $redis->get ("TestKey");//Output value
?>

PHP Operation Redis (i) PHP connection to Redis with Redis password authentication, specifying a REDIS database

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.