# Use root/example as user/password credentials version:'3.1' services: db: image:mysql command:--default-authentication-plugin=mysql_native_password restart:always environment: MYSQL_ROOT_PASSWORD:example
这里配置了 root 账号的密码,也修改了认证插件让我们的 MySQL Client 可以进行登录。
除了这些,我们还需要什么?
新建数据库、导入 SQL 文件、配置 IP。
查看手册,我们可以看到 MYSQL_DATABASE 环境变量:
1
This variable is optional and allows you to specify the nameof a databaseto be created on image startup. If a user/password was supplied (see below) then that user will be granted superuseraccess (corresponding toGRANTALL) to this database.
When a container is started forthefirsttime, anew database withthe specified name will be created and initialized withthe provided configuration variables. Furthermore, it will execute fileswith extensions .sh, .sql and .sql.gz that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. You can easily populate your mysql services by mounting a SQL dump into that directoryand provide custom images with contributed data. SQL files will be imported by default tothe database specified bythe MYSQL_DATABASE variable.
root@1f39692ad792:/# mysql -uroot -ptwingsdocker mysql: [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 8 Server version: 8.0.16 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.
mysql> use flag;
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 mysql> select * from flag; +------------------+ | username | +------------------+ | flag{1234567890} | +------------------+ 1 row in set (0.00 sec)