mysql搭建使用

Mysql的搭建和使用

大数据项目主要分为三个模块

模块一:大数据集群的搭建及其部署优化和数据库运维

模块二:数据获取以及数据清洗

模块三:数据可视化

而Mysql则是模块一中的数据库方面的内容

废话不多说直接开始:

首先是安装Mysql,而安装Mysql则根据不同的操作系统进行安装(这里主要围绕Linux,操作系统是Centos7)

[Windows和Mac的自行找教程] 因为比赛使用的是Centos7所以我使用的就是Centos7

怎么安装Centos7我录制了一个视频自己看,不喜欢虚拟机的也可以找没用的机装系统

主分享链接:https://www.123865.com/s/JaZrjv-rifl 提取码:nLDd

备分享链接:https://www.123684.com/s/JaZrjv-rifl 提取码:nLDd

1.安装Mysql

要安装Mysql得先进行安装Mysql所需要的依赖(也可以使用默认官方的Yum源下载,就是很慢)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 先将镜像挂载
[root@localhost ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 is write-protected, mounting read-only

# 查看/mnt下有东西就是挂载成功了
[root@localhost mnt]# ls /mnt
CentOS_BuildTag EFI EULA GPL images isolinux LiveOS Packages repodata RPM-GPG-KEY-CentOS-7 RPM-GPG-KEY-CentOS-Testing-7 TRANS.TBL

# 然后去弄一个Yum
[root@localhost mnt]# cd /etc/yum.repos.d/
[root@localhost yum.repos.d]# ls
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Media.repo CentOS-Vault.repo
CentOS-CR.repo CentOS-fasttrack.repo CentOS-Sources.repo CentOS-x86_64-kernel.repo
[root@localhost yum.repos.d]# rm -rf *
[root@localhost yum.repos.d]# vi yum.repo
# 然后摁一下i键就可以进行编写了,编写完之后摁esc然后:wq就可以保存并退出了,应该都有Linux基础吧
# 最后更新元数据缓存就可以了
[root@localhost yum.repos.d]# yum makecache

# 最后进行安装依赖包
[root@localhost yum.repos.d]# yum install net-tools perl lrzsz -y

然后正式开始安装Mysql,我这里使用rpm包安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 首先将Mysql的压缩包放进Centos7中来
# 选定目录然后直接拖进来或者使用rz命令都可以,我这里使用rz来教学一波.如果出现乱码不要慌张,断开链接重新连接然后重新导入进来就可以了
# 导入完后该目录下就会出现你的包
[root@localhost opt]# ls
mysql-5.7.44-1.el7.x86_64.rpm-bundle.tar

# 然后进行解压,解压完后/root目录下就会有很多rpm包
[root@localhost opt]# tar -xvf mysql-5.7.44-1.el7.x86_64.rpm-bundle.tar -C /root/
mysql-community-client-5.7.44-1.el7.x86_64.rpm
mysql-community-common-5.7.44-1.el7.x86_64.rpm
mysql-community-devel-5.7.44-1.el7.x86_64.rpm
mysql-community-embedded-5.7.44-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.44-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.44-1.el7.x86_64.rpm
mysql-community-libs-5.7.44-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.44-1.el7.x86_64.rpm
mysql-community-server-5.7.44-1.el7.x86_64.rpm
mysql-community-test-5.7.44-1.el7.x86_64.rpm
[root@localhost opt]# ls /root/
anaconda-ks.cfg mysql-community-embedded-5.7.44-1.el7.x86_64.rpm mysql-community-libs-compat-5.7.44-1.el7.x86_64.rpm
mysql-community-client-5.7.44-1.el7.x86_64.rpm mysql-community-embedded-compat-5.7.44-1.el7.x86_64.rpm mysql-community-server-5.7.44-1.el7.x86_64.rpm
mysql-community-common-5.7.44-1.el7.x86_64.rpm mysql-community-embedded-devel-5.7.44-1.el7.x86_64.rpm mysql-community-test-5.7.44-1.el7.x86_64.rpm
mysql-community-devel-5.7.44-1.el7.x86_64.rpm mysql-community-libs-5.7.44-1.el7.x86_64.rpm

# 对rpm包进行安装,并且初始化以及启动Mysql
# 首先我们得先清理掉旧版的mariadb-libs
[root@localhost ~]# rpm -e --nodeps mariadb-libs
# 然后再进行安装
[root@localhost ~]# cd /root/
[root@localhost ~]# rpm -ivh mysql-community-common-5.7.44-1.el7.x86_64.rpm
[root@localhost ~]# rpm -ivh mysql-community-libs-5.7.44-1.el7.x86_64.rpm
[root@localhost ~]# rpm -ivh mysql-community-libs-compat-5.7.44-1.el7.x86_64.rpm
[root@localhost ~]# rpm -ivh mysql-community-client-5.7.44-1.el7.x86_64.rpm
[root@localhost ~]# rpm -ivh mysql-community-server-5.7.44-1.el7.x86_64.rpm

# 然后进行初始化
[root@localhost ~]# mysqld --initialize-insecure --user=mysql --datadir=/var/lib/mysql
# 最后启动Mysql
[root@localhost ~]# systemctl start mysqld

2.使用Mysql

既然已经安装完Mysql了,我们该如何去使用呢

首先我们先登录进Mysql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.44 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>
# 登录进去后就是这样的,这条命令中的-u后面是用户名,-p后面是密码,一般不直接在命令中写密码,因为没有设置了空密码,所以提示输入密码的时候直接回车就可以了

3.创建数据库

既然已经能够正常运行数据库了,这个时候我们就得需要对数据库进行增删改查的操作了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 创建一个数据库(使用create database + 数据库名称)
mysql> create database bigdata;
Query OK, 1 row affected (0.00 sec)

# 然后是使用数据库(使用use + 数据库名称)
mysql> use bigdata;
Database changed

# 要是不知道你有什么数据库,就可以查看所有数据库(使用show databases)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| bigdata |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)

# 要是你不想要这个数据库了,你可以进行删除,删库跑路(drop + 数据库名称)
mysql> drop database bigdata;
Query OK, 0 rows affected (0.01 sec)

4.创建表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# 有了数据库,你的库里面肯定得有表才能进行存放数据嘛
# 创建表( create table + 表名称(字段 类型); )
mysql> create table hkjcpdd(
-> id int,
-> name varchar(255));
Query OK, 0 rows affected (0.02 sec)

# 查看这个库中的所有表
mysql> show tables;
+-------------------+
| Tables_in_bigdata |
+-------------------+
| hkjcpdd |
+-------------------+
1 row in set (0.00 sec)

# 查看某个表的详细信息
mysql> desc hkjcpdd;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
2 rows in set (0.01 sec)

# 查看表中的所有信息(select * from 表名,*代表所有的意思)因为这个表没数据所以没东西
mysql> select * from hkjcpdd;
Empty set (0.00 sec)

# 删除表(drop table 表名)
mysql> drop table hkjcpdd;
Query OK, 0 rows affected (0.01 sec)

5.增加数据,这里使用一个常用的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 增加数据( insert into [表名] value values )

mysql> insert into hkjcpdd value (1, 'hkj'), (2, 'dcx'), (3, 'cbc'), (4, 'o
ojh');
Query OK, 4 rows affected (0.00 sec)
Records: 4 Duplicates: 0 Warnings: 0

# 然后查看数据是否添加进去了
mysql> select * from hkjcpdd;
+------+------+
| id | name |
+------+------+
| 1 | hkj |
| 2 | dcx |
| 3 | cbc |
| 4 | ojh |
+------+------+
4 rows in set (0.00 sec)

# 删除数据(delete from 表名 where 条件)
# 比如我想删除名字为cbc的数据
mysql> delete from hkjcpdd where name='cbc';
Query OK, 1 row affected (0.01 sec)

mysql> select * from hkjcpdd;
+------+------+
| id | name |
+------+------+
| 1 | hkj |
| 2 | dcx |
| 4 | ojh |
+------+------+
3 rows in set (0.00 sec)

# 修改数据(update [表] set [修改内容1, 修改内容2, ....] where [条件];)
# 比如我想修改名字为hkj的修改为hkjcpdd
mysql> update hkjcpdd set name='hkjcpdd' where name='hkj';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql> select * from hkjcpdd;
+------+---------+
| id | name |
+------+---------+
| 1 | hkjcpdd |
| 2 | dcx |
| 4 | ojh |
+------+---------+
3 rows in set (0.00 sec)

6.查找查看数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 查看查找数据就是根据不同的条件找出需要的数据
# 命令:select 显示的内容 from 表名 where 条件;
# 比如我想查看id为2的是哪个人
mysql> select * from hkjcpdd where id=2;
+------+------+
| id | name |
+------+------+
| 2 | dcx |
+------+------+
1 row in set (0.00 sec)

# 比如我想知道hkjcpdd是什么id
mysql> select id from hkjcpdd where name='hkjcpdd';
+------+
| id |
+------+
| 1 |
+------+
1 row in set (0.00 sec)