yii2 redis错误:Database Exception – yii\db\Exception:Redis error: NOAUTH Authentication required.

  1. question

When using the yii2 framework, redis operations need to be used, and redis-related data can be configured in main.php and main-local.php, as follows:

Redis related configuration in main.php

 'cache' => [
    'class' => 'yii\redis\Cache',
    'redis' => [
        'hostname' => '192.168.1.1111',
        'port' => 6389,
        'database' => 0,
        'password' => '123456'
    ],
],

Redis related configuration in main-local.php

 'cache' => [
    'class' => 'yii\redis\Cache',
    'redis' => [
        'hostname' => '192.168.1.1111',
        'port' => 6389,
        'database' => 0,
        'password' => 'xxxxx'
    ],
],

The cache configuration in main-local.php will overwrite the cache configuration in main.php. When running the project, an error may occur:

Redis error: NOAUTH Authentication required.
  1. Error analysis and solutions

(1). Whether the configuration is correct

It may be caused by the corresponding redis configuration error in main-local.php, check the corresponding reids configuration: hostname, port, database, and password are configured correctly

Solution:

Confirm again whether the redis configuration is correct, restart the redis program after checking, and revisit the project to see the result

(2). Is redis started?

Check whether redis is started and whether you can enter the redis server

Solution:

Download reids-server, redis-cli, after starting reids-server, run the command: redis-cli -h {ip} -p {port} -a{password} to see if you can enter redis

(3). The password is invalid

There is such a problem: When the project is running online, when using the redis of the cloud service , some cloud service redis can only be accessed on the corresponding intranet (for example: Amazon aws ), and there is no password (because it can only be accessed in the internal network) network access, so when aws opens redis, there is no password), and it can only be configured and accessed through the provided terminal node . Therefore, when the project is configured, there is no configured password, as follows:

Redis related configuration in main-local.php

 'cache' => [
    'class' => 'yii\redis\Cache',
    'redis' => [
        'hostname' => 'xxx', //亚马逊aws redis终端节点
        'port' => 6389,
        'database' => 0,
        //'password' => '',  // 不配置密码,或者密码为空
    ],
],

At this time, access the project and appear:

Redis command was: AUTH 123456
The reason for this is: The password in the corresponding redis configuration in main.php is 123456, which covers the password in main-local.php. Why is this happening? This is because: in the entry file index.php of yii2,
the yii\helpers\ArrayHelper:: merge () function is used

Entry file index.php code:

<?php

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require __DIR__ . '/../../vendor/autoload.php';
require __DIR__ . '/../../vendor/yiisoft/yii2/Yii.php';
require __DIR__ . '/../../common/config/bootstrap.php';
require __DIR__ . '/../config/bootstrap.php';

$config = yii\helpers\ArrayHelper::merge(
    require __DIR__ . '/../../common/config/main.php',
    require __DIR__ . '/../../common/config/main-local.php',
    require __DIR__ . '/../config/main.php',
    require __DIR__ . '/../config/main-local.php'
);

(new yii\web\Application($config))->run();
It will overwrite the attributes existing in main.php in main-local.php , but if a key does not exist in an array, it will be added directly, and if the value corresponding to the key is empty, it will be used in main.php Corresponding to the value of the key, the final configuration at this time is 'password' => '123456', using the 'password' in main.php

Solution:

In the main.php configuration, delete the password item .

Guess you like

Origin blog.csdn.net/zhoupenghui168/article/details/131324327