Docker 安装 MySQL

1,docker pull mysql

查找Docker Hub上的mysql镜像(https://hub.docker.com/_/mysql/)

Supported tags and respective Dockerfile links
8.0.11, 8.0, 8, latest (8.0/Dockerfile)
5.7.22, 5.7, 5 (5.7/Dockerfile)
5.6.40, 5.6 (5.6/Dockerfile)
5.5.60, 5.5 (5.5/Dockerfile)

这里我们拉取最新的镜像(mysql8.0.11) ,标签是 8,

$ docker pull mysql:8

2,使用mysql镜像

我们在服务器上建个目录:

$mkdir /server/mysql

切换到 /server/mysql 目录下,执行

$ docker run -p 3306:3306 --name mysql8 -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:8
ec06e796f37a3a7f603dadc225643755cce99d77cbb50267d6783be6eda59c35//这里是返回的数据

命令说明:

命令 解释
-p 3306:3306 将容器的 3306 端口映射到主机的 3306 端口
—name mysql8 指定一个容器名
-v $PWD/conf:/etc/mysql/conf.d 将主机当前目录下的 conf/my.cnf 挂载到容器的 /etc/mysql/my.cnf
-v $PWD/logs:/logs 将主机当前目录下的 logs 目录挂载到容器的 /logs
-v $PWD/data:/var/lib/mysql 将主机当前目录下的data目录挂载到容器的 /var/lib/mysql
-e MYSQL_ROOT_PASSWORD=123456 初始化 root 用户的密码
-d 表示使用守护进程运行,即服务挂在后台

3,查看容器启动情况

$ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
ec06e796f37a        mysql8        "docker-entrypoint..."   9 minutes ago       Up 9 minutes        0.0.0.0:3306->3306/tcp  mysql8

4,进入docker mysql

$ docker exec -it mysql8 bash #获取用container Id 也可以登录

5,查看docker mysql 日志

 $sudo docker logs -f mysql8 #mysql8是容器镜像名称
 ***返回***

6,docker mysql 停止、启动和重启

1,停止

$sudo docker stop mysql8

2,启动

$sudo docker start mysql8

3,重启

$sudo docker restart mysql8

常见问题

1,

Error response from daemon: Conflict. The container name “/mysql8” is already in use by container 9068eedde2256c2c768b45487d02fa24dd803349a3f6ec0f6312149eedaa1d14. You have to remove (or rename) that container to be able to reuse that name..
See ‘/usr/bin/docker-current run —help’.

这是因为名字冲突了。

$sudo docker ps -a 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                      PORTS               NAMES
9e6cafa522d6        8d99edb9fd40        "docker-entrypoint..."   5 minutes ago       Exited (1) 5 minutes ago                        awesome_lalande
4af38dd96e72        8d99edb9fd40        "docker-entrypoint..."   7 minutes ago       Exited (1) 7 minutes ago                        sharp_noyce
9068eedde225        mysql               "docker-entrypoint..."   12 minutes ago      Exited (0) 10 minutes ago                       mysql8
9d573e976f1d        8d99edb9fd40        "docker-entrypoint..."   40 hours ago        Exited (1) 40 hours ago                         kind_golick
ec06e796f37a        mysql:latest        "docker-entrypoint..."   41 hours ago        Exited (0) 41 hours ago                         mymysql

可以看出mysql8已经远行了10 minutes
我们把这个删除:

$sudo docker rm mysql8

然后重新运行,启动实例。

2,

0.0.0.0:3306->3306/tcp

如果我们不想外网访问3306端口,可以这么设置:

$ docker run -p ip:3306:3306 --name mysql8 -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql8

查看一下:

127.0.0.1:3306->3306/tcp

** MySQL相关

1,mysql 添加一个用户

>use mysql;
>insert into mysql.user(Host,User,Password)values("localhost","test",password("1234"));

2,mysql授权

>GRANT REPLICATION SLAVE ON *.* to 'root'@'%'; #%最好用内网ip
>flush privileges; //刷新系统权限表

3,修改MySQL配置

我们把容器里的/etc/mysql/conf.d挂接到了当前主机的 $PWD(当前目录)的conf下了,我们在$PWD/conf 下创建一个 my.cnf

* my.cnf

[mysqld]
max_connections=30000

重启docker mysql

$sudo docker restart mysql8

查看修改结果:

SELECT t1.*, VARIABLE_VALUE  FROM performance_schema.variables_info t1         JOIN performance_schema.global_variables t2           ON t2.VARIABLE_NAME=t1.VARIABLE_NAME       WHERE t1.VARIABLE_NAME LIKE 'max_connections'\G
*************************** 1. row ***************************
  VARIABLE_NAME: max_connections
VARIABLE_SOURCE: GLOBAL
  VARIABLE_PATH: /etc/mysql/conf.d/my.cnf
      MIN_VALUE: 1
      MAX_VALUE: 100000
       SET_TIME: NULL
       SET_USER: NULL
       SET_HOST: NULL
 VARIABLE_VALUE: 30000
1 row in set (0.00 sec)

ph.w
ph.w 1年前

已被 0 人挖起

回应
登录 后发表评论
  • 消灭零回应