Connecting MySQL Workbench to MySQL Running in Docker
Accessing MySQL within a Docker container is straightforward, but connecting to it from your local machine can be challenging due to default connection restrictions. However, by making a few adjustments to the MySQL container, you can allow external connections.
Modify MySQL Connection Settings
Start by creating a MySQL container with required port mappings:
docker run -p 3306:3306 --name=mysql57 -d mysql/mysql-server:5.7
Obtain the default password for fresh installations:
docker logs mysql57 2>&1 | grep GENERATED
Connect to MySQL using the command line:
docker exec -it mysql57 mysql -uroot -p
If necessary, change the root password using the ALTER USER command.
Execute the following SQL statement:
update mysql.user set host = '%' where user='root';
Restart the Container
Once the settings have been adjusted, restart the container:
docker restart mysql57
Connect from MySQL Workbench
You should now be able to connect to MySQL from MySQL Workbench using the following settings:
Verifying the user's host settings will show:
select host, user from mysql.user; +-----------+---------------+ | host | user | +-----------+---------------+ | % | root | | localhost | healthchecker | | localhost | mysql.session | | localhost | mysql.sys | +-----------+---------------+
The above is the detailed content of How to Connect MySQL Workbench to a MySQL Docker Container?. For more information, please follow other related articles on the PHP Chinese website!