Implementation of mysql architecture cache in redis server environment

Keywords: Linux Operation & Maintenance Redis bash

Build an instance based on memcache:

web(php) host: 192.168.181.6

redis host: 192.168.181.5

mysql host: 192.168.181.4

First, close the firewall of the three hosts

systemctl stop firewalld

systemctl disable firewalld

sed -i 's/enforcing/disabled/'/etc/selinux/config

setenforce 0

Synchronization time

yum -y install ntp ntpdate

ntpdate cn.pool.ntp.org

hwclock --systohc

  1. On the web server side, install the plug-in:
  2. yum install php php-fpm php-cli php-common php-gd php-mbstring php-mysql php-pdo php-devel php-xmlrpc php-xml php-bcmath php-dba php-enchant
  3. yum install gcc gcc-c++
  4. Install the redis extension of php:  

4. Unzip the compressed package into a file;  

5. Then unzip the tar -zxvf file in the directory and enter the directory, cd redis-2.2.7/

7 execute the command:. / configure

  1. Execute make   install may have an error (atal error: zend_smart_str.h: No such file or directory

It is an error that the redis module in php is not loaded (you need to download the plug-in)

Use the following command: wget http://pecl.php.net/get/redis-2.2.7.tgz

Put the downloaded package into the same directory as above

8. Then decompress: tar -zxvf redis-2.2.7.tgz

  1. cd redis-2.2.7/
  2. ./configure --with-php-config=/usr/bin/php-config

Then execute the make command, and the error will not be reported,

10. After compiling, you can see the installation directory. After entering, you can see the compiled module redis.so

 

  1. Use vim to modify the configuration file/ etc/php.ini; Add: extension=redis.so at the end of the file
  2. Restart the httpd service of the web server; systemctl restart httpd
  3. Create php page code in / var/www/html Directory:
  4. <?php
    phpinfo();
    ?>
    

  5. . then visit

 

Then access the page in the browser under window: the following page indicates that the previous configuration is successful

//

Install redis on the redis host:

yum install -y gcc gcc-c++

wget -c -t 0 http://download.redis.io/releases/redis-2.8.19.tar.gz

tar xvf redis-2.8.19.tar.gz   (any directory)

cd redis-2.8.19

make 

After installation, the directory is as follows:

 

You also need to modify the redis configuration file:

In the redis.conf file: change the daemon no to Yes (indicates that background startup is enabled.)

At the same time, comment out the line in the file as shown in the figure

Then start redis in the background:

Command: Src / redis server redis.conf

If there are errors, please refer to another article: (1 message) Redis installation test_ Wu's blog - CSDN bloghttps://blog.csdn.net/qq_59369367/article/details/121596185

If no error is reported during startup, the configuration is correct

Go to the web host: create the redis.php test page in the / var/www/html / Directory:

 

The code is as follows:

<?php

        $redis = new Redis();

        $redis->connect('192.168.181.5',6379) or die ("could net connect redis server");

        $query = "select * from  employee5  limit 8";    //Here is the table name in your database

        //For simplicity, eight pieces of data are read here

        for ($key = 1; $key < 9; $key++)

        {

                if (!$redis->get($key))

                {

                        $connect = mysql_connect('192.168.181.4','root','Nebula@123');

//Here is your data, ip address, user name and password

                        mysql_select_db(test);    //Here is your database name

                        $result = mysql_query($query);

                        //If $key is not found, the result of the query sql will be cached in redis

                        while ($row = mysql_fetch_assoc($result))

                        {

                                $redis->set($row['id'],$row['name']);

                        }

                        $myserver = 'mysql';

                        break;

                }

                else

{

                        $myserver = "redis";

                        $data[$key] = $redis->get($key);

                }

        }

        echo $myserver;

        echo "<br>";

        for ($key = 1; $key < 9; $key++)

        {

                echo "number is <b><font color=#FF0000>$key</font></b>";

                echo "<br>";

                echo "name is <b><font color=#FF0000>$data[$key]</font></b>";

                echo "<br>";

        }

?>

Then go to the browser to access the page:

First access: (the data will be accessed directly from the mysql database first.)

 

When you refresh the access again: at this time, redis already has relevant data, which will show that the content in redis is in the form of key value pairs

 

Here is the successful test link

Troubleshooting: it is easy to report errors at the last link:

In most cases, the firewall of the database host, selinux, must be turned off because of the firewall

The database user you use must have access to the database you view,

Use this command: select user,host,plugin from mysql.user;

You can view the given permissions,

 

Posted by True`Logic on Sun, 28 Nov 2021 17:09:09 -0800