Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

最新v3.0.4 用mongo子账号提示Authentication Failed #96

Open
hbh112233abc opened this issue Jun 26, 2019 · 2 comments
Open

最新v3.0.4 用mongo子账号提示Authentication Failed #96

hbh112233abc opened this issue Jun 26, 2019 · 2 comments

Comments

@hbh112233abc
Copy link

用mongo子账号在客户端可以连接到某个db的,所以权限没问题的,同样的问题在以下链接看到了
tp5 mongodb Authentication failed(认证失败)
建议connector/Mongo.php 135行dsn增加database的配置
修改前:

$config['dsn'] = 'mongodb://' . ($config['username'] ? "{$config['username']}" : '') . ($config['password'] ? ":{$config['password']}@" : '') . $config['hostname'] . ($config['hostport'] ? ":{$config['hostport']}" : '');

修改后

$config['dsn'] = 'mongodb://' . ($config['username'] ? "{$config['username']}" : '') . ($config['password'] ? ":{$config['password']}@" : '') . $config['hostname'] . ($config['hostport'] ? ":{$config['hostport']}" : '').($config['database'] ? "/{$config['database']}" : '');
@liu21st
Copy link
Member

liu21st commented Jun 26, 2019

#79

@hbh112233abc
Copy link
Author

根据 #79 的建议增加params参数问题解决,不过感觉不是很方便,容易踩坑,希望下次踩坑的同学看到这个讨论,最后database.php配置如下:

<?php
use think\facade\Env;

return [
    // 默认使用的数据库连接配置
    'default'         => Env::get('database.driver', 'mysql'),

    // 数据库连接配置信息
    'connections'     => [
        'mysql' => [
            ...
        ],

        // 更多的数据库配置信息
        'mongo' => [
            // 数据库类型
            'type'     => '\think\db\connector\Mongo',
            // 设置查询类
            'query'    => '\think\db\Mongo',
            // 服务器地址
            'hostname' => Env::get('mongo.hostname', '127.0.0.1'),
            // 集合名
            'database' => Env::get('mongo.database', 'efiles'),
            // 用户名
            'username' => Env::get('mongo.username', 'efile'),
            // 密码
            'password' => Env::get('mongo.password', ''),
            // 端口
            'hostport' => Env::get('mongo.hostport', 27017),
            'params'   => [
                'authSource' => Env::get('mongo.database', 'efiles'),
            ],
        ],
    ],

    // 自定义时间查询规则
    'time_query_rule' => [],
    // 自动写入时间戳字段
    'auto_timestamp'  => 'timestamp',
    // 时间字段取出后的默认时间格式
    'datetime_format' => 'Y-m-d H:i:s',
];

最主要的是在mongo的连接配置增加:

'params'   => [
       'authSource' => Env::get('mongo.database', 'efiles'),
],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants