Installing MySQL in Docker fails with error message “Can't connect to local MySQL server through socket”

后端 未结 13 1475
轻奢々
轻奢々 2020-12-13 01:53

I\'m trying to install mysql inside a docker container,Tried various images from github, it seems they all manage to successfully install the mysql but when I try to run the

13条回答
  •  臣服心动
    2020-12-13 02:13

    Check out what's in your database.yml file. If you already have your plain Rails app and simply wrapping it with Docker, you should change (inside database.yml):

    socket: /var/run/mysqld/mysqld.sock #just comment it out
    

    to

    host: db 
    

    where db is the name of my db-service from docker-compose.yml. And here's my docker-compose.yml:

    version: '3'
    services:
      web:
        build: .
        command: bundle exec rails s -p 3000 -b '0.0.0.0'
        volumes:
          - .:/myapp
        ports:
          - "3000:3000"
        links:
          - db
      db:
        image: mysql:5.7
        environment:
          MYSQL_ROOT_PASSWORD: root
    

    You start your app in console (in app folder) as docker-compose up. Then WAIT 1 MINUTE (let your mysql service to completely load) until some new logs stop appearing in console. Usually the last line should be like

    db_1 | 2017-12-24T12:25:20.397174Z 0 [Note] End of list of non-natively partitioned tables

    Then (in a new terminal window) apply:

    docker-compose run web rake db:create
    

    and then

    docker-compose run web rake db:migrate
    

    After you finish your work stop the loaded images with

    docker-compose stop
    

    Don't use docker-compose down here instead because if you do, you will erase your database content.

    Next time when you want to resume your work apply:

    docker-compose start
    

    The rest of the things do exactly as explained here: https://docs.docker.com/compose/rails/

提交回复
热议问题