使用docker起一个mysql服务

1.拉取最新的mysql

docker pull mysql:latest

 

2. 查看拉取mysql镜像是否成功

docker images    

 

3.创建挂载目录

mkdir -p /usr/mysql/conf  /usr/mysql/data  /usr/mysql/logs
//依次为配置文件,数据,日志

 


4./usr/mysql/conf 目录下定义mysql 配置文件

vim my.cnf //编辑配置文件
------------------



[client] #socket = /usr/mysql/mysqld.sock default-character-set = utf8mb4 [mysqld] pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock datadir = /var/lib/mysql #socket = /usr/mysql/mysqld.sock #pid-file = /usr/mysql/mysqld.pid character_set_server = utf8mb4 collation_server = utf8mb4_bin secure-file-priv= NULL # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 # Custom config should go here !includedir /etc/mysql/conf.d/


----------------------------------

:wq //保存配置文件

 

5.启动容器

docker run -itd --name=mysql -v /usr/mysql/conf/my.cnf:/etc/mysql/my.cnf -v /usr/mysql/logs:/logs -v /usr/mysql/data/mysql:/var/lib/mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql

 

6.进入容器

docker exec -it mysql /bin/bash

 

7.链接mysql

mysql -uroot -p123456

 

8.使用mysql

use mysql;

 

9.修改访问主机密码

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

 

10.刷新

flush privileges

 

 

 

posted @ 2022-01-10 17:28  大楚打码人  阅读(91)  评论(0编辑  收藏  举报