search

Home  >  Q&A  >  body text

docker - MySQL 报错:Access denied for user 'xxx'@'localhost'

使用 MySQL 创建一个新的用户,理论上是这样的,
先用 root 用户登录,然后
CREATE USER ‘newuser’@‘localhost' IDENTIFIED BY '12345';
用 docker 搭建的开发环境,数据库主机的名字是 db 不是 localhost,
于是 CREATE USER ‘newuser’@‘db' IDENTIFIED BY '12345';

然后我登录遇到问题了:
mysql -u newuser -p
输入密码后提示
Access denied for user 'newuser'@'localhost' (using password: YES)

好像是主机名的问题,具体因为啥不太清楚,请教一下大家,谢谢

迷茫迷茫2785 days ago649

reply all(4)I'll reply

  • 巴扎黑

    巴扎黑2017-04-17 15:05:55

    Try this one below!

    mysql --host=db -u root -p 

    reply
    0
  • 黄舟

    黄舟2017-04-17 15:05:55

    First use the root account to check if newuser has permission to connect to localhost

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 15:05:55

    localhost in mysql refers to the local machine, not the machine name

    reply
    0
  • 巴扎黑

    巴扎黑2017-04-17 15:05:55

    The problem was solved. I asked a master who is familiar with docker for advice:
    It needs to be created like this:

    CREATE USER 'newuser'@'%' IDENTIFIED BY '12345';

    View users and hosts:
    select host, user, password from mysql.user;

    Return something like this:

    host user password
    % root *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B
    % app *E56A114692FE0DE073F9A1DD68A00EEB9703F3F1
    % newuser *00A51F3F48415C7D4E8908980D443C29C69B60C9

    3 rows in set (0.00 sec)

    reply
    0
  • Cancelreply