环境
- CentOS 7
- Docker 20.10.12
- MySQL8.0
本文可能解决的问题
- ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2);
- ERROR 1396 (HY000): Operation ALTER USER failed for ‘root’@’%’;
- 2003 - Can’t connect to MySQL server on ‘101.201.225.143’(10060 “Unknown error”) ;
- curl: (56) Recv failure: Connection reset by peer.
- docker run -d 后容器停止.
安装步骤
1. 镜像拉取
docker pull mysql:8.0
2. 在宿主机创建放置mysql的配置文件目录、数据目录和日志目录,并且进行授权
mkdir -p /mydata/mysql/conf /mydata/mysql/data /mydata/mysql/log
chmod -R 755 /mydata/mysql/
3. 创建MySQL配置文件
注:该配置文件可解决这个错误 ERROR 2002 (HY000): Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)。
vim /mydata/mysql/conf/my.cnf
添加以下内容到上述创建的配置文件中
[client]
default-character-set = utf8mb4
[mysqld]
datadir = /mydata/mysql/data
character_set_server = utf8mb4
collation_server = utf8mb4_bin
secure-file-priv= NULL
symbolic-links=0
!includedir /etc/mysql/conf.d/
4. 创建并启动容器
docker run \
--restart=unless-stopped \
-p 3306:3306 \
--name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf/my.cnf:/etc/mysql/my.conf \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:8.0
参数说明:
- –restart=unless-stopped : 可解决执行
docker run -d 后,容器停止的问题;
5. 进入容器
docker exec -it mysql /bin/bash
6. 连接数据库
mysql -uroot -p
注意:看到Enter password时,直接回车,否则会出现这样的错误Access denied for user ‘root’@‘localhost’ (using password: YES)
使用数据库
use mysql;
7. 修改访问主机以及密码等,设置为所有主机可访问
① 查看是否运行远程访问;如果root用户的host为localhost ,要远程访问,需要将它改成%
select host,user,plugin from user;
② 将host改为%
update user set host='%' where user ='root';
③ 刷新权限
flush privileges;
④ 更改连接的密码校验方式(mysql_native_password,mysql8.x版本必须使用这种模式,否则navicate无法正确连接)
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '新密码';
⑤ 刷新权限
flush privileges;
⑥ 使用Navicat远程连接测试
|