安装
查看可用版本
$ docker search mysql
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
mysql MySQL is a widely used, open-source relati... 2529 [OK]
mysql/mysql-server Optimized MySQL Server Docker images. Crea... 161 [OK]
centurylink/mysql Image containing mysql. Optimized to be li... 45 [OK]
sameersbn/mysql 36 [OK]
google/mysql MySQL server for Google Compute Engine 16 [OK]
appcontainers/mysql Centos/Debian Based Customizable MySQL Con... 8 [OK]
marvambass/mysql MySQL Server based on Ubuntu 14.04 6 [OK]
drupaldocker/mysql MySQL for Drupal 2 [OK]
azukiapp/mysql Docker image to run MySQL by Azuki - http:... 2 [OK]
...
拉取镜像
#当前mysql最新版为8.x
$ docker pull mysql:latest
查看本地镜像
$ docker images
运行容器
# 例如我的宿主机目录在/home/admin/docker,在此目下创建mysql目录(存放相关数据),再执行容器创建
$ mkdir mysql
$ cd mysql
ubuntu
#创建容器
$ docker run --name mysqlserver -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d -i -p 3306:3306 mysql:latest
#查看创建的容器
$ docker ps
centos7
$ todo...
使用
登录容器
$ docker exec -it mysqlserver bash
#提示输入密码,密码默认123456
$ mysql -u root -p
修改密码
$ use mysql;
$ ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
新增远程访问
#添加远程登录用户
$ CREATE USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
$ GRANT ALL PRIVILEGES ON *.* TO 'test'@'%';
刷新
$ flush privileges;
客户端
使用 navicat premium 进行连接
可能产生的问题
:::info
2002 - Can’t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock’
:::
本地主机ip地址不要用 localhost,填入127.0.0.1即可