【MySQL】Install MySQL in Docker

Posted by 西维蜀黍 on 2021-10-26, Last Modified on 2022-04-01

Install

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

… where some-mysql is the name you want to assign to your container, my-secret-pw is the password to be set for the MySQL root user and tag is the tag specifying the MySQL version you want. See the list above for relevant tags.

# install the latest mysql with root's passord being 1234 and allowing remote access
$ docker run --name sw-mysql --restart=unless-stopped -e MYSQL_ROOT_HOST=% -e MYSQL_ROOT_PASSWORD=1234 -p 3306:3306 -d mysql

# enter the container
$ docker exec -it sw-mysql sh
  • if you want to access your mysql server remotely, you need mapping the port 3306 of the container to port 3306 of the host machine.
  • If you wanna run MySQL in another port, use -p 3307:3306, meaning map the container-port 3306 on the prefered TCP port (of your server), in this case it is 3307
  • Set the environment variable MYSQL_ROOT_HOST with wildcards to allow root connections from other hosts
# access mysql from another host
$ mysql -u root -h 192.168.18.134 -p1234

Reference