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

Docker部署时遇到SQLSTATE[HY000] [2002] Connection refused #214

Closed
mobguang opened this issue Nov 30, 2022 · 4 comments
Closed

Docker部署时遇到SQLSTATE[HY000] [2002] Connection refused #214

mobguang opened this issue Nov 30, 2022 · 4 comments

Comments

@mobguang
Copy link

重现步骤:

  1. 执行下列命令:
    docker run -it --rm --name wizard
    -e DB_HOST=host.docker.internal
    -e DB_PORT=3306
    -e DB_DATABASE=wizard
    -e DB_USERNAME=wizard
    -e DB_PASSWORD=wizard
    mylxsw/wizard

  2. 在docker环境中执行:php artisan migrate:install

遇到下列错误:
SQLSTATE[HY000] [2002] Connection refused (SQL: create table migrations (id int unsigned not null auto_increment primary
key, migration varchar(191) not null, batch int not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

哪位大神可以帮忙看一下,可以如何解决,不胜感激!

@mylxsw
Copy link
Owner

mylxsw commented Dec 1, 2022

连不上数据库,检查下你宿主机上的数据库是否启动了,账号密码权限是否已经分配

@mobguang
Copy link
Author

mobguang commented Dec 1, 2022

Hi @mylxsw ,

感谢回复。我后来尝试的步骤如下:

  1. 我在主机上通过docker启动了mysql,并且执行了如下mysql的初始化脚本:
    create database wizard default character set utf8mb4 collate utf8mb4_bin ;
    create user 'wizard'@'%' identified by 'wizard' ;
    grant all privileges on wizard.* to 'wizard'@'%' ;
    flush privileges ;

  2. 然后在本机执行了如下脚本:
    docker run -d --name wizard
    -e DB_HOST=10.8.18.87
    -e DB_PORT=3306
    -e DB_DATABASE=wizard
    -e DB_USERNAME=wizard
    -e DB_PASSWORD=wizard
    -p 8080:80
    -v /Volumes/M2SSD/Space/GuangResourcesPool/Guang_workspace/workspace/wizard/content:/webroot/storage/app/public
    mylxsw/wizard

  3. 执行docker exec -it wizard /bin/bash,进入docker;

  4. 执行php artisan migrate:install,遇到如下error:

SQLSTATE[HY000] [2054] The server requested authentication method unknown to the client (SQL: create table migrations (id
int unsigned not null auto_increment primary key, migration varchar(191) not null, batch int not null) default character
set utf8mb4 collate 'utf8mb4_unicode_ci')

请问是哪一步有问题吗,感谢指导!

@mylxsw
Copy link
Owner

mylxsw commented Dec 1, 2022

这是用的 mysql8 吧,这样,修改下用户信息

alter user 'wizard'@'%' identified with mysql_native_password by 'wizard';

@mobguang
Copy link
Author

mobguang commented Dec 1, 2022

Hi @mylxsw ,

谢谢,可以启动了。

@mobguang mobguang closed this as completed Dec 1, 2022
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