docker 安装mysql

本文介绍了如何通过命令行使用Docker拉取并运行MySQL 5.6镜像,设置容器名称、端口映射及环境变量,还涉及到用户授权和通过Dockerfile创建MySQL容器的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

https://www.cnblogs.com/han-1034683568/p/6941337.html?utm_source=debugrun&utm_medium=referral

https://yeasy.gitbooks.io/docker_practice/content/image/dockerfile/cmd.html


docker search mysql

docker pull mysql:5.6

docker run -p 3306:3306 --name mymysql -v /usr/local/mymysql/conf:/etc/mysql/conf.d -v /usr/local/mymysql/logs:/logs -v /usr/local/mymysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7

-p 3306:3306:将容器的 3306 端口映射到主机的 3306 端口。

-v /usr/local/mymysql/conf:/etc/mysql/conf.d:将主机当前目录下的 conf/my.cnf 挂载到容器的 /etc/mysql/my.cnf。

-v /usr/local/mymysql/logs:/logs:将主机当前目录下的 logs 目录挂载到容器的 /logs。

-v /usr/local/mymysql/data:/var/lib/mysql :将主机当前目录下的data目录挂载到容器的 /var/lib/mysql 。

-e MYSQL_ROOT_PASSWORD=123456:初始化 root 用户的密码。
-d: 后台运行容器,并返回容器ID


docker run -v /usr/local/mypython:/usr/src/myapp --name python3.6 -w /usr/src/myapp python:3.6 python testPython.py

-v 挂载主机上的文件卷到容器
-w 容器内的默认工作目录


#进入容器
docker exec -it mysql bash

docker build -t mysql:5.7 .
-t      ---------指定镜像名和标签

FROM mysql:5.7.20

MAINTAINER FinTester_Helix

# 指定工作目录
WORKDIR /usr/fin/mysql

#设置时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai  /etc/localtime

#设置免密登录
ENV MYSQL_ALLOW_EMPTY_PASSWORD yes

#将所需文件放到容器中
COPY setup.sh ./
COPY schema.sql ./
COPY privileges.sql ./

#设置容器启动时执行的命令
CMD ["sh", "/setup.sh"]


setup.sh

#!/bin/bash
# set -e
#查看mysql服务的状态,方便调试,这条语句可以删除
echo `service mysql status`
echo '1.启动mysql....2320'
#启动mysql
service mysql start
sleep 5
#echo `service mysql status`
#echo '2.开始导入数据....'
#导入数据
#mysql < schema.sql
#echo '3.导入数据完毕....'
#sleep 3
#echo `service mysql status`
#重新设置mysql密码
#echo '4.开始创建用户及授权....'
#mysql < privileges.sql
#echo '5.创建用户及授权....'
#sleep 3
echo `service mysql status`
#echo `mysql容器启动完毕,且数据导入成功`
tail -f /dev/null



privileges.sql

use mysql;
select host, user from user;
-- 因为mysql版本是5.7,因此新建用户为如下命令:
create user testgroup identified by '123456';
-- 将所有数据库的权限授权给创建的testgroup用户,密码为123456:
-- grant all privileges on *.* to 'test'@'%'identified by '123456' with grant option
-- all代表接受所有操作,比如 select,insert,delete....; *.* 代表所有库下面的所有表;% 代表这个用户允许从任何地方登录;为了安全起见,这个%可以替换为你允许的ip地址;
grant all privileges on *.* to testgroup@'%' identified by '123456' with grant option;
-- 这一条命令一定要有:
flush privileges;




touch mysql.dockerfile

docker build -t mysql:5.7 -f mysql.dockerfile .
  • 命令行创建
    1. 拉取官方的镜像,标签为5.6

  • docker pull mysql:5.6

5.6: Pulling from library/mysql
fc7181108d40: Pull complete 
787a24c80112: Pull complete 
a08cb039d3cd: Pull complete 
4f7d35eb5394: Pull complete 
5aa21f895d95: Pull complete 
345649b63bc3: Pull complete 
591a87fc59ec: Pull complete 
6019ecdb5901: Pull complete 
aadc682b6a75: Pull complete 
e0969183ab5f: Pull complete 
8a01a78fff5a: Pull complete 
Digest: sha256:e4a70c0f52bfda9ed28d2127b461ff44cbc381450e7ca22e83936560d8875f14
Status: Downloaded newer image for mysql:5.6
   2. 查看镜像
  • docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mysql               5.6                 3ed1080b793f        13 days ago         256MB
python              3.6                 2bb3204ab1d1        2 months ago        924MB
   3. 运行容器
  • docker run --name docker_mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql:5.6
Initializing database
2019-06-24 11:51:27 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-06-24 11:51:27 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2019-06-24 11:51:27 0 [Note] /usr/sbin/mysqld (mysqld 5.6.44) starting as process 33 ...
2019-06-24 11:51:27 33 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-06-24 11:51:27 33 [Note] InnoDB: The InnoDB memory heap is disabled
2019-06-24 11:51:27 33 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-06-24 11:51:27 33 [Note] InnoDB: Memory barrier is not used
2019-06-24 11:51:27 33 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-06-24 11:51:27 33 [Note] InnoDB: Using Linux native AIO
2019-06-24 11:51:27 33 [Note] InnoDB: Using CPU crc32 instructions
2019-06-24 11:51:27 33 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-06-24 11:51:27 33 [Note] InnoDB: Completed initialization of buffer pool
2019-06-24 11:51:27 33 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2019-06-24 11:51:27 33 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2019-06-24 11:51:27 33 [Note] InnoDB: Database physically writes the file full: wait...
2019-06-24 11:51:27 33 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2019-06-24 11:51:27 33 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2019-06-24 11:51:28 33 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2019-06-24 11:51:28 33 [Warning] InnoDB: New log files created, LSN=45781
2019-06-24 11:51:28 33 [Note] InnoDB: Doublewrite buffer not found: creating new
2019-06-24 11:51:28 33 [Note] InnoDB: Doublewrite buffer created
2019-06-24 11:51:28 33 [Note] InnoDB: 128 rollback segment(s) are active.
2019-06-24 11:51:28 33 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-06-24 11:51:28 33 [Note] InnoDB: Foreign key constraint system tables created
2019-06-24 11:51:28 33 [Note] InnoDB: Creating tablespace and datafile system tables.
2019-06-24 11:51:28 33 [Note] InnoDB: Tablespace and datafile system tables created.
2019-06-24 11:51:28 33 [Note] InnoDB: Waiting for purge to start
2019-06-24 11:51:28 33 [Note] InnoDB: 5.6.44 started; log sequence number 0
2019-06-24 11:51:28 33 [Note] Binlog end
2019-06-24 11:51:28 33 [Note] InnoDB: FTS optimize thread exiting.
2019-06-24 11:51:28 33 [Note] InnoDB: Starting shutdown...
2019-06-24 11:51:30 33 [Note] InnoDB: Shutdown completed; log sequence number 1625977


2019-06-24 11:51:30 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-06-24 11:51:30 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2019-06-24 11:51:30 0 [Note] /usr/sbin/mysqld (mysqld 5.6.44) starting as process 56 ...
2019-06-24 11:51:30 56 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-06-24 11:51:30 56 [Note] InnoDB: The InnoDB memory heap is disabled
2019-06-24 11:51:30 56 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-06-24 11:51:30 56 [Note] InnoDB: Memory barrier is not used
2019-06-24 11:51:30 56 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-06-24 11:51:30 56 [Note] InnoDB: Using Linux native AIO
2019-06-24 11:51:30 56 [Note] InnoDB: Using CPU crc32 instructions
2019-06-24 11:51:30 56 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-06-24 11:51:30 56 [Note] InnoDB: Completed initialization of buffer pool
2019-06-24 11:51:30 56 [Note] InnoDB: Highest supported file format is Barracuda.
2019-06-24 11:51:30 56 [Note] InnoDB: 128 rollback segment(s) are active.
2019-06-24 11:51:30 56 [Note] InnoDB: Waiting for purge to start
2019-06-24 11:51:30 56 [Note] InnoDB: 5.6.44 started; log sequence number 1625977
2019-06-24 11:51:30 56 [Note] Binlog end
2019-06-24 11:51:30 56 [Note] InnoDB: FTS optimize thread exiting.
2019-06-24 11:51:30 56 [Note] InnoDB: Starting shutdown...
2019-06-24 11:51:32 56 [Note] InnoDB: Shutdown completed; log sequence number 1625987




PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

  /usr/bin/mysqladmin -u root password 'new-password'
  /usr/bin/mysqladmin -u root -h 844da9757490 password 'new-password'

Alternatively you can run:

  /usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

  http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

Note: new default config file not created.
Please make sure your config file is current

WARNING: Default config file /etc/mysql/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

Database initialized
MySQL init process in progress...
2019-06-24 11:51:32 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-06-24 11:51:32 0 [Note] mysqld (mysqld 5.6.44) starting as process 84 ...
2019-06-24 11:51:32 84 [Note] Plugin 'FEDERATED' is disabled.
2019-06-24 11:51:32 84 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-06-24 11:51:32 84 [Note] InnoDB: The InnoDB memory heap is disabled
2019-06-24 11:51:32 84 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-06-24 11:51:32 84 [Note] InnoDB: Memory barrier is not used
2019-06-24 11:51:32 84 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-06-24 11:51:32 84 [Note] InnoDB: Using Linux native AIO
2019-06-24 11:51:32 84 [Note] InnoDB: Using CPU crc32 instructions
2019-06-24 11:51:32 84 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-06-24 11:51:32 84 [Note] InnoDB: Completed initialization of buffer pool
2019-06-24 11:51:32 84 [Note] InnoDB: Highest supported file format is Barracuda.
2019-06-24 11:51:32 84 [Note] InnoDB: 128 rollback segment(s) are active.
2019-06-24 11:51:32 84 [Note] InnoDB: Waiting for purge to start
2019-06-24 11:51:32 84 [Note] InnoDB: 5.6.44 started; log sequence number 1625987
2019-06-24 11:51:32 84 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 69205d4a-9676-11e9-b7ff-0242ac110002.
2019-06-24 11:51:32 84 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2019-06-24 11:51:32 84 [Warning] 'user' entry 'root@844da9757490' ignored in --skip-name-resolve mode.
2019-06-24 11:51:32 84 [Warning] 'user' entry '@844da9757490' ignored in --skip-name-resolve mode.
2019-06-24 11:51:32 84 [Warning] 'proxies_priv' entry '@ root@844da9757490' ignored in --skip-name-resolve mode.
2019-06-24 11:51:32 84 [Note] Event Scheduler: Loaded 0 events
2019-06-24 11:51:32 84 [Note] mysqld: ready for connections.
Version: '5.6.44'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server (GPL)
Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
2019-06-24 11:51:34 84 [Warning] 'proxies_priv' entry '@ root@844da9757490' ignored in --skip-name-resolve mode.

2019-06-24 11:51:34 84 [Note] mysqld: Normal shutdown

2019-06-24 11:51:34 84 [Note] Giving 0 client threads a chance to die gracefully
2019-06-24 11:51:34 84 [Note] Event Scheduler: Purging the queue. 0 events
2019-06-24 11:51:34 84 [Note] Shutting down slave threads
2019-06-24 11:51:34 84 [Note] Forcefully disconnecting 0 remaining clients
2019-06-24 11:51:34 84 [Note] Binlog end
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'partition'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_FT_DELETED'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_METRICS'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_CMPMEM'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_CMP_RESET'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_CMP'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_LOCKS'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'INNODB_TRX'
2019-06-24 11:51:34 84 [Note] Shutting down plugin 'InnoDB'
2019-06-24 11:51:34 84 [Note] InnoDB: FTS optimize thread exiting.
2019-06-24 11:51:34 84 [Note] InnoDB: Starting shutdown...
2019-06-24 11:51:36 84 [Note] InnoDB: Shutdown completed; log sequence number 1625997
2019-06-24 11:51:36 84 [Note] Shutting down plugin 'BLACKHOLE'
2019-06-24 11:51:36 84 [Note] Shutting down plugin 'ARCHIVE'
2019-06-24 11:51:36 84 [Note] Shutting down plugin 'MRG_MYISAM'
2019-06-24 11:51:36 84 [Note] Shutting down plugin 'MyISAM'
2019-06-24 11:51:36 84 [Note] Shutting down plugin 'MEMORY'
2019-06-24 11:51:36 84 [Note] Shutting down plugin 'CSV'
2019-06-24 11:51:36 84 [Note] Shutting down plugin 'sha256_password'
2019-06-24 11:51:36 84 [Note] Shutting down plugin 'mysql_old_password'
2019-06-24 11:51:36 84 [Note] Shutting down plugin 'mysql_native_password'
2019-06-24 11:51:36 84 [Note] Shutting down plugin 'binlog'
2019-06-24 11:51:36 84 [Note] mysqld: Shutdown complete


MySQL init process done. Ready for start up.

2019-06-24 11:51:36 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-06-24 11:51:36 0 [Note] mysqld (mysqld 5.6.44) starting as process 1 ...
2019-06-24 11:51:36 1 [Note] Plugin 'FEDERATED' is disabled.
2019-06-24 11:51:36 1 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-06-24 11:51:36 1 [Note] InnoDB: The InnoDB memory heap is disabled
2019-06-24 11:51:36 1 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-06-24 11:51:36 1 [Note] InnoDB: Memory barrier is not used
2019-06-24 11:51:36 1 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-06-24 11:51:36 1 [Note] InnoDB: Using Linux native AIO
2019-06-24 11:51:36 1 [Note] InnoDB: Using CPU crc32 instructions
2019-06-24 11:51:36 1 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-06-24 11:51:36 1 [Note] InnoDB: Completed initialization of buffer pool
2019-06-24 11:51:36 1 [Note] InnoDB: Highest supported file format is Barracuda.
2019-06-24 11:51:36 1 [Note] InnoDB: 128 rollback segment(s) are active.
2019-06-24 11:51:36 1 [Note] InnoDB: Waiting for purge to start
2019-06-24 11:51:36 1 [Note] InnoDB: 5.6.44 started; log sequence number 1625997
2019-06-24 11:51:36 1 [Note] Server hostname (bind-address): '*'; port: 3306
2019-06-24 11:51:36 1 [Note] IPv6 is available.
2019-06-24 11:51:36 1 [Note]   - '::' resolves to '::';
2019-06-24 11:51:36 1 [Note] Server socket created on IP: '::'.
2019-06-24 11:51:36 1 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
2019-06-24 11:51:36 1 [Warning] 'proxies_priv' entry '@ root@844da9757490' ignored in --skip-name-resolve mode.
2019-06-24 11:51:36 1 [Note] Event Scheduler: Loaded 0 events
2019-06-24 11:51:36 1 [Note] mysqld: ready for connections.
Version: '5.6.44'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server (GPL)
   4. 运行容器
  • docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                    NAMES
844da9757490        mysql:5.6           "docker-entrypoint.s…"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp   docker_mysql
   5. 进度容器
  • docker exec -it docker_mysql bash

在这里插入图片描述
5. 用户授权

   **如果不是%符号,需要授权**
  • 创建test_group用户
CREATE USER 'test_group'@'localhost' IDENTIFIED BY '123456';

在这里插入图片描述

  • 授权
grant all privileges on *.* to test_group@'%' identified by '123456' with grant option;

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • dockerfile创建容器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值