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

发布于:2024-10-17 ⋅ 阅读:(42) ⋅ 点赞:(0)

通过下面的命令安装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 Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 日 2024-10-13 14:22:26 CST; 25s ago
  Process: 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: 23
   Memory: 130.7M
   CGroup: /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 -A

Database 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 -A

Database 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 -A

mysql> 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;