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

发布于:2024-04-27 ⋅ 阅读:(32) ⋅ 点赞:(0)

下载镜像

[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