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

后端 未结 13 1403
轻奢々
轻奢々 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:04

    In my case I tried to connect to DB (which was inside docker) like this:

    mysql -ppass -u root
    

    but got same error as OP.

    Specifying host and port helped:

    mysql --host 0.0.0.0 --port 3306 -ppass -u root
    
    0 讨论(0)
  • 2020-12-13 02:05

    My problem was that I was trying to connect from a version of mysql client that seems to be incompatible with the mysql server I installed (mysql:latest which installed version 8.0.22 at the time of this writing).

    my mysql client version:

    $ mysql --version
    mysql  Ver 14.14 Distrib 5.7.26, for Linux (x86_64) using  EditLine wrapper
    

    The docker command that I used to install mysql:latest:

    $ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=somerootpassword -e MYSQL_USER=someuser -e MYSQL_PASSWORD=someuserpassword -d -p 3306:3306 mysql:latest
    

    The errors I got when connecting from my local mysql client to the mysql server:

    $ mysql -u someuser -p -h 127.0.0.1
    ERROR 2026 (HY000): SSL connection error: unknown error number
    

    (sometimes I would get another error: "ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 2". But I think this happens when I try to connect to the server too early after I started it)

    My solution was to install mysql:5.7 instead:

    $ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=somerootpassword -e MYSQL_USER=someuser -e MYSQL_PASSWORD=someuserpassword -d -p 3306:3306 mysql:5.7
    

    and then I can connect to the server (after waiting perhaps 1 minute until the server is ready to accept connections):

    $ mysql -u someuser -p -h 127.0.0.1
    
    0 讨论(0)
  • 2020-12-13 02:08

    Don't know how do i achieve this, but, i've be able to reach MYSQL by typing

    $ mysql -u root -h

    mywebsite:
      image: benftwc/pldev-webserver
      volumes:
        - ./mywebsite.fr/:/var/www/
      working_dir: /var/www/
      ports:
        - "8009:8009"
      command: php -S 0.0.0.0:8009
      links:
        - database
    database:
      image: library/mysql
      environment:
        MYSQL_ROOT_PASSWORD: root
      ports:
        - "3310:3306
    

    root@422f4d1f454a:/# mysql -u root -h 127.0.0.1 -p3310
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
    

    root@422f4d1f454a:/# mysql -u root -h database -p
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or \g...........
    
    0 讨论(0)
  • 2020-12-13 02:10

    Months after this question, I've levelup my Docker skills. I should use Docker container name instead.

    That use dokerized-nginx as bridge to expose ip+port of the container.

    Within WEB configuration, I now use mysql://USERNAME:PASSWORD@docker_container_name/DB_NAME to access to Mysql socket through docker (also works with docker-compose, use compose-name instead of container one)

    0 讨论(0)
  • 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/

    0 讨论(0)
  • I ran into the same issue today, try running ur container with this command.

    docker run --name mariadbtest -p 3306:3306 -e MYSQL_ROOT_PASSWORD=mypass -d mariadb/server:10.3
    
    0 讨论(0)
提交回复
热议问题