Docker使用mysql

IT
IT
IT
335
文章
1
评论
2020年5月15日14:04:57 评论 1,084 3240字阅读10分48秒

拉取官方MySQL镜像

[root@instance-7tgaowaa ~]# docker pull mysql:5.6

查看拉取的镜像

[root@instance-7tgaowaa ~]# docker images |grep mysql
mysql 5.6 7edb93321b06 38 hours ago 256MB

拉下来以后大可以按照官方的说明无脑启动,但是外部无法访问,所以绑定端口:

[root@instance-7tgaowaa ~]# docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6 
39d7467ddf2fa5400812cbdbffb67042785fac844ff14df7583c2348659ccf3b

查看运行的镜像

[root@instance-7tgaowaa ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
39d7467ddf2f mysql:5.6 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:3306->3306/tcp mysql
f18fd5277e56 redis "/bin/sh -c 'redis-s…" 44 hours ago Up 44 hours 0.0.0.0:6379->6379/tcp redis

进入容器

[root@instance-7tgaowaa ~]# docker exec -it mysql bash
root@39d7467ddf2f:/# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
root@39d7467ddf2f:/# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.41 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

挂载目录到容器

[vip]

创建用于挂载的目录

[root@instance-7tgaowaa ~]# mkdir -p /home/mysql/datadir 
[root@instance-7tgaowaa ~]# mkdir -p /home/mysql/conf.d

使用镜像创建容器

[root@instance-7tgaowaa ~]# docker run --name mysql -p 3306:3306 -v /home/mysql/datadir:/var/lib/mysql -v /home/mysql/conf.d:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6
88784dd4b1fef7f9117cb2028b7ba5a0ea923790da301c63ce00b0420bc83e73

命令解析:
--name:容器名

--p:映射宿主主机端口

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

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

-d:后台运行容器

查看运行的容器

[root@instance-7tgaowaa ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
88784dd4b1fe mysql:5.6 "docker-entrypoint.s…" 4 seconds ago Up 3 seconds 0.0.0.0:3306->3306/tcp mysql
f18fd5277e56 redis "/bin/sh -c 'redis-s…" 44 hours ago Up 44 hours 0.0.0.0:6379->6379/tcp redis

查看本地目录和挂载的目录

[root@instance-7tgaowaa datadir]# pwd
/home/mysql/datadir
[root@instance-7tgaowaa datadir]# ls
auto.cnf ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema

默认情况下,MySQL的启动配置文件是/etc/mysql/my.cnf,而/etc/mysql/conf.d目录下的存在任何.cnf格式的文件时,都会使用该文件中配置项替换默认配置。

增加一个不区分大小写表的参数

[root@instance-7tgaowaa ~]# cd /home/mysql/conf.d/
[root@instance-7tgaowaa conf.d]# cat my.cnf 
[mysqld]
lower_case_table_names = 1

然后关掉或者清理掉之前启动的mysql容器

docker rm $(docker ps -a -q)

这个命令慎用,否则会删除之前所有关闭掉后的容器ID

重新启用容器

[root@instance-7tgaowaa ~]# docker run --name mysql -p 3306:3306 -v /home/mysql/datadir:/var/lib/mysql -v /home/mysql/conf.d:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6

验证是否不区分大小写

Docker使用mysql

查看日志

[root@instance-7tgaowaa conf.d]# docker logs mysql
2018-08-02 07:41:36 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-08-02 07:41:36 0 [Note] mysqld (mysqld 5.6.41) starting as process 1 ...
2018-08-02 07:41:36 1 [Note] Plugin 'FEDERATED' is disabled.
2018-08-02 07:41:36 1 [Note] InnoDB: Using atomics to ref count buffer pool pages
2018-08-02 07:41:36 1 [Note] InnoDB: The InnoDB memory heap is disabled

时时查看日志信息

[root@instance-7tgaowaa conf.d]# docker logs -f mysql
2018-08-02 07:41:36 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

[/vip]

继续阅读
  • 我的QQ
  • QQ扫一扫
  • weinxin
  • 我的头条
  • 头条扫一扫
  • weinxin
IT
  • 本文由 发表于 2020年5月15日14:04:57
  • 除非特殊声明,本站文章均为原创,转载请务必保留本文链接
Docker部署Apollo Apollo

Docker部署Apollo

安装docker $ cd /opt/ $ sudo git clone --depth=1 https://github.com/cncentoscn/setuptools.git $ cd set...
Docker安装redis Docker

Docker安装redis

下载镜像 # docker pull redis 创建目录 # mkdir -p /home/redis/data # mkdir -p /home/redis/config 从 Redis 官方下载...
Centos8.1安装docker Docker

Centos8.1安装docker

yum方式安装 安装依赖 # yum install -y yum-utils device-mapper-persistent-data lvm2 添加仓库 # yum-config-manager...