1.docker search mysql
#查询mysql版本
2.docker pull mysql:8.0.15
#拉取mysql
3.创建mysql容器
docker stop mysql_8
docker rm mysql_8
docker run -d -e TZ=“Asia/Shanghai” -p 3306:3306 --name mysql_8 -e MYSQL_ROOT_PASSWORD=123456 -v /etc/localtime:/etc/localtime mysql:8.0.15
补充说明:
-v /etc/localtime:/etc/localtime 映射时区
-v /home/mysqld.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf 配置文档
-v /var/mysql/log/:/var/lib/mysql/ 日志目录(show variables like ‘general_log_file’;)
注意:运行多个容器时修改挂载目录及端口号
列子:
docker run -d -e TZ=“Asia/Shanghai” -p 3306:3306 --name mysql_5.6 -e MYSQL_ROOT_PASSWORD=123456 --restart=always -v /etc/localtime:/etc/localtime -v /opt/mysql_5.6_xxx/log/:/var/lib/mysql/ mysql:5.6
4.开启root远程访问
docker exec -it mysql_8 /bin/bash
mysql -u root -p
use mysql;
ALTER USER ‘root’@’%’ IDENTIFIED WITH mysql_native_password BY ‘123456’;
flush privileges;
select host,user,plugin,authentication_string from mysql.user;
注意:容器的内部的端口为3306,不能改变,当数据库有多个时,注意挂载的目录不要重复
mysql5.7:GRANT ALL PRIVILEGES ON . TO ‘root’@’%’ IDENTIFIED BY ‘123456’;