root1@ubuntu:~$ mysql -uroot -p
Enter password:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2 "No such file or directory")
1、先创建好mysql容器
sudo docker run -p 3306:3306 --name mysql -v /opt/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest
2、进入容器
sudo docker exec -it mysql /bin/bash
3、登陆容器内数据库
mysql -u root -p 设置的密码
网友提供解决方案如下:
原创 创建时间:2018-12-29 访问量:532 赞:0 踩:0
1、具体所错与下所示:
[user@cluster2 ~]$ mysql -uroot -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包就可以了。具体参考百度。