如何执行yii迁移时解决错误Exception? [英] How fix the error Exception when execute yii migrate?

查看:73
本文介绍了如何执行yii迁移时解决错误Exception?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在ubuntu 18.04中使用docker运行yii2基本项目.这是我的docker-compose.yml:

I try to run a yii2 basic project with docker in ubuntu 18.04. This is my docker-compose.yml:

version: '2'
services:
  php:
    image: yiisoftware/yii2-php:7.1-apache
    volumes:
      - ~/projects/basic/.composer-docker/cache:/root/.composer/cache:delegated
      - ./:/app:delegated
    ports:
      - 8000:80
    links:
      - mysql

  mysql:
   image: mysql:5.7
   volumes:
      - ~/projects/basic/.composer-docker/mysql:/var/lib/mysql
   environment:
      - "MYSQL_ROOT_PASSWORD=secret"
      - "MYSQL_USER=app"
      - "MYSQL_PASSWORD=secret"
      - "MYSQL_DATABASE=app"
   ports:
    - '33061:3306' 

  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    container_name: phpmyadmin
    restart: always
    environment:
      PMA_HOST: mysql
      MYSQL_USERNAME: app
      MYSQL_ROOT_PASSWORD: secret
  ports:
    - 8080:80
  volumes:
    - /sessions
  links:
    - 'mysql:mysql'

这是config/db.php:

and this is config/db.php:

return [
'class' => 'yii\db\Connection',
'dsn' => 'mysql:host=127.0.0.1;dbname=app',
'username' => 'app',
'password' => 'secret',
'charset' => 'utf8',

我使用与yii2 Advanced中相同的文件夹控制台/迁移.当我运行命令./yii migration时,发生错误异常'yii \ db \ Exception',消息为'SQLSTATE [HY000] [2002]连接被拒绝'".在另一种情况下,当host = mysql时,出现错误异常'yii \ db \ Exception',消息为'SQLSTATE [HY000] [2002] php_network_getaddresses:getaddrinfo失败:名称或服务未知".我做错了什么?

I use the same folder console/migrations like in the yii2 advanced. When I run command ./yii migrate, the error "Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002] Connection refused'" is occured. In the another case when host=mysql I get error "Exception 'yii\db\Exception' with message 'SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known'". What I did wrong?

推荐答案

在使用docker-compose时,将通过服务名称来引用您的主机.因此您需要的是 host = mysql ,而不是 host = 127.0.0.1 .

while using docker-compose your host would be referred by the name of the service. so you would have the host=mysql in your case, not host=127.0.0.1.

如果您运行docker-compose,则不同的服务就像不同的主机.您可以在php(服务名称)应用程序中运行Web应用程序,该应用程序将数据库服务连接到mysql(服务名称)应用程序

If you run docker-compose different services are like different hosts. You run the web app in your php (service name) app which connects to the database service to the mysql (service name) app

这篇关于如何执行yii迁移时解决错误Exception?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆