Docker Mysql

Mysql:8.0

1.搜索mysql镜像

1
docker search mysql

2.拉镜像

1
docker pull mysql:8.0

3.运行mysql镜像

1
docker run  --restart=always --name mysql -p 3306:3306 -v /usr/local/lib/data:/var/lib/mysql -v /usr/local/lib/conf.d:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD='zmkj123!@#' -e TZ=Asia/Shanghai -d mysql:8.0             

Mariadb

1
docker run  --restart=always --name mysql -p 3306:3306 -v /Users/nick/work/lib/mysql/data:/var/lib/mysql -v /Users/nick/work/lib/mysql/conf.d:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD='zmkj123!@#' -d mariadb

启动镜像

命令解析:

–name:容器名

–p:映射宿主主机端口

-v:挂载宿主目录到容器目录

-e:设置环境变量,此处指定root密码

-d:后台运行容器

4.查看运行mysql容器

1
docker ps

运行容器列表

5.进入容器内部

1
docker exec -it 7a036187d7b9 /bin/sh        

6.连接mysql

1
mysql -uroot -p

输入密码

登录

输入mysql查询语句

1
mysql> select host,user,plugin,authentication_string from mysql.user;              

user列表

7.修改mysql的访问ip

‘%’表示任何ip都可以访问

1
mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';         

Mysql-server:8.0

拉取镜像

1
docker pull mysql/mysql-server:8.0

启动容器

1
docker run  --restart=always --name=mysql -e MYSQL_ROOT_PASSWORD='tim123' -p 3306:3306 --volume=/Users/tim/work/mysql/conf.d:/etc/mysql/conf.d --volume=/Users/tim/work/mysql/data:/var/lib/mysql mysql/mysql-server:8.0

进入容器

1
docker exec -it <ContainerId> /bin/sh

授权全程访问

1
2
3
#授权登录
CREATE USER 'root'@'%' IDENTIFIED BY 'tim123';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
Donate
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.

请我喝杯咖啡吧~

支付宝
微信