Docker mysql quick reference Japanese translation

Japanese translation of Docker Hub mysql Quick Reference.

What is MySQL?

MySQL is the most popular open source database in the world. With proven performance, reliability, and ease of use, MySQL has become the leading database choice for web-based applications covering the full range of personal projects and websites, e-commerce and information services. .. It is also used in web properties such as Facebook, Twitter, YouTube, and Yahoo !. For more information on MySQL Server and other MySQL products and related downloads, please visit www.mysql.com.

How to use the image

Start a mysql server instance

Starting a MySQL instance is easy.

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

Some-mysql is the name you assign to the container, my-secret-pw is the password you set for the MySQL root user, and tag is the tag that specifies the required MySQL version. See the list above for related tags. Connect to MySQL from the MySQL command line client. The following command launches another mysql container instance and runs the mysql command line client against the original mysql container so that you can execute SQL statements against the database instance.

$ docker run -it --network some-network --rm mysql mysql -hsome-mysql -uexample-user -p

Some-mysql is the name of the original mysql container ( some-network attached to the Docker network). This image can also be used as a client for non-Docker instances or remote instances.

$ docker run -it --rm mysql mysql -hsome.mysql.host -usome-mysql-user -p

See the MySQL documentation for more information on the MySQL command line client.

Usage via docker stack deploy or docker-compose

stack.yml description example


# 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

  adminer:
    image: adminer
    restart: always
    ports:
      - 8080:8080

Run docker stack deploy -c stack.yml mysql (or docker-compose -f stack.yml up) and wait for it to be fully initialized http: // swarm-ip Go to: 8080, http: // localhost: 8080, or http: // host-ip: 8080.

Accessing container shells and viewing MySQL logs

You can use the docker exec command to execute commands inside a Docker container. The following command line provides a bash shell inside a mysql container.

$ docker exec -it some-mysql bash

Logs are available from Docker's container logs.

$ docker logs some-mysql

Using a custom MySQL configuration file

The default MySQL settings are in /etc/mysql/my.cnf. This may include additional directories such as /etc/mysql/conf.d and /etc/mysql/mysql.conf.d. Check the relevant files and directories in the mysql image for more information. If / my / custom / config-file.cnf is the path and name of the custom configuration file, you can start the mysql container as follows (with this command, only the directory path of the custom configuration file Please note that it is used).

$ docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

This allows the MySQL instance to use a combination of the startup settings of / etc / mysql / my.cnf and /etc/mysql/conf.d/config-file.cnf, the latter setting. Starts a new container, some-mysql, which takes precedence.

Setting method that does not use the cnf file

Many configuration options can be passed to mysqld as flags. This gives you the flexibility to customize the container without the need for a cnf file. For example, if you want to change the default encoding and collation of all tables to use UTF-8 (utf8mb4), just run the following command:

$ docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

If you want to see the complete list of available options, do the following:

$ docker run -it --rm mysql:tag --verbose --help

Environment variable

You can adjust the configuration of your MySQL instance by passing one or more environment variables on the docker run command line when you start the mysql image. Note that none of the following variables have any effect if you start the container in a data directory that already contains a database. The existing database is not changed at all times when the container is started. For documentation of environment variables that MySQL itself respects, especially variables like MYSQL_HOST that are known to cause problems when used with this image, https://dev.mysql.com/ See also doc / refman / 5.7 / en / environment-variables.html.

MYSQL_ROOT_PASSWORD

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

MYSQL_DATABASE

This variable is optional. You can specify the name of the database that will be created when the image boots. If a user / password is specified (see below), that user will be granted superuser access to this database (corresponding to GRANT ALL).

MYSQL_USER, MYSQL_PASSWORD

These variables are optional. Used in combination to create a new user and set a password for that user. This user will be granted superuser privileges (see above) for the database specified by the MYSQL_DATABASE variable. Both variables are required to create a user. Note that you do not need to use this mechanism to create the root superuser. The root superuser is created by default with the password specified in the MYSQL_ROOT_PASSWORD variable.

MYSQL_ALLOW_EMPTY_PASSWORD

This is an optional variable. Setting it to a non-empty value, such as yes, allows the container to be started as the root user without a password.

Note: Setting this variable to yes is not recommended unless you really understand what you are doing. This completely protects your MySQL instance and allows anyone to get full superuser access.

MYSQL_RANDOM_ROOT_PASSWORD

This is an optional variable. Set to a non-empty value such as yes to generate a random initial password for the root user (using pwgen). The generated root password is printed to stdout (GENERATED ROOT PASSWORD: ......).

MYSQL_ONETIME_PASSWORD

When the initialization is complete, it sets the root user (not the user specified by [MYSQL_USER](# mysql_user-mysql_password)) as expired and forces the password change on the first login. This setting becomes active if there is a non-empty value.

Note: This feature is only supported in MySQL 5.6 and later. If you use this option in MySQL 5.5, an error will be thrown during initialization.

MYSQL_INITDB_SKIP_TZINFO

By default, the entry point script automatically loads the time zone data needed for the CONVERT_TZ () function. If not needed, specifying a non-empty value will disable time zone loading.

Docker Secrets

Instead of passing sensitive information through environment variables, add _FILE to the above environment variables so that the initialization script loads the values of those variables from a file that resides inside the container. I can. In particular this can be used to load passwords from Docker secrets stored in the / run / secrets / <secret_name> file. 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 the following variables:

Initialize a new instance

When the container is started for the first time, a new database with the specified name is created and initialized with the specified configuration variables. It also runs files with the extensions .sh, .sql, and .sql.gz located in / docker-entrypoint-initdb.d. The files are executed in alphabetical order. You can easily populate the mysql service by mounting the SQL dump in that directory and providing a custom image with the data provided. By default, SQL files are imported into the database specified by the MYSQL_DATABASE variable.

Data storage location

Important Note: There are several ways to store the data used by applications running in Docker containers. Users of the mysql image are encouraged to become familiar with the available options:

Let Docker manage the storage of database data by writing database files to disks on the host system using its own internal volume management. This is the default and is easy and fairly transparent to the user. The downside is that it can be difficult to find files outside the tools and applications that run directly on the host system, that is, outside the container. Create a data directory on the host system (outside the container) and mount it in a directory visible from inside the container. This puts the database files in a known location on the host system, making it easy for tools and applications on the host system to access the files. The downside is that the user needs to make sure that the directory exists and that the host system's directory permissions and other security mechanisms are set correctly.

The Docker documentation is a good starting point for understanding different storage options and variations, and there are several blog and forum posts that discuss and advise in this area. Here is a brief description of the basic steps for the latter option above.

Create a data directory such as / my / own / datadir on the appropriate volume on the host system. Start the mysql container as follows:

$ docker run --name some-mysql -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

The -v / my / own / datadir: / var / lib / mysql part of the command will take the / my / own / datadir directory from the underlying host system to the / var in the container. Mount as / lib / mysql. MySQL writes data files by default. There will be no connection until MySQL initialization is complete. If there is no database initialized when the container starts, a default database will be created. This is expected behavior, but it means that it will not accept incoming connections until the initialization is complete. This can cause problems when using automated tools such as docker-compose, which launches multiple containers at the same time. If the application trying to connect to MySQL does not handle MySQL downtime, or is waiting for MySQL to start successfully, you need to set up a connection and retry loop before the service starts. .. See WordPress or Bonita for an example of such an implementation in the official image.

Usage for existing databases

If you want to start the mysql container instance in a data directory that already contains a database (specifically, a mysql subdirectory), omit the $ MYSQL_ROOT_PASSWORD variable from the execution command line. Even if the above variable is specified, it will be ignored and the existing database will not be changed.

How to run as any user

You need to run mysqld if you know that the directory permissions are already set properly (for example, if you are running against an existing database as described above), or if you want to run mysqld with a specific UID / GID. If so, you can use --user to call this image. Set to any value (other than root / 0) to achieve the desired connection / configuration.

$ mkdir data
$ ls -lnd data
drwxr-xr-x 2 1000 1000 4096 Aug 27 15:54 data
$ docker run -v "$PWD/data":/var/lib/mysql --user 1000:1000 --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag

Creating a database dump

Most of the usual tools work, but their usage can be a bit complicated to give you access to the mysqld server. An easy way to check this is to use docker exec and run the tool from the same container as follows:

$ docker exec some-mysql sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /some/path/on/your/host/all-databases.sql

Data restore from dump file

For data recovery. You can use the docker exec command with the -i flag as follows:

$ docker exec -i some-mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /some/path/on/your/host/all-databases.sql

license

View license information for the software contained in this image. Like all Docker images, these are direct or indirect dependencies of other software that may be under other licenses (such as Bash in the base distribution) included in the software. Relationship) may also be included. Some additional license information that could be auto-discovered may be in the mysql / directory of the repo-info repository. With respect to the use of a pre-built image, it is the image user's responsibility to ensure that the use of this image complies with the relevant licenses of all software contained therein.

Recommended Posts

Docker mysql quick reference Japanese translation
Dockerfile reference Japanese translation
docker help Japanese translation
docker build --help Japanese translation
docker run --help Japanese translation
Compose file version 3 reference Japanese translation
Apache Spark Document Japanese Translation --Quick Start
sosreport Japanese translation
man systemd Japanese translation
streamlit explanation Japanese translation
streamlit tutorial Japanese translation
man systemd.service Japanese translation
man nftables Japanese translation
docker-compose --help Japanese translation
Japanese translation of sysstat manual
Japanese translation of Linux manual