[单master节点k8s部署]39.安装mysql

embedded/2024/10/22 4:19:19/

通过下面的命令安装mysql。首先下载mysql的rpm包。mysql-community-release-el7-5.noarch.rpm 这个包的作用是将 MySQL 的官方 YUM 仓库添加到系统中,随后通过yum install来安装mysql

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-server 

设置权限,将 /var/lib/mysql 的所有者和组设置为 mysql 确保 MySQL 服务可以读取和写入数据。

chown mysql:mysql -R /var/lib/mysql

随后进行mysql的初始化。执行以下命令。有的教程可能会叫你执行mysql --initialize,但是这条语句是mySQL5.7中的功能,我使用的是5.6,这条命令不被支持。

sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

初始化成功后,启动mysqld,并查看其状态,如果是active的,则mysql 安装成功。

[root@master 36microservice]# systemctl start mysqld
[root@master 36microservice]# systemctl status mysqld
● mysqld.service - MySQL Community ServerLoaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)Active: active (running) since 日 2024-10-13 14:22:26 CST; 25s agoProcess: 70463 ExecStartPost=/usr/bin/mysql-systemd-start post (code=exited, status=0/SUCCESS)Process: 70445 ExecStartPre=/usr/bin/mysql-systemd-start pre (code=exited, status=0/SUCCESS)Main PID: 70461 (mysqld_safe)Tasks: 23Memory: 130.7MCGroup: /system.slice/mysqld.service├─70461 /bin/sh /usr/bin/mysqld_safe --basedir=/usr└─70639 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mysqld.log --pid-file=/v...10月 13 14:22:24 master systemd[1]: Starting MySQL Community Server...
10月 13 14:22:25 master mysqld_safe[70461]: 241013 14:22:25 mysqld_safe Logging to '/var/log/mysqld.log'.
10月 13 14:22:26 master mysqld_safe[70461]: 241013 14:22:26 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
10月 13 14:22:26 master systemd[1]: Started MySQL Community Server.

随后输入mysql,可以看到直接登录进了mysql数据库,但是这是不安全的,这里利用下面语句进行账号密码的设置。

[root@master 36microservice]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.51 MySQL Community Server (GPL)Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.

通过mysqladmin -uroot password "root"的命令,设置用户名和密码。 

[root@master 36microservice]# mysqladmin -u root password "root"
Warning: Using a password on the command line interface can be insecure.[root@master 36microservice]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.51 MySQL Community Server (GPL)Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.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> 
创建数据库
mysql> create database tb_order;
mysql> create database tb_stock;
mysql> create database tb_product;

此时查看数据库,可以看到我们创建的数据库。

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| tb_order           |
| tb_product         |
| tb_stock           |
+--------------------+
6 rows in set (0.03 sec)
传入数据
mysql> use tb_order
Database changed
mysql> source /root/yaml_file/36microservice/mysql_data/order.sql
mysql> use tb_product
Database changed
mysql> source /root/yaml_file/36microservice/mysql_data/product.sql
mysql> use tb_stock
Database changed
mysql> source /root/yaml_file/36microservice/mysql_data/stock.sql

查看传入的数据。此时的order数据库还是空的。 

mysql> use tb_order;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> show tables;
+--------------------+
| Tables_in_tb_order |
+--------------------+
| orders             |
+--------------------+
1 row in set (0.00 sec)mysql> select * from orders;
Empty set (0.00 sec)mysql> use tb_stock;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -ADatabase changed
mysql> show tables-> ;
+--------------------+
| Tables_in_tb_stock |
+--------------------+
| stock              |
+--------------------+
1 row in set (0.00 sec)mysql> select * from stock;
+----+---------+-------------+------------+
| id | prod_id | sales_stock | real_stock |
+----+---------+-------------+------------+
|  1 |       1 |          99 |         99 |
|  2 |       2 |          88 |         88 |
|  3 |       3 |          77 |         77 |
|  4 |       4 |          66 |         66 |
+----+---------+-------------+------------+
4 rows in set (0.00 sec)
mysql> use tb_product;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -Amysql> show tables;
+----------------------+
| Tables_in_tb_product |
+----------------------+
| product              |
+----------------------+
1 row in set (0.00 sec)mysql> select * from product;
+----+-----------------+----------+
| id | product_name    | price    |
+----+-----------------+----------+
|  1 | 手机            |   99.990 |
|  2 | 大彩电          |  999.000 |
|  3 | 洗衣机          |  100.000 |
|  4 | 超级大冰箱      | 9999.000 |
+----+-----------------+----------+
4 rows in set (0.00 sec)
 访问授权

mysql数据库进行授权,从而规定谁可以访问这个数据库。这里进行了两个网段的授权,一个是10.244.%.%,这代表k8s集群中所有pod的网段。另一个是192.168.%.%,代表了集群中的所有虚拟机的网段。identified by后面的内容是mysql的密码。

mysql> grant all on *.* to 'root'@'10.244.%.%' identified by 'root'mysql> grant all on *.* to 'root'@'192.168.%.%' identified by 'root';
Query OK, 0 rows affected (0.00 sec)mysql> flush privileges;
Query OK, 0 rows affected (0.02 sec)

如果你希望 root 用户可以从任何主机连接,可以使用以下授权,这个对于打包发布的微服务的数据库是非常必要的。 

grant all on *.* to 'root'@'%' identified by 'root';
flush privileges;


http://www.ppmy.cn/embedded/129463.html

相关文章

连锁收银系统

商淘云连锁管理系统助力连锁企业实现“人货账”全方位数字化管理,它依托连锁品牌进销存管理实现门店订货、线下收银、线上商城、会员营销等一体化管理。 门店订货补货支持连锁直营、加盟 不同门店不同进货价、不同门店不同商品、不同门店在线或者账期支付、门店PC或…

Axios 的基本使用与 Fetch 的比较、在 Vue 项目中使用 Axios 的最佳实践

文章目录 1. 引言2. Axios 的基本使用2.1 安装 Axios2.2 发起 GET 请求2.3 发起 POST 请求2.4 请求拦截器2.5 设置全局配置 3. Axios 与 Fetch 的比较3.1 Axios 与 Fetch 的异同点3.2 Fetch 的基本使用3.3 使用 Fetch 处理 POST 请求 4. 讨论在 Vue 项目中使用 Axios 的最佳实践…

docker 和 containerd 关系

containerd 是一个开源的容器运行时,它是用来管理容器生命周期的守护进程。containerd 支持 Docker 和其他容器格式,并且是许多现代容器编排系统(如 Kubernetes)的基础组件之一。 containerd 提供了一个命令行工具 ctr&#xff0…

MyBatis 动态 SQL 详解

1. 什么是动态 SQL? 在使用 MyBatis 进行数据库查询时,可能会遇到一些需要根据条件动态生成 SQL 语句的情况。MyBatis 提供了强大的动态 SQL 支持,通过标签和条件语句,可以让 SQL 语句根据不同的输入参数动态生成。这大大提高了代…

通过OpenCV实现 Lucas-Kanade 算法

目录 简介 Lucas-Kanade 光流算法 实现步骤 1. 导入所需库 2. 视频捕捉与初始化 3. 设置特征点参数 4. 创建掩模 5. 光流估计循环 6. 释放资源 结论 简介 在计算机视觉领域,光流估计是一种追踪物体运动的技术。它通过比较连续帧之间的像素强度变化来估计图…

Python数据分析工具OpenCV用法示例

Python数据分析工具OpenCV是一个强大的计算机视觉库,提供了丰富的图像处理算法和功能,支持多种编程语言,包括Python、C、C#等。以下是OpenCV在Python中的一些常见用法示例: 一、图像读取、显示与保存 读取图像 import cv2 im…

Springboot中基于 IP 地址的请求速率限制拦截器

基于 IP 地址的请求速率限制拦截器&#xff0c;使用了 Bucket4j 库来管理请求的令牌桶。下面是对代码的详细解释&#xff0c;以及如何在触发请求拒绝时将 IP 地址加入黑名单的实现。 导入依赖 <dependency><groupId>com.github.vladimir-bukhtoyarov</groupId…

FFmpeg 怎样根据图片和文本生成视频

使用FFmpeg根据图片和文本生成视频&#xff0c;你可以使用image2过滤器来处理图片&#xff0c;并使用subtitles过滤器来添加文本。以下是一个基本的命令行示例&#xff0c;它将图片转换为视频&#xff0c;并将文本作为字幕叠加&#xff1a; ffmpeg -loop 1 -i image.jpg -vf &…