Docker部署MySQL8数据库

1、准备工作

docker pull mysql:8.0.27
Pwd="/data/software/mysql"
mkdir ${Pwd}/{
    
    data,logs} -p
chmod 777 ${Pwd}/logs

2、添加配置文件

cat > ${Pwd}/my.cnf << EOF
[mysqld]
user=mysql
pid-file=/var/run/mysqld/mysqld.pid
socket=/var/run/mysqld/mysqld.sock
datadir=/var/lib/mysql
symbolic-links=0
log-error=/var/log/mysql/mysqld.log
slow_query_log_file=/var/log/mysql/slow.log
secure-file-priv= NULL
skip_ssl
default-time-zone='+8:00'
lower_case_table_names=1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
log_bin_trust_function_creators=1
event_scheduler=ON
skip_name_resolve
long_query_time=3
slow_query_log=1
innodb_log_buffer_size=16M
innodb_log_file_size=128M
innodb_buffer_pool_size=1024M
key_buffer_size=256M
thread_cache_size=16
wait_timeout=28000
max_connections=1500
open_files_limit=102400
character-set-server=utf8
server_id=1
log-bin=mysqld-bin
default-authentication-plugin=mysql_native_password
explicit_defaults_for_timestamp=1

!includedir /etc/mysql/conf.d/
EOF

3、运行容器

docker run -itd --name mysql \
    -e MYSQL_ROOT_PASSWORD=NTQ34tg*@19VF \
    -v ${Pwd}/my.cnf:/etc/mysql/my.cnf \
    -v ${Pwd}/data:/var/lib/mysql \
    -v ${Pwd}/logs:/var/log/mysql \
    -v /etc/localtime:/etc/localtime \
    -p 3306:3306 \
    --restart=always mysql:8.0.27

4、连接数据库,测试服务是否正常

docker exec -it mysql mysql -uroot -pNTQ34tg*@19VF

mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.27 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

也可以使用工具测试是否可以连接
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45310323/article/details/133205161