Linux下怎么快速部署MySQL服务,并使用

下载镜像

[zry@localhost ~]$ docker pull mysql
Using default tag: latest
latest: Pulling from library/mysql
bce031bc522d: Pull complete 
cf7e9f463619: Pull complete 
105f403783c7: Pull complete 
878e53a613d8: Pull complete 
2a362044e79f: Pull complete 
6e4df4f73cfe: Pull complete 
69263d634755: Pull complete 
fe5e85549202: Pull complete 
5c02229ce6f1: Pull complete 
7320aa32bf42: Pull complete 
Digest: sha256:4ef30b2c11a3366d7bb9ad95c70c0782ae435df52d046553ed931621ea36ffa5
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:latest
[zry@localhost ~]$ 

启动镜像,使用宿主机的共享网络,设定管理员账户密码是root。

docker run --name mysql-container -e MYSQL_ROOT_HOST=localhost -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -d mysql:latest

启动后查看

[zry@localhost ~]$ docker ps
CONTAINER ID   IMAGE              COMMAND                   CREATED          STATUS          PORTS                                                                                                                                                                                                                               NAMES
40a2d169caea   mysql:latest       "docker-entrypoint.s…"   34 seconds ago   Up 30 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp                                                                                                                                                                                mysql-container

进到镜像内部

[zry@localhost ~]$ docker exec -it mysql-container /bin/bash
bash-4.4# ls
bin  boot  dev  docker-entrypoint-initdb.d  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

进入MySQL服务

bash-4.4# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 8.2.0 MySQL Community Server - GPL

Copyright (c) 2000, 2023, 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> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'IDENTIFIED BY 'root' WITH GRANT OPTION' at line 1
mysql> CREATE USER 'root'@'%' IDENTIFIED BY 'root';
Query OK, 0 rows affected (0.12 sec)

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION;
Query OK, 0 rows affected (0.42 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.04 sec)

mysql> 

使用时:
![[Pasted image 20240115162353.png]]

![[Pasted image 20240115162544.png]]


分享一个有趣的 学习链接:https://xxetb.xet.tech/s/HY8za

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-23 06:30:07       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-23 06:30:07       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-23 06:30:07       87 阅读
  4. Python语言-面向对象

    2024-04-23 06:30:07       96 阅读

热门阅读

  1. 开发一款游戏,需要注意哪些问题?

    2024-04-23 06:30:07       45 阅读
  2. DBA搞钱之路

    2024-04-23 06:30:07       39 阅读
  3. Unity实现一个简单的状态机Demo

    2024-04-23 06:30:07       33 阅读
  4. 为什么删除node_modules文件夹很慢

    2024-04-23 06:30:07       32 阅读
  5. 《SQLite系列》SQLite数据库常用命令大全

    2024-04-23 06:30:07       39 阅读
  6. 「PHP系列」PHP Cookie/Session详解

    2024-04-23 06:30:07       41 阅读
  7. 基于大语言模型的本地知识库问答系统构建方案

    2024-04-23 06:30:07       35 阅读
  8. css三角的巧妙运用

    2024-04-23 06:30:07       93 阅读
  9. HCIP-Datacom-ARST必选题库_22_SDWAN【1道题】

    2024-04-23 06:30:07       34 阅读
  10. 《前端面试题》- TypeScript - TypeScript的优/缺点

    2024-04-23 06:30:07       35 阅读
  11. Android Studio 快捷键及使用技巧汇总

    2024-04-23 06:30:07       35 阅读