docker安装mysql

Posted Demonson

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了docker安装mysql相关的知识,希望对你有一定的参考价值。

1.查看镜像源

docker search mysql

NAME                              DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
mysql                             MySQL is a widely used, open-source relation…   11028               [OK]                
mariadb                           MariaDB Server is a high performing open sou…   4178                [OK]                
mysql/mysql-server                Optimized MySQL Server Docker images. Create…   820                                     [OK]
centos/mysql-57-centos7           MySQL 5.7 SQL database server                   88                                      
mysql/mysql-cluster               Experimental MySQL Cluster Docker images. Cr…   87                                      
centurylink/mysql                 Image containing mysql. Optimized to be link…   59                                      [OK]
bitnami/mysql                     Bitnami MySQL Docker Image                      52                                      [OK]
databack/mysql-backup             Back up mysql databases to... anywhere!         44                                      
deitch/mysql-backup               REPLACED! Please use http://hub.docker.com/r…   41                                      [OK]
prom/mysqld-exporter                                                              39                                      [OK]
tutum/mysql                       Base docker image to run a MySQL database se…   35                                      
linuxserver/mysql                 A Mysql container, brought to you by LinuxSe…   30                                      
schickling/mysql-backup-s3        Backup MySQL to S3 (supports periodic backup…   29                                      [OK]
centos/mysql-56-centos7           MySQL 5.6 SQL database server                   20                                      
circleci/mysql                    MySQL is a widely used, open-source relation…   20                                      
mysql/mysql-router                MySQL Router provides transparent routing be…   20                                      
arey/mysql-client                 Run a MySQL client from a docker container      17                                      [OK]
fradelg/mysql-cron-backup         MySQL/MariaDB database backup using cron tas…   14                                      [OK]
yloeffler/mysql-backup            This image runs mysqldump to backup data usi…   7                                       [OK]
genschsa/mysql-employees          MySQL Employee Sample Database                  7                                       [OK]
openshift/mysql-55-centos7        DEPRECATED: A Centos7 based MySQL v5.5 image…   6                                       
devilbox/mysql                    Retagged MySQL, MariaDB and PerconaDB offici…   3                                       
ansibleplaybookbundle/mysql-apb   An APB which deploys RHSCL MySQL                2                                       [OK]
jelastic/mysql                    An image of the MySQL database server mainta…   1                                       
widdpim/mysql-client              Dockerized MySQL Client (5.7) including Curl…   1                                       [OK]

2.拉取镜像

docker pull mysql:5.7.30

8559a31e96f4: Pull complete 
d51ce1c2e575: Pull complete 
c2344adc4858: Pull complete 
fcf3ceff18fc: Pull complete 
16da0c38dc5b: Pull complete 
b905d1797e97: Pull complete 
4b50d1c6b05c: Pull complete 
d85174a87144: Pull complete 
a4ad33703fa8: Pull complete 
f7a5433ce20d: Pull complete 
3dcd2a278b4a: Pull complete 
Digest: sha256:32f9d9a069f7a735e28fd44ea944d53c61f990ba71460c5c183e610854ca4854
Status: Downloaded newer image for mysql:5.7.30
docker.io/library/mysql:5.7.30

docker images

REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
feisky/app          mem-leak-fix        88b9a8aaeffa        11 months ago       73.9MB
feisky/app          mem-leak            8949a3c07284        11 months ago       73.9MB
feisky/app          io-cached           6a465f6f0be7        11 months ago       73.9MB
feisky/app          io-direct           da0f397bc71e        11 months ago       73.9MB
ubuntu              latest              adafef2e596e        11 months ago       73.9MB
mysql               5.7.30              9cfcce23593a        12 months ago       448MB

3.运行MySQL容器

#开通端口
firewall-cmd --zone=public --add-port=3308/tcp --permanent
firewall-cmd --reload

#创建容器
docker run -d -p 3308:3306 --privileged=true  -v /docker/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root123 --name mysql mysql:5.7.30 --character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci

4.参数说明:

run       run 是运行一个容器
-d        表示后台运行
-p       表示服务器端口和容器内部端口映射关联
--privileged=true                       设值MySQL 的root用户权限, 否则外部不能使用root用户登陆
-v /docker/mysql/conf/my.cnf:/etc/my.cnf 将服务器中的my.cnf配置映射到docker中的/docker/mysql/conf/my.cnf配置
-v /docker/mysql/data:/var/lib/mysql   同上,映射数据库的数据目录, 避免以后docker删除重新运行MySQL容器时数据丢失
-e MYSQL_ROOT_PASSWORD=123456         设置MySQL数据库root用户的密码
--name mysql                         设值容器名称为mysql
mysql:5.7.30                           表示从docker镜像mysql:5.7中启动一个容器
--character-set-server=utf8mb4 --collation-server=utf8mb4_general_ci  设值数据库默认编码

5.新建容器内MySQL的用户并登录

#先进入容器
docker exec -it mysql bash

#执行MySQL命令, 输入root密码, 连接MySQL
mysql -uroot -proot123

#查看版本和端口
mysql> status
--------------
mysql  Ver 14.14 Distrib 5.7.30, for Linux (x86_64) using  EditLine wrapper

Connection id:		2
Current database:	
Current user:		root@localhost
SSL:			Not in use
Current pager:		stdout
Using outfile:		''
Using delimiter:	;
Server version:		5.7.30 MySQL Community Server (GPL)
Protocol version:	10
Connection:		Localhost via UNIX socket
Server characterset:	utf8mb4
Db     characterset:	utf8mb4
Client characterset:	latin1
Conn.  characterset:	latin1
UNIX socket:		/var/run/mysqld/mysqld.sock
Uptime:			2 hours 19 min 53 sec

Threads: 1  Questions: 5  Slow queries: 0  Opens: 105  Flush tables: 1  Open tables: 98  Queries per second avg: 0.000

mysql> show variables like '%port%';
+--------------------------+-------+
| Variable_name            | Value |
+--------------------------+-------+
| innodb_support_xa        | ON    |
| large_files_support      | ON    |
| port                     | 3306  |
| report_host              |       |
| report_password          |       |
| report_port              | 3306  |
| report_user              |       |
| require_secure_transport | OFF   |
+--------------------------+-------+
8 rows in set (0.01 sec)


#输入密码后, 执行下面命令创建新用户 (用户名: test , 密码: test123)
GRANT ALL PRIVILEGES ON *.* TO 'test'@'%' IDENTIFIED BY 'test123' WITH GRANT OPTION;

#在容器外登录
mysql -utest -ptest123 -P3308 -h127.0.0.1

以上是关于docker安装mysql的主要内容,如果未能解决你的问题,请参考以下文章

docker安装mysql

docker 下安装 mysql 及登录

Linux系统下祼机安装mysql8.0和docker mysql 8.0 性能差异对比~

Docker删除报错:Error response from daemon: conflict: unable to delete 08b152afcfae (must be forced)(代码片段

12-Docker-部署SonarQube

使用docker安装mysql5.7