报错:“ERROR 2003: Can't connect to MySQL server on 'localhost' (10061)”
检查:
docker run -p 主机端口:虚拟机端口--name mysql
mkdir /docker
mkdir /docker/mysql
mkdir /docker/mysql/conf
mkdir /docker/mysql/data
创建my.cnf配置文件
touch /docker/mysql/conf/my.cnf
my.cnf添加如下内容:
[mysqld]
init_connect='SET collation_connection = utf8_unicode_ci'
init_connect='SET NAMES utf8'
character-set-server=utf8
collation-server=utf8_unicode_ci
skip-character-set-client-handshake
skip-name-resolve
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
创建容器,启动
docker run \
--restart=always \
--privileged=true \
-p 3306:3306 --name mysql \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/my.cnf:/etc/mysql/my.cnf \
-v /mydata/mysql/conf.d:/etc/mysql/conf.d \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql
参数说明:
docker ps查看启动的服务,ports显示端口已经绑定
如果端口ports为空,则端口没有绑定
移除mysql服务,重新拉镜像
#列出所有容器 ID
docker ps
#停止所有容器
docker stop 容器名
#停止单个容器
docker stop 容器名
#删除所有容器
docker rm 容器名
#删除单个容器
docker rm 容器名
#删除所有的镜像
docker rmi 容器名
如果无法按容器名启动容器,输入docker ps -a查看,最前面的序列就是容器ID,按容器ID启动就行,删除容器同理
先进入看看能不能进去mysql容器中
#第一步
docker exec -it mysql bash
#第二部
mysql -u root -p
#正常流程
[root@localhost ~]# docker exec -it mysql bash
root@eb3dbfb0958f:/# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7 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>
修改权限
方法一:允许root用户从任何主机连接到MYSQL
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '新密码' WITH GRANT OPTION;
方法二:允许root从主机192.168.10.56连接到MYSQL,并使用新密码登录
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.10.56' IDENTIFIED BY '新密码' WITH GRANT OPTION;
刷新权限
flush privileges;
登陆成功
提示:
查看报错日志:docker log 容器ID