美文网首页
【docker】docker下进入mysql命令行

【docker】docker下进入mysql命令行

作者: 前端菜篮子 | 来源:发表于2020-03-13 10:33 被阅读0次

    docker下进入mysql命令行

    [root@VM_0_8_centos ~]# docker ps
    CONTAINER ID        IMAGE                                COMMAND                  CREATED             STATUS              PORTS                                               
    89c5b9c81e74        hub.c.163.com/library/mysql:latest   "docker-entrypoint..."   3 months ago        Up 3 months         0.0.0.0:3306->3306/tcp  
    

    docker ps查看到mysql在运行 CONTAINER ID89c5b9c81e74

    错误写法

    [root@VM_0_8_centos ~]# docker exec -it bash 89c5b9c81e74        
    Error response from daemon: No such container: bash
    

    正确写法

    [root@VM_0_8_centos ~]# docker exec -it 89c5b9c81e74  bash
    root@89c5b9c81e74:/# mysql -u root -p
    
    Enter password: 
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 141019
    Server version: 5.7.18 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> 
    

    本地宿主机通过mysql命令连接mysql Docker容器中的服务器报错 ERROR 2002 (HY000)

    1、具体所错与下所示:

    [user@cluster2 ~]$ mysql -u root -p
    Enter password:
    ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
    

    2、根据报错提示,是本地mysql连接服务器时,没有找到/var/lib/mysql/mysql.sock文件。那么从这入手,我们看到mysql容器中的服务器启动后的mysql.sock文件在哪。

    我们进入mysql容器中看看:

    sudo docker exec -it mysql /bin/bash
    

    查看mysql配置文件my.cnf

    root@9e313681fc3e:/# cat /etc/mysql/my.cnf
    

    我们看到下面的一些配置:

    [mysqld]
    pid-file        = /var/run/mysqld/mysqld.pid
    socket          = /var/run/mysqld/mysqld.sock
    datadir         = /var/lib/mysql
    secure-file-priv= NULL
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    

    这里可以看到mysqld.sock的目录是在/var/run/mysqld目录下,但是这个目录,我们并没有挂载主机目录,下面我们重新运行mysql容器,挂载相应的容器,如下所示:

    sudo docker run --name=mysql -it -p 3306:3306 -v /opt/data/mysql/mysqld:/var/run/mysqld -v /opt/data/mysql/db:/var/lib/mysql -v /opt/data/mysql/conf:/etc/mysql/conf.d -v /opt/data/mysql/files:/var/lib/mysql-files -e MYSQL_ROOT_PASSWORD=123456 --privileged=true -d mysql
    

    使用以上命令运行mysql容器,这里使用/opt/data/mysql中的相关目录挂载到mysql容器中,启动容器之后你会发现mysql服务器无法正常启动,报错如下:

    2018-12-29T06:25:09.480604Z 0 [ERROR] [MY-010273] [Server] Could not create unix socket lock file /var/run/mysqld/mysqld.sock.lock.
    2018-12-29T06:25:09.480619Z 0 [ERROR] [MY-010268] [Server] Unable to setup unix socket lock file.
    2018-12-29T06:25:09.480625Z 0 [ERROR] [MY-010119] [Server] Aborting
    2018-12-29T06:25:11.289786Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.13)  MySQL Community Server - GPL.
    [user@cluster2 mysql]$ sudo docker logs mysql
    2018-12-29T06:25:09.095301Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
    2018-12-29T06:25:09.095434Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.13) starting as process 1
    2018-12-29T06:25:09.478963Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
    2018-12-29T06:25:09.480604Z 0 [ERROR] [MY-010273] [Server] Could not create unix socket lock file /var/run/mysqld/mysqld.sock.lock.
    2018-12-29T06:25:09.480619Z 0 [ERROR] [MY-010268] [Server] Unable to setup unix socket lock file.
    2018-12-29T06:25:09.480625Z 0 [ERROR] [MY-010119] [Server] Aborting
    2018-12-29T06:25:11.289786Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.13)  MySQL Community Server - GPL.
    

    说是无法创建:Could not create unix socket lock file /var/run/mysqld/mysqld.sock.lock。这说明本地的/opt/data/mysql/mysqld目录,mysql容器中无权限防问。

    注意:这里的/opt/data/mysql目录下的所示目录都是mysql容器启动时自动创建的,这里只有db目录的用户和组是:polkitd input,其它的就是root root,因此,我们要将其它几个目录的用户和组都改成polkitd input。命令如下:

    cd /opt/data
    sudo chown -R polkitd:input mysql
    

    这样,删除mysql docker 容器。重新创建:

    sudo docker rm mysql
    sudo docker run --name=mysql -it -p 3306:3306 -v /opt/data/mysql/mysqld:/var/run/mysqld -v /opt/data/mysql/db:/var/lib/mysql -v /opt/data/mysql/conf:/etc/mysql/conf.d -v /opt/data/mysql/files:/var/lib/mysql-files -e MYSQL_ROOT_PASSWORD=123456 --privileged=true -d mysql
    

    这样容器中的mysql就能正常运行了。

    3、本地主机连接容器的mysql时,需要查到 /var/lib/mysql/mysql.sock。我们启动mysql容器后,在/opt/data/mysql/mysqld目录下有一个mysqld.sock。我们要把这个文件链接到本地主机的var/lib/myql目录中。

    sudo ln -s /opt/data/mysql/mysqld/mysqld.sock /var/lib/mysql/mysql.sock
    

    这样在运行mysql -uroot -p输入密码就能正常连接,docker容器中的mysql服务了。

    4、本地主机只需要安装mysql-community-client包就可以了。具体参考百度。

    MySQL登录时出现Access denied for user 'root'@'localhost' (using password: YES) 拒绝访问,并可修改MySQL密码

    键入mysql –uroot –p;直接回车(Enter),无密登录,然后修改密码
    update mysql.user set authentication_string='123' where user='root'

    设置密码成功...mysql> flush privileges;刷新数据库

    相关文章

      网友评论

          本文标题:【docker】docker下进入mysql命令行

          本文链接:https://www.haomeiwen.com/subject/yrafshtx.html