Docker安装MySQL

下载镜像

docker pull mysql/mysql-server:5.7 

运行服务

docker run --name=mysql5.7 -p 3306:3306 -p 33060:33060 -d mysql/mysql-server:5.7


初始化完成后,查看Docker的输出日志,将包含为root用户生成的随机密码

docker logs mysql5.7 2>&1 | grep GENERATED
GENERATED ROOT PASSWORD: Axegh3kAJyDLaRuBemecis&EShOs

连接服务

docker exec -it mysql5.7 mysql -uroot -p

输入MySQL生成的随机密码进入后修改默认密码:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'newpassword';

现在MySQL服务以及OK了,但还只能本地访问,不支持远程连接,报错误: ERROR 1130: Host * is not allowed to connect to this MySQL server

授权访问

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'glowdog123' WITH GRANT OPTION;
mysql> delete from mysql.user where user='root' and host='localhost';
mysql> FLUSH PRIVILEGES;

修改默认配置

进入容器后执行,可以根据需求修改默认配置。

cat > /etc/my.cnf <<EOF

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html

[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
character-set-server=utf8mb4
collation-server=utf8mb4_bin
default-storage-engine=INNODB
max_allowed_packet=256M
innodb_log_file_size=2GB
transaction-isolation=READ-COMMITTED
binlog_format=row


# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
EOF

修改后,exit退出,重启MySQL即可生效。

docker stop mysql5.7
docker start mysql5.7
0 0 投票数
Article Rating
订阅评论
提醒
guest
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x