1. 在Mac中打开Terminal
Michaels-MacBook-Pro-3:workspace-architect michaelzhang$ docker pull mysql:5.6
5.6: Pulling from library/mysql
d599a449871e: Pull complete
f287049d3170: Pull complete
08947732a1b0: Pull complete
96f3056887f2: Pull complete
871f7f65f017: Pull complete
111ea1dd4e23: Pull complete
2dcf2d87da45: Pull complete
648aa2667757: Pull complete
418a18378dc0: Pull complete
02a64522fded: Pull complete
577b15a8d700: Pull complete
Digest: sha256:5345afaaf1712e60bbc4d9ef32cc62acf41e4160584142f8d73115f16ad94af4
Status: Downloaded newer image for mysql:5.6
2. 查看mysql镜像
Michaels-MacBook-Pro-3:workspace-architect michaelzhang$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 5.6 e143ed325782 4 weeks ago 302MB
3. 启动mysql的容器
其中 docker run是启动容器的命令;i是交互式操作,t是一个终端,d指的是在后台运行,
-P指在本地生成一个随机端口,用来映射mysql的3306
端口,mysql指运行mysql镜像,5.6是Tag的值,bash指创建一个交互式shell。
Michaels-MacBook-Pro-3:workspace-architect michaelzhang$ docker run -itd -P mysql:5.6 bash
a94fd445ca731e9c5251034db58f2e61cd245c8ddd31328a4099c695ebddd4af
4. 查看已经运行的docker镜像,可以看到mysql镜像的3306端口绑定了本地的32768端口,如果你要在本机中访问docker中的mysql数据库就需要使用localhost:32768来进行访问, hardcore_lichterman 是镜像的名字。
Michaels-MacBook-Pro-3:workspace-architect michaelzhang$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a94fd445ca73 mysql:5.6 "docker-entrypoint.s…" 8 seconds ago Up 15 seconds 0.0.0.0:32768->3306/tcp hardcore_lichterman
5. 从本机连接到docker的mysql镜像中
docker exec 是docker镜像的连接命令,类似于ssh一样的命令,hardcore_lichterman是镜像的名字,镜像每次启动都必须有一个名字,该名字可以手动指定也可以自己生成。
Michaels-MacBook-Pro-3:workspace-architect michaelzhang$ docker exec -it hardcore_lichterman bash
root@a94fd445ca73:/# service mysql status
[info] MySQL Community Server 5.6.46 is not running.
root@a94fd445ca73:/# service mysql start
No directory, logging in with HOME=/
2019-12-23 07:19:59 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-12-23 07:19:59 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2019-12-23 07:19:59 0 [Note] /usr/sbin/mysqld (mysqld 5.6.46) starting as process 128 ...
2019-12-23 07:19:59 128 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-12-23 07:19:59 128 [Note] InnoDB: The InnoDB memory heap is disabled
2019-12-23 07:19:59 128 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-12-23 07:19:59 128 [Note] InnoDB: Memory barrier is not used
2019-12-23 07:19:59 128 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-12-23 07:19:59 128 [Note] InnoDB: Using Linux native AIO
2019-12-23 07:19:59 128 [Note] InnoDB: Using CPU crc32 instructions
2019-12-23 07:19:59 128 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-12-23 07:19:59 128 [Note] InnoDB: Completed initialization of buffer pool
2019-12-23 07:19:59 128 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2019-12-23 07:19:59 128 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2019-12-23 07:19:59 128 [Note] InnoDB: Database physically writes the file full: wait...
2019-12-23 07:19:59 128 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2019-12-23 07:19:59 128 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2019-12-23 07:19:59 128 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2019-12-23 07:19:59 128 [Warning] InnoDB: New log files created, LSN=45781
2019-12-23 07:19:59 128 [Note] InnoDB: Doublewrite buffer not found: creating new
2019-12-23 07:19:59 128 [Note] InnoDB: Doublewrite buffer created
2019-12-23 07:19:59 128 [Note] InnoDB: 128 rollback segment(s) are active.
2019-12-23 07:19:59 128 [Warning] InnoDB: Creating foreign key constraint system tables.
2019-12-23 07:19:59 128 [Note] InnoDB: Foreign key constraint system tables created
2019-12-23 07:19:59 128 [Note] InnoDB: Creating tablespace and datafile system tables.
2019-12-23 07:19:59 128 [Note] InnoDB: Tablespace and datafile system tables created.
2019-12-23 07:19:59 128 [Note] InnoDB: Waiting for purge to start
2019-12-23 07:19:59 128 [Note] InnoDB: 5.6.46 started; log sequence number 0
2019-12-23 07:19:59 128 [Note] RSA private key file not found: /var/lib/mysql//private_key.pem. Some authentication plugins will not work.
2019-12-23 07:19:59 128 [Note] RSA public key file not found: /var/lib/mysql//public_key.pem. Some authentication plugins will not work.
2019-12-23 07:20:00 128 [Note] Binlog end
2019-12-23 07:20:00 128 [Note] InnoDB: FTS optimize thread exiting.
2019-12-23 07:20:00 128 [Note] InnoDB: Starting shutdown...
2019-12-23 07:20:01 128 [Note] InnoDB: Shutdown completed; log sequence number 1625977
2019-12-23 07:20:01 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2019-12-23 07:20:01 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2019-12-23 07:20:01 0 [Note] /usr/sbin/mysqld (mysqld 5.6.46) starting as process 151 ...
2019-12-23 07:20:01 151 [Note] InnoDB: Using atomics to ref count buffer pool pages
2019-12-23 07:20:01 151 [Note] InnoDB: The InnoDB memory heap is disabled
2019-12-23 07:20:01 151 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2019-12-23 07:20:01 151 [Note] InnoDB: Memory barrier is not used
2019-12-23 07:20:01 151 [Note] InnoDB: Compressed tables use zlib 1.2.11
2019-12-23 07:20:01 151 [Note] InnoDB: Using Linux native AIO
2019-12-23 07:20:01 151 [Note] InnoDB: Using CPU crc32 instructions
2019-12-23 07:20:01 151 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2019-12-23 07:20:01 151 [Note] InnoDB: Completed initialization of buffer pool
2019-12-23 07:20:01 151 [Note] InnoDB: Highest supported file format is Barracuda.
2019-12-23 07:20:01 151 [Note] InnoDB: 128 rollback segment(s) are active.
2019-12-23 07:20:01 151 [Note] InnoDB: 5.6.46 started; log sequence number 1625977
2019-12-23 07:20:01 151 [Note] RSA private key file not found: /var/lib/mysql//private_key.pem. Some authentication plugins will not work.
2019-12-23 07:20:01 151 [Note] RSA public key file not found: /var/lib/mysql//public_key.pem. Some authentication plugins will not work.
2019-12-23 07:20:02 151 [Note] Binlog end
2019-12-23 07:20:02 151 [Note] InnoDB: FTS optimize thread exiting.
2019-12-23 07:20:02 151 [Note] InnoDB: Starting shutdown...
2019-12-23 07:20:03 151 [Note] InnoDB: Shutdown completed; log sequence number 1625987
No directory, logging in with HOME=/
...
[info] MySQL Community Server 5.6.46 is started.
6. 报错(No directory, logging in with HOME=/)没有找到mysql所在的目录,配置用户的登录目录即可,但是要先停mysql的服务
root@a94fd445ca73:/# service mysql stop
.....
[info] MySQL Community Server 5.6.46 is stopped.
root@a94fd445ca73:/# usermod -d /var/lib/mysql/ mysql
root@a94fd445ca73:/# service mysql start
..
[info] MySQL Community Server 5.6.46 is started.
7. 输入mysql 验证mysql是否启动成功
root@a94fd445ca73:/# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.46 MySQL Community Server (GPL)
Copyright (c) 2000, 2019, 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.
8. 查看所有的数据库, 到此为止,docker中的mysql已经启动成功。
mysql> show databases
-> ;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
9. 如何在外部使用root连接这个mysql? 为了安全,首先需要设置root帐号的密码,先将当前库切到mysql
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
10. 设置root账户的密码为root
mysql> update user set authentication_string = password('root') where user = 'root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4 Changed: 4 Warnings: 0
11. 给所有root用户赋权,外部的电脑也能够通过root访问Docker中的mysql数据库
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
mysql>
12. 使用mysql的workbench从本机链接到docker中的mysql
链接成功,测试执行成功。