docker run mysql -e 的环境变量 Environment Variables

例子

sudo docker run -itd --name DockerMysqlLatest3307 -p 3307:3306 -e MYSQL_ROOT_PASSWORD='root的密码' mysql:latest
### root无密码
sudo docker run -itd --name Mysql57 -p 57:3306 -e MYSQL_ALLOW_EMPTY_PASSWORD='root' mysql:5.7

https://hub.docker.com/_/mysql?tab=description

Environment Variables (docker run -e 的环境变量)

When you start the mysql image, you can adjust the configuration of the MySQL instance by passing one or more environment variables on the docker run command line. Do note that none of the variables below will have any effect if you start the container with a data directory that already contains a database: any pre-existing database will always be left untouched on container startup.

See also https://dev.mysql.com/doc/refman/5.7/en/environment-variables.html for documentation of environment variables which MySQL itself respects (especially variables like MYSQL_HOST, which is known to cause issues when used with this image).

  • MYSQL_ROOT_PASSWORD
    This variable is mandatory and specifies the password that will be set for the MySQL root superuser account. In the above example, it was set to my-secret-pw.

  • MYSQL_DATABASE
    This variable is optional and allows you to specify the name of a database to be created on image startup. If a user/password was supplied (see below) then that user will be granted superuser access (corresponding to GRANT ALL) to this database.

  • MYSQL_USER, MYSQL_PASSWORD
    These variables are optional, used in conjunction to create a new user and to set that user’s password. This user will be granted superuser permissions (see above) for the database specified by the MYSQL_DATABASE variable. Both variables are required for a user to be created.

Do note that there is no need to use this mechanism to create the root superuser, that user gets created by default with the password specified by the MYSQL_ROOT_PASSWORD variable.

  • MYSQL_ALLOW_EMPTY_PASSWORD
    This is an optional variable. Set to a non-empty value, like yes, to allow the container to be started with a blank password for the root user. NOTE: Setting this variable to yes is not recommended unless you really know what you are doing, since this will leave your MySQL instance completely unprotected, allowing anyone to gain complete superuser access.

  • MYSQL_RANDOM_ROOT_PASSWORD
    This is an optional variable. Set to a non-empty value, like yes, to generate a random initial password for the root user (using pwgen). The generated root password will be printed to stdout (GENERATED ROOT PASSWORD: …).

  • MYSQL_ONETIME_PASSWORD
    Sets root (not the user specified in MYSQL_USER!) user as expired once init is complete, forcing a password change on first login. Any non-empty value will activate this setting. NOTE: This feature is supported on MySQL 5.6+ only. Using this option on MySQL 5.5 will throw an appropriate error during initialization.

  • MYSQL_INITDB_SKIP_TZINFO
    By default, the entrypoint script automatically loads the timezone data needed for the CONVERT_TZ() function. If it is not needed, any non-empty value disables timezone loading.

Docker Secrets
As an alternative to passing sensitive information via environment variables, _FILE may be appended to the previously listed environment variables, causing the initialization script to load the values for those variables from files present in the container. In particular, this can be used to load passwords from Docker secrets stored in /run/secrets/<secret_name> files. For example:

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD_FILE=/run/secrets/mysql-root -d mysql:tag
Currently, this is only supported for MYSQL_ROOT_PASSWORD, MYSQL_ROOT_HOST, MYSQL_DATABASE, MYSQL_USER, and MYSQL_PASSWORD.




Deploying MySQL on Linux with Docker : 在Linux上用docker部署MySQL : https://dev.mysql.com/doc/refman/8.0/en/linux-installation-docker.html

MySQL官方文档对dockers关于mysql的环境变量的说明

Docker Environment Variables
When you create a MySQL Server container, you can configure the MySQL instance by using the --env option (short form -e) and specifying one or more environment variables. No server initialization is performed if the mounted data directory is not empty, in which case setting any of these variables has no effect (see Persisting Data and Configuration Changes), and no existing contents of the directory, including server settings, are modified during container startup.

Environment variables which can be used to configure a MySQL instance are listed here:

The boolean variables including MYSQL_RANDOM_ROOT_PASSWORD, MYSQL_ONETIME_PASSWORD, MYSQL_ALLOW_EMPTY_PASSWORD, and MYSQL_LOG_CONSOLE are made true by setting them with any strings of nonzero lengths. Therefore, setting them to, for example, “0”, “false”, or “no” does not make them false, but actually makes them true. This is a known issue.

MYSQL_RANDOM_ROOT_PASSWORD: When this variable is true (which is its default state, unless MYSQL_ROOT_PASSWORD is set or MYSQL_ALLOW_EMPTY_PASSWORD is set to true), a random password for the server’s root user is generated when the Docker container is started. The password is printed to stdout of the container and can be found by looking at the container’s log (see Starting a MySQL Server Instance).

MYSQL_ONETIME_PASSWORD: When the variable is true (which is its default state, unless MYSQL_ROOT_PASSWORD is set or MYSQL_ALLOW_EMPTY_PASSWORD is set to true), the root user’s password is set as expired and must be changed before MySQL can be used normally.

MYSQL_DATABASE: This variable allows you to specify the name of a database to be created on image startup. If a user name and a password are supplied with MYSQL_USER and MYSQL_PASSWORD, the user is created and granted superuser access to this database (corresponding to GRANT ALL). The specified database is created by a CREATE DATABASE IF NOT EXIST statement, so that the variable has no effect if the database already exists.

MYSQL_USER, MYSQL_PASSWORD: These variables are used in conjunction to create a user and set that user’s password, and the user is granted superuser permissions for the database specified by the MYSQL_DATABASE variable. Both MYSQL_USER and MYSQL_PASSWORD are required for a user to be created—if any of the two variables is not set, the other is ignored. If both variables are set but MYSQL_DATABASE is not, the user is created without any privileges.

Note
There is no need to use this mechanism to create the root superuser, which is created by default with the password set by either one of the mechanisms discussed in the descriptions for MYSQL_ROOT_PASSWORD and MYSQL_RANDOM_ROOT_PASSWORD, unless MYSQL_ALLOW_EMPTY_PASSWORD is true.

MYSQL_ROOT_HOST: By default, MySQL creates the ‘root’@‘localhost’ account. This account can only be connected to from inside the container as described in Connecting to MySQL Server from within the Container. To allow root connections from other hosts, set this environment variable. For example, the value 172.17.0.1, which is the default Docker gateway IP, allows connections from the host machine that runs the container. The option accepts only one entry, but wildcards are allowed (for example, MYSQL_ROOT_HOST=172...* or MYSQL_ROOT_HOST=%).

MYSQL_LOG_CONSOLE: When the variable is true (which is its default state for MySQL 8.0 server containers), the MySQL Server’s error log is redirected to stderr, so that the error log goes into the Docker container’s log and is viewable using the docker logs mysqld-container command.

Note
The variable has no effect if a server configuration file from the host has been mounted (see Persisting Data and Configuration Changes on bind-mounting a configuration file).

MYSQL_ROOT_PASSWORD: This variable specifies a password that is set for the MySQL root account.

Warning
Setting the MySQL root user password on the command line is insecure. As an alternative to specifying the password explicitly, you can set the variable with a container file path for a password file, and then mount a file from your host that contains the password at the container file path. This is still not very secure, as the location of the password file is still exposed. It is preferable to use the default settings of MYSQL_RANDOM_ROOT_PASSWORD and MYSQL_ONETIME_PASSWORD both being true.

MYSQL_ALLOW_EMPTY_PASSWORD. Set it to true to allow the container to be started with a blank password for the root user.

Warning
Setting this variable to true is insecure, because it is going to leave your MySQL instance completely unprotected, allowing anyone to gain complete superuser access. It is preferable to use the default settings of MYSQL_RANDOM_ROOT_PASSWORD and MYSQL_ONETIME_PASSWORD both being true.




MySQL自身的环境变量

VariableDescription
AUTHENTICATION_KERBEROS_CLIENT_LOGKerberos authentication logging level.
AUTHENTICATION_LDAP_CLIENT_LOGClient-side LDAP authentication logging level.
AUTHENTICATION_PAM_LOGPAM authentication plugin debug logging settings.
CCThe name of your C compiler (for running CMake).
CXXThe name of your C++ compiler (for running CMake).
CCThe name of your C compiler (for running CMake).
DBI_USERThe default user name for Perl DBI.
DBI_TRACETrace options for Perl DBI.
HOMEThe default path for the mysql history file is $HOME/.mysql_history.
LD_RUN_PATHUsed to specify the location of libmysqlclient.so.
LIBMYSQL_ENABLE_CLEARTEXT_PLUGINEnable mysql_clear_password authentication plugin; see Section 6.4.1.4, “Client-Side Cleartext Pluggable Authentication”.
LIBMYSQL_PLUGIN_DIRDirectory in which to look for client plugins.
LIBMYSQL_PLUGINSClient plugins to preload.
MYSQL_DEBUGDebug trace options when debugging.
MYSQL_GROUP_SUFFIXOption group suffix value (like specifying --defaults-group-suffix).
MYSQL_HISTFILEThe path to the mysql history file. If this variable is set, its value overrides the default for $HOME/.mysql_history.
MYSQL_HISTIGNOREPatterns specifying statements that mysql should not log to $HOME/.mysql_history, or syslog if --syslog is given.
MYSQL_HOMEThe path to the directory in which the server-specific my.cnf file resides.
MYSQL_HOSTThe default host name used by the mysql command-line client.
MYSQL_OPENSSL_UDF_DH_BITS_THRESHOLDMaximum key length for create_dh_parameters(). See Section 6.6.3, “MySQL Enterprise Encryption Usage and Examples”.
MYSQL_OPENSSL_UDF_DSA_BITS_THRESHOLDMaximum DSA key length for create_asymmetric_priv_key(). See Section 6.6.3, “MySQL Enterprise Encryption Usage and Examples”.
MYSQL_OPENSSL_UDF_RSA_BITS_THRESHOLDMaximum RSA key length for create_asymmetric_priv_key(). See Section 6.6.3, “MySQL Enterprise Encryption Usage and Examples”.
MYSQL_PS1The command prompt to use in the mysql command-line client.
MYSQL_PWDThe default password when connecting to mysqld. Using this is insecure. See note following table.
MYSQL_TCP_PORTThe default TCP/IP port number.
MYSQL_TEST_LOGIN_FILEThe name of the .mylogin.cnf login path file.
MYSQL_TEST_TRACE_CRASHWhether the test protocol trace plugin crashes clients. See note following table.
MYSQL_TEST_TRACE_DEBUGWhether the test protocol trace plugin produces output. See note following table.
MYSQL_UNIX_PORTThe default Unix socket file name; used for connections to localhost.
MYSQLX_TCP_PORTThe X Plugin default TCP/IP port number.
MYSQLX_UNIX_PORTThe X Plugin default Unix socket file name; used for connections to localhost.
NOTIFY_SOCKETSocket used by mysqld to communicate with systemd.
PATHUsed by the shell to find MySQL programs.
PKG_CONFIG_PATHLocation of mysqlclient.pc pkg-config file. See note following table.
TMPDIRThe directory in which temporary files are created.
TZThis should be set to your local time zone. See Section B.3.3.7, “Time Zone Problems”.
UMASKThe user-file creation mode when creating files. See note following table.
UMASK_DIRThe user-directory creation mode when creating directories. See note following table.
USERThe default user name on Windows when connecting to mysqld.
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kfepiza

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值