Guide to use mysql containers in Docker, and mapping through ports, and external access
Construct a container
docker run --name some-mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql
- –name specified container name
- -P parameters Map the 3306 port in the container with the local 3306 interface
- -e adds an environment variable MySQL_ROOT_PASSWORD, which is defined as the root password
Configure external IP access permissions
The current database is already running in the container, but it is still unable to access the outside, because the access right of external IP needs to be configured;
docker exec -it hnister-mysql /bin/bash
Enter the container and configure mysql permissions
# Log in MySQL
MySQL -UROOT -PMY -SECRET -PW
# After entering, USE MySQL database:
mysql> use mysql;
# Add ROOT users can remotely access the end password to your external access password
MySQL> Grant All Privileges on*.* to 'root'@'%' identified by 'password';
mysql> update `mysql`.`user` set `Grant_priv` = 'Y' where `user` = 'root';
mysql> delete from user where user='root' and host='localhost';
mysql> flush privileges;