【docker】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 ID
为89c5b9c81e74
错误写法
[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;
刷新数据库