安装

查看可用版本

  1. $ docker search mysql
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. mysql MySQL is a widely used, open-source relati... 2529 [OK]
  4. mysql/mysql-server Optimized MySQL Server Docker images. Crea... 161 [OK]
  5. centurylink/mysql Image containing mysql. Optimized to be li... 45 [OK]
  6. sameersbn/mysql 36 [OK]
  7. google/mysql MySQL server for Google Compute Engine 16 [OK]
  8. appcontainers/mysql Centos/Debian Based Customizable MySQL Con... 8 [OK]
  9. marvambass/mysql MySQL Server based on Ubuntu 14.04 6 [OK]
  10. drupaldocker/mysql MySQL for Drupal 2 [OK]
  11. azukiapp/mysql Docker image to run MySQL by Azuki - http:... 2 [OK]
  12. ...

拉取镜像

  1. #当前mysql最新版为8.x
  2. $ docker pull mysql:latest

查看本地镜像

  1. $ docker images

运行容器

  1. # 例如我的宿主机目录在/home/admin/docker,在此目下创建mysql目录(存放相关数据),再执行容器创建
  2. $ mkdir mysql
  3. $ cd mysql

ubuntu

  1. #创建容器
  2. $ 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
  3. #查看创建的容器
  4. $ docker ps

centos7

  1. $ todo...

使用

登录容器

  1. $ docker exec -it mysqlserver bash
  2. #提示输入密码,密码默认123456
  3. $ mysql -u root -p

修改密码

  1. $ use mysql;
  2. $ ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

新增远程访问

  1. #添加远程登录用户
  2. $ CREATE USER 'test'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
  3. $ GRANT ALL PRIVILEGES ON *.* TO 'test'@'%';

刷新

  1. $ 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即可