The steps to install MySQL on Docker include: 1. Make sure Docker and docker-compose are installed; 2. Use the docker run command to quickly start the test instance and set the password; 3. Mount the data volume to prevent data loss after container deletion; 4. It is recommended to use the docker-compose.yml file management service for maintenance; 5. Pay attention to password settings, port mapping, version control, and character set configuration. Just follow the above order to successfully complete MySQL deployment on Docker.
Installing MySQL to Docker is not difficult, it can be completed in just a few steps. The key is to configure the parameters, mount the directory and set the password to avoid the data being lost or the database cannot be connected to the container once it restarts.

Preparation: Make sure Docker is installed
Before you begin, make sure that Docker is installed on your system. You can check it by following the command:

docker --version
If you return information similar to Docker version 24.xx
, it means that it has been installed. If not, please download the installation package and install it according to your operating system.
In addition, it is recommended to install docker-compose
, as it will be more convenient to configure it later.

The easiest way to start a MySQL container
If you just want to run a test MySQL instance quickly, you can start it directly with the following command:
docker run --name mysql-test -e MYSQL_ROOT_PASSWORD=yourpassword -d -p 3306:3306 mysql:latest
Explain a few key parameters:
-
--name
: Give the container a name for easy management; -
-e MYSQL_ROOT_PASSWORD=...
: Set the password of the root user; -
-d
: run in the background; -
-p 3306:3306
: Map the 3306 port of the host to 3306 of the container; -
mysql:latest
: Use the latest version of the image.
After this execution, MySQL will be launched, and you can try connecting with the client.
More recommended practice: Mount data volumes to avoid data loss
Although the above method is simple, there is a problem: once the container is deleted, the data will be gone. To prevent this, it is best to mount the data directory.
You can do this:
docker run --name mysql-prod \ -e MYSQL_ROOT_PASSWORD=yourpassword \ -v /your/local/path/mysql/data:/var/lib/mysql \ -p 3306:3306 \ -d mysql:latest
Here is a -v
parameter, which means mapping /var/lib/mysql
in the container to a directory in your local area. Even if the container is deleted, the data is still retained locally.
Suggestion: Wait for a few minutes when running the first time, because it may take time to initialize the data for the first time in MySQL, especially after using persistent storage.
Use docker-compose to build more clearly
If you like to use configuration files to manage services, you can write a docker-compose.yml
file, with the following content:
version: '3.8' services: mysql: image: mysql:latest container_name: mysql-server environment: MYSQL_ROOT_PASSWORD: yourpassword Volumes: - ./mysql-data:/var/lib/mysql Ports: - "3306:3306" restart: unless-stopped
Then execute in the same directory:
docker-compose up -d
The advantage of this method is that it is clear structure, easy to maintain, and is suitable for long-term operating environments.
Notes and FAQs
- Don't forget the password : If you forget what password you set, you have to rebuild the container.
- Port conflict problem : If MySQL is already running on the machine, it will prompt the port to occupy, and you can change the mapping to
3307:3306
. - Version control : It is recommended to specify a specific version number in the production environment, such as
mysql:8.0
, rather thanlatest
. - Character set settings : Sometimes you will encounter Chinese garbled code. You can add
--character-set-server=utf8mb4
and--collation-server=utf8mb4_unicode_ci
to the startup command.
Basically that's it. The operation is not complicated, but some details are easy to ignore, especially the data mount and password settings.
The above is the detailed content of How to install MySQL on Docker. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The default user name of MySQL is usually 'root', but the password varies according to the installation environment; in some Linux distributions, the root account may be authenticated by auth_socket plug-in and cannot log in with the password; when installing tools such as XAMPP or WAMP under Windows, root users usually have no password or use common passwords such as root, mysql, etc.; if you forget the password, you can reset it by stopping the MySQL service, starting in --skip-grant-tables mode, updating the mysql.user table to set a new password and restarting the service; note that the MySQL8.0 version requires additional authentication plug-ins.

GTID (Global Transaction Identifier) ??solves the complexity of replication and failover in MySQL databases by assigning a unique identity to each transaction. 1. It simplifies replication management, automatically handles log files and locations, allowing slave servers to request transactions based on the last executed GTID. 2. Ensure consistency across servers, ensure that each transaction is applied only once on each server, and avoid data inconsistency. 3. Improve troubleshooting efficiency. GTID includes server UUID and serial number, which is convenient for tracking transaction flow and accurately locate problems. These three core advantages make MySQL replication more robust and easy to manage, significantly improving system reliability and data integrity.

MySQL main library failover mainly includes four steps. 1. Fault detection: Regularly check the main library process, connection status and simple query to determine whether it is downtime, set up a retry mechanism to avoid misjudgment, and can use tools such as MHA, Orchestrator or Keepalived to assist in detection; 2. Select the new main library: select the most suitable slave library to replace it according to the data synchronization progress (Seconds_Behind_Master), binlog data integrity, network delay and load conditions, and perform data compensation or manual intervention if necessary; 3. Switch topology: Point other slave libraries to the new master library, execute RESETMASTER or enable GTID, update the VIP, DNS or proxy configuration to

There are three ways to modify or reset MySQLroot user password: 1. Use the ALTERUSER command to modify existing passwords, and execute the corresponding statement after logging in; 2. If you forget your password, you need to stop the service and start it in --skip-grant-tables mode before modifying; 3. The mysqladmin command can be used to modify it directly by modifying it. Each method is suitable for different scenarios and the operation sequence must not be messed up. After the modification is completed, verification must be made and permission protection must be paid attention to.

The steps to connect to the MySQL database are as follows: 1. Use the basic command format mysql-u username-p-h host address to connect, enter the username and password to log in; 2. If you need to directly enter the specified database, you can add the database name after the command, such as mysql-uroot-pmyproject; 3. If the port is not the default 3306, you need to add the -P parameter to specify the port number, such as mysql-uroot-p-h192.168.1.100-P3307; In addition, if you encounter a password error, you can re-enter it. If the connection fails, check the network, firewall or permission settings. If the client is missing, you can install mysql-client on Linux through the package manager. Master these commands

Toalteralargeproductiontablewithoutlonglocks,useonlineDDLtechniques.1)IdentifyifyourALTERoperationisfast(e.g.,adding/droppingcolumns,modifyingNULL/NOTNULL)orslow(e.g.,changingdatatypes,reorderingcolumns,addingindexesonlargedata).2)Usedatabase-specifi

InnoDB implements repeatable reads through MVCC and gap lock. MVCC realizes consistent reading through snapshots, and the transaction query results remain unchanged after multiple transactions; gap lock prevents other transactions from inserting data and avoids phantom reading. For example, transaction A first query gets a value of 100, transaction B is modified to 200 and submitted, A is still 100 in query again; and when performing scope query, gap lock prevents other transactions from inserting records. In addition, non-unique index scans may add gap locks by default, and primary key or unique index equivalent queries may not be added, and gap locks can be cancelled by reducing isolation levels or explicit lock control.

IndexesinMySQLimprovequeryspeedbyenablingfasterdataretrieval.1.Theyreducedatascanned,allowingMySQLtoquicklylocaterelevantrowsinWHEREorORDERBYclauses,especiallyimportantforlargeorfrequentlyqueriedtables.2.Theyspeedupjoinsandsorting,makingJOINoperation
