2023-10-18 MySQL学习

2023-10-17  本文已影响0人  沉潜_2f21

一、docker 安装 mysql

1.1 安装mysql
sudo docker pull mysql:8.0.20mkdir -p /mydata/mysql/datamkdir -p /mydata/mysql/conftouch /mydata/mysql/my.cnf
1.2 修改 mydata/mysql/my.cnf 下的信息
vi /mydata/mysql/my.cnf 在my.cnf中添加如下信息[mysqld]user=mysqlcharacter-set-server=utf8default_authentication_plugin=mysql_native_passwordsecure_file_priv=/var/lib/mysqlexpire_logs_days=7sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTIONmax_connections=1000lower_case_table_names = 1 [client]default-character-set=utf8 [mysql]default-character-set=utf8

二、docker 镜像挂载 mysql

2.1 挂载 mysql,指定容器,挂载目录
sudo docker run --restart=always -p 3307:3307 --name mysql \-v /mydata/mysql/data:/var/lib/mysql \-v /mydata/mysql/conf:/etc/mysql/conf.d \-v /mydata/mysql/my.cnf:/etc/mysql/my.cnf \-e MYSQL_ROOT_PASSWORD=<密码> \-d mysql:8.0.20
2.2 输入 docker 命令,查看docker 挂载
docker ps -a
image.png
2.3 mysql目录映射
vi /mydata/mysql/conf/my.conf  [client]default-character-set=utf8[mysql]default-character-set=utf8[mysqld]init_connect='SET collation_connection = utf8_unicode_ci'init_connect='SET NAMES utf8'character-set-server=utf8collation-server=utf8_unicode_ciskip-character-set-client-handshakeskip-name-resolve
2.4 重启 mysql,并 设置docker 启动时自动启动mysql
docker restart mysql docker update mysql --restart=always

三、docker 镜像下修改 mysql 密码

3.1 使用命令,进入 docker 挂载下的 msyql 中
docker ps -a -- CONTAINER IDdocker exec -it 'CONTAINER ID<msyql的挂载id>' /bin/bash 
image.png

出现这个输入密码正确

3.2 进入 挂载的 mysql中
1. 输入命令,进入msyql 输入密码
  mysql -uroot -p 
2. 修改密码:    
  ALTER USER 'root'@'%' IDENTIFIED BY '<new password>' PASSWORD EXPIRE NEVER;   
  ALTER USER 'root'@'localhost' IDENTIFIED BY '<new password>';     # 注:这两个密码需要一样   
3.退出,重启 docker挂载的mysql
  exit; 
  docker start 'CONTAINER ID<msyql的挂载id>'
image.png
上一篇下一篇

猜你喜欢

热点阅读