Docker version 20.10.5
MySQL5.7
Centos 7.8
docker pull mysql:5.7
说明:如果不执行该步骤,执行创建MySQL容器时会自动拉取镜像:docker pull mysql:latest
。
# mkdir -p /usr/local/mysql/data /usr/local/mysql/logs /usr/local/mysql/conf # chmod -R 755 /usr/local/mysql/data # chmod -R 755 /usr/local/mysql/logs # chmod -R 755 /usr/local/mysql/conf
[mysqld] basedir=/usr/local/mysql datadir=/usr/local/mysql/data port=3306 character_set_database=utf8 character_set_server=utf8 user=mysql slow_query_log=on slow_query_log_file=/usr/local/mysql/logs/slow.log long_query_time=0.3 default-storage-engine=INNODB innodb_buffer_pool_size=64M innodb_purge_threads = 1 innodb_log_buffer_size=2M innodb_log_file_size = 64M innodb_thread_concurrency=8 innodb_lock_wait_timeout = 120 innodb_flush_log_at_trx_commit=1 max_connections=512 query_cache_size=0 tmp_table_size=18M thread_cache_size=8 myisam_max_sort_file_size=64G myisam_sort_buffer_size=35M key_buffer_size=25M read_buffer_size=64K read_rnd_buffer_size=256K sort_buffer_size=16M log-error=/usr/local/mysql/logs/mysql.log sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION [mysql] default-character-set=utf8 character_set_database=utf8 [client] port=3306 default-character-set=utf8
说明:该配置文件存放在上文创建的/usr/local/mysql/conf
目录下
# sudo docker run --restart=always -p 3306:3306 --name db.mysql -v /usr/local/mysql/conf:/etc/mysql -v /usr/local/mysql/my.cnf:/etc/mysql/my.cnf -v /usr/local/mysql/logs:/var/log/mysql -v /usr/local/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=mFF!Wmh\& -d mysql:5.7
MySQL环境变量配置说明:
MYSQL_ROOT_PASSWORD : 指定root用户初始密码,例中为 mFF!Wmh&,还可以配置其它 实践时发现,以下3个配置不起作用 MYSQL_DATABASE : 运行时需要创建的数据库名称; MYSQL_USER : 运行时需要创建用户名,与MYSQL_PASSWORD一起使用; MYSQL_PASSWORD : 运行时需要创建的用户名对应的密码,与MYSQL_USER一起使用; 以下配置未验证过 MYSQL_ALLOW_EMPTY_PASSWORD : 是否允许root用户的密码为空,该参数对应的值为:yes; MYSQL_RANDOM_ROOT_PASSWORD:为root用户生成随机密码; MYSQL_ONETIME_PASSWORD : 设置root用户的密码必须在第一次登陆时修改(只对5.6以上的版本支持)。 MYSQL_ROOT_PASSWORD 和 MYSQL_RANDOM_ROOT_PASSWORD 两者必须有且只有一个。
# docker exec -it db.mysql /bin/bash root@0b023eb3b811:/# root@0b023eb3b811:/# mysql -uroot -pmH1FF\!Kemh\& mysql> exit Bye root@0b023eb3b811:/# exit #
如上,成功了
注意:登录时,密码如果包含特殊字符,需要进行转义,否则会报类似如下错误
bash: !Kemh: event not found