当前位置: 首页 > news >正文

docker安装mysql数据库及简单使用

一、docker环境检查

- 1、检查docker运行环境

[root@lyx ~]# docker version
[root@lyx ~]# systemctl status docker

二、部署mysql数据库

  • 1、拉取mysql数据库镜像
[root@lyx ~]# docker pull mysql:8.0.36
  • 2、在宿主机创建目录实现数据的持久化
[root@lyx ~]# mkdir -pv /root/data/mysql/{conf,data,log}
[root@lyx ~]# tree data
data
└── mysql├── conf├── data└── log
  • 3、部署服务
[root@lyx ~]# docker run -d --name mysql --restart=always --privileged=true -v /root/mysql/log:/var/log/mysql -v /root/mysql/data:/var/lib/mysql -v /root/mysql/conf:/etc/mysql/conf.d -v /etc/localtime:/etc/localtime:ro -e MYSQL_ROOT_PASSWORD=123456 mysql:8.0.36-oracle 
命令行参数解释:
-d:后台运行mysql容器
--name mysql:容器的名称是mysql
--restart=always:总是和docker启动
--privileged=true:获取宿主机root权限
-v /root/mysql/log:/var/log/mysql:映射日志目录(宿主机目录:容器目录)
-v /root/mysql/data:/var/lib/mysql:映射数据目录(宿主机目录:容器目录)
-v /root/mysql/conf:/etc/mysql/conf.d:映射配置目录(宿主机目录:容器目录)
-v /etc/localtime:/etc/localtime:ro:容器时间与宿主机时间同步
-e MYSQL_ROOT_PASSWORD=123456:指定mysql环境变量,root用户名密码为123456(自定义)
mysql:8.0.36-oracle:使用的镜像名称

**

  • 4、查看容器的运行状态

**

[root@lyx ~]# docker ps -a | grep mysql
0acf0b68336c   mysql:8.0.36-oracle                                           "docker-entrypoint.s…"   21 minutes ago   Up 21 minutes         3306/tcp, 33060/tcp                         mysql

**

**

  • 5、登录数据库

**

**

[root@lyx ~]# docker exec -it mysql bash
bash-4.4# mysql -u root -p 
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.36 MySQL Community Server - GPLCopyright (c) 2000, 2024, 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> 

三、数据库简单使用

  • 1、检查当前数据库用户情况
mysql> select user,host,authentication_string from mysql.user;
  • 2、创建用户
    2.1、创建本地登录用户
mysql> create user lv@'localhost' identified by '123321';
Query OK, 0 rows affected (0.05 sec)

2.2、创建远程登录用户

mysql> create user ya@'192.168.16.%' identified by '456654';
Query OK, 0 rows affected (0.01 sec)
  • 3、用户登录
bash-4.4# mysql -u lv -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 8.0.36 MySQL Community Server - GPLCopyright (c) 2000, 2024, 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> 
  • 4、检查当前登录用户
mysql> select user();
+--------------+
| user()       |
+--------------+
| lv@localhost |
+--------------+
1 row in set (0.02 sec)
  • 5、删除用户
mysql> drop user ya@'192.168.16.%';
Query OK, 0 rows affected (0.01 sec)
  • 6、给用户授权
mysql> grant Alter,Create,Delete,Drop,Insert,Select,Update on mysql.* to lv@'localhost';
Query OK, 0 rows affected, 1 warning (0.08 sec)
  • 7、检查用户权限
mysql> show grants for lv@'localhost';
+--------------------------------------------------------------------------------------------+
| Grants for lv@localhost                                                                    |
+--------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO `lv`@`localhost`                                                     |
| GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, ALTER ON `mysql`.* TO `lv`@`localhost` |
+--------------------------------------------------------------------------------------------+
2 rows in set (0.01 sec)
  • 8、回收权限
mysql> revoke Select on mysql.* from lv@'localhost';
Query OK, 0 rows affected, 1 warning (0.01 sec)

相关文章:

  • 自恢复式保险丝如何实现自恢复?
  • 走线宽度对高频插入损耗的影响
  • Blender——建构、粒子、灯光、动画
  • TensorFlow Serving学习笔记2: 模型服务
  • Linux下成功编译CPU版Caffe的保姆级教程(基于Anaconda Python3.8 包含完整可用Makefile.config文件)
  • 使用预训练卷积神经模型进行分类(MATLAB例)
  • 【读点论文】A Survey on Open-Set Image Recognition
  • JavaScript基础-事件委托(代理、委派)
  • 【系统分析师】2009年真题:案例分析-答案及详解
  • FreeRTOS任务相关API简介
  • KJY0047-J1阶段测试
  • 【游资悟道】陈小群成长历史与股市悟道心法
  • 玩转Docker | 使用Docker部署Blinko个人笔记工具
  • C++操作系统与网络编程(针对特定岗位)
  • 使用 MoviePy 实现图像序列合成视频并添加背景音乐
  • 有些植物果实结在土里,但是花开在枝叶上,果不结在花上,花需要授粉才能结果吗?不用授粉的话,开花的意义在哪里?
  • (javaSE)抽象类和接口:抽象类概念语法和特性, 抽象类的作用;接口的概念 接口特性 实现多个接口 接口间的继承 Object类
  • Linux操作系统之运维常用命令
  • JUC核心解析系列(五)——执行框架(Executor Framework)深度解析
  • 音频驱动数字人13款深度评测
  • 局网站建设工作征求意见/网站流量查询工具
  • 网站建设推广是什么工作/谷歌seo公司
  • 作风建设年活动网站/常用于网站推广的营销手段是
  • 制作班徽的小程序/无锡网站制作优化
  • 做网站先做前台还是后台/360搜索引擎下载
  • 如何建设好英文网站/seo收费标准多少