[MySQL / Mariadb] 数据库学习-Linux中安装MySQL,YUM方式

news/2024/7/5 2:30:50

[Mariadb] 数据库学习笔记

  • 在Linux中安装MySQL,YUM方式
  • mariadb 介绍
  • 安装启服务
  • 初始配置
    • 修改密码
  • 密码策略,默认策略是1
      • show variables; 查所有变量
      • show variables like "%变量%"; 查特定的变量参数
      • 临时:
      • 永久:
  • MySQL基本操作
    • 连接SQL服务
    • SQL命令使用规则
    • SQL命令分类
    • 库管理命令:库类似于文件夹,用来存储表
      • 综合练习

在Linux中安装MySQL,YUM方式

新安装的MySQL,是没有密码的
进入方法:

[root@control ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.17 Source distribution

Copyright (c) 2000, 2019, 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> alter user root@"localhost" identified by "111";
Query OK, 0 rows affected (0.00 sec)

[root@control ~]# mysql -uroot -p111

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 8.0.17 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

mariadb 介绍

1)常见软件
主流操作系统:Unix、Linux、Windows

			软件名		开源	跨平台	厂商
	Oracle		否	是	甲骨文
	MySQL		是	是	甲骨文
	SQL Server	否	否	微软
	DB2		否	是	IBM
	Redis		是	是	开源软件
	Memcached	是	是	开源软件
	MongoDB	是	是	开源软件

2)专业术语
	DB(DataBase)
		数据库
		依照某种数据模型进行组织并存放到存储器的数据集合
	DBMS(DataBase Management System)
		数据库管理系统
		用来操纵和管理数据库的服务软件
	DBS(DataBase System)
		数据库系统:即DB+DBMS
		指带有数据库并整合了数据库管理软件的计算机系统

3)MySQL介绍
	起源与发展:
		应用最广泛的开源数据库软件
			最早属于瑞典的MySQL AB公司
			2008年1月,MySQL AB被Sun收购
			2009年4月,SUN被Oracle收购
		崭新的开源分支MariaDB
			为应付 MySQL可能会闭源的风险而诞生
			由MySQL原作者 Widenius主导开发
			与MySQL保持最大程度兼容

4)特点与应用
	主要特点:
		适用于中小规模,关系型数据库系统
		支持Linux、Unix、Windows等多种操作系统
		支持Python、Java、Perl、PHP等编程语言
	典型应用环境:
		LAMP平台,与Apache HTTP Server组合
		LNMP平台,与Nginx组合

安装启服务

环境前提:关闭防火墙、selinux、YUM源

[root@myserver ~]# yum -y install mariadb-server
Last metadata expiration check: 1:24:11 ago on Thu 04 May 2023 09:23:23 AM UTC.
Package mariadb-server-3:10.3.17-1.module_el8.1.0+257+48736ea6.x86_64 is already installed.
Dependencies resolved.
Nothing to do.
Complete!
[root@myserver ~]# systemctl start mariadb
[root@myserver ~]# systemctl enable mariadb
[root@myserver ~]# systemctl status mariadb
● mariadb.service - MariaDB 10.3 database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since Thu 2023-05-04 06:21:27 UTC; 4h 26min ago
     Docs: man:mysqld(8)
           https://mariadb.com/kb/en/library/systemd/
 Main PID: 20175 (mysqld)
   Status: "Taking your SQL requests now..."
    Tasks: 31 (limit: 23978)
   Memory: 85.6M
   CGroup: /system.slice/mariadb.service
           └─20175 /usr/libexec/mysqld --basedir=/usr

相关参数:
			/etc/my.cnf	主配置文件
			/var/lib/mysql	数据库目录
			默认端口号	3306
			进程名		mysqld
			传输协议		TCP
			进程所有者	mysql
			进程所属组	mysql
			错误日志文件	/var/log/mariadb/mariadb.log

初始配置

数据库管理员名为root

默认仅允许root本机连接

首次登陆密码在安装软件时随机生成
		
随机密码存储在日志文件/var/log/mysqld.log里
		
连接命令:mysql  -h数据库地址  -u用户  -p密码

grep  password  /var/log/mariadb/mariadb.log			##查看初始化密码

mysql  -hlocalhost  -uroot  -p'密码'			##使用初始化密码登陆数据库

例:

[root@myserver ~]# mysql -uroot -p222
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 20
Server version: 10.3.17-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| aab                |
| aac                |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.003 sec)

MariaDB [(none)]>

修改密码

MariaDB [(none)]> alter user root@"localhost" identified by "111qwe..A";
Query OK, 0 rows affected (0.005 sec)

MariaDB [(none)]> exit
Bye
[root@myserver ~]# mysql -uroot -p111qwe..A
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 22
Server version: 10.3.17-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit
Bye
[root@myserver ~]#

密码策略,默认策略是1

修改密码策略
LOW(0) 长度
MEDIUM(1) 长度、数字、小写/大写、和特殊字符
STRONG(2) 长度、数字、小写/大写、和特殊字符、字典文件

show variables; 查所有变量

show variables like “%变量%”; 查特定的变量参数

临时:

	mysql  -uroot  -p密码				
	##临时修改密码策略,重启服务后会失效
	show  variables  like  "%password%";			
	##查看系统中和密码相关的变量
	set  global  validate_password_policy=0;			
	##设置密码策略为LOW,只检验长度
	set  global  validate_password_length=6;			
	##设置密码的长度最短为6位
	show  variables  like  "%password%";

永久:

	vim  /etc/my.cnf					##永久修改密码策略
	[mysqld]				
	validate_password_policy=0				
	##设置密码策略为LOW,只检验长度
	validate_password_length=6				
	##设置密码的长度最短为6位
	:wq
	
	systemctl  restart  mysqld
	mysql  -uroot  -p密码
	show  variables  like  "%password%";

例:
我这里找到的密码策略和变量没有练习中的那些

MariaDB [(none)]> show variables like "%password%";
+----------------------------+-------+
| Variable_name              | Value |
+----------------------------+-------+
| old_passwords              | OFF   |
| report_password            |       |
| strict_password_validation | ON    |
+----------------------------+-------+
3 rows in set (0.004 sec)

MySQL基本操作

连接SQL服务

连接方式:客户端连接MySQL服务的方法
命令行
web页面
安装图形软件
编写脚本(PHP、Java、Python…)
使用mysql 命令:
mysql -h服务器IP -u用户名 -p密码 [数据库名]
quit 或 exit 退出

2)数据存储流程
	客户端把数据存储到数据库服务器上的步骤
		连接数据库服务器
		建库			##相当于创建文件夹
		建表			##相当于创建文本文件
		插入记录			##相当于在文件文件中写入内容
		断开连接			

SQL命令使用规则

SQL命令不区分字母大小写(密码,变量值除外)
每条SQL命令以**;**结束
默认命令不支持Tab键自动补齐
\c 终止sql命令

SQL命令分类

	管理数据库使用SQL(结构化查询语言)
	DDL	数据定义语言:如 create、alter、drop
	DML	数据操作语言:如 insert、update、delete
	DCL	数据控制语言:如 grant、revoke
	DTL	数据事务语言:如 commit、rollback、savepoint

库管理命令:库类似于文件夹,用来存储表

	show  databases;		##显示已有的库
	select  user();		##显示连接用户
	use  库名;		##切换库
	select  database();		##显示当前所在的库
	create  database  库名;	##创建新库
	show  tables;		##显示已有的表
	drop  database  库名;	##删除库
例:
MariaDB [(none)]> show databases;     #显示已有的库
+--------------------+
| Database           |
+--------------------+
| aab                |
| aac                |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.003 sec)

MariaDB [(none)]> select user();    #显示连接用户
+----------------+
| user()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.002 sec)

MariaDB [(none)]> use mysql;     #切换库
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

MariaDB [mysql]> select database();     #显示当前所在的库
+------------+
| database() |
+------------+
| mysql      |
+------------+
1 row in set (0.003 sec)


MariaDB [mysql]> create database aaa;   #创建库
Query OK, 1 row affected (0.003 sec)

MariaDB [mysql]> show tables;    #显示当前库已有的表
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| column_stats              |
| columns_priv              |
| db                        |
| event                     |
| func                      |
| general_log               |
| gtid_slave_pos            |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| host                      |
| index_stats               |
| innodb_index_stats        |
| innodb_table_stats        |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| roles_mapping             |
| servers                   |
| slow_log                  |
| table_stats               |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| transaction_registry      |
| user                      |
+---------------------------+
31 rows in set (0.002 sec)

MariaDB [mysql]> show databases;    
+--------------------+
| Database           |
+--------------------+
| aaa                |
| aab                |
| aac                |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
6 rows in set (0.004 sec)



MariaDB [mysql]> drop database aaa;    # 删库
Query OK, 0 rows affected (0.003 sec)

MariaDB [mysql]> show databases;
+--------------------+
| Database           |
+--------------------+
| aab                |
| aac                |
| information_schema |
| mysql              |
| performance_schema |
+--------------------+
5 rows in set (0.001 sec)


综合练习

请添加图片描述


http://lihuaxi.xjx100.cn/news/1102183.html

相关文章

【转存】Go语言设计模式(推荐阅读)

文章目录 0.前言1.责任链模式 概念示例 登机过程测试程序运行结果2.命令模式 概念示例 电饭煲接收器电饭煲命令测试程序运行结果3.迭代器模式 概念示例 班级成员运行结果4.中介者模式 概念示例 飞机对象机场塔台测试程序运行结果5.备忘录模式 概念示例闯关游戏 测试程序运行结果…

Notepad++配置C语言环境和C++环境

背景: Notepad是我们经常使用的编辑器,我们可以用它编译和运行各种类型的文档,其中就包括了C和C文档。但是编译和运行C或者C文档首先要配置编译环境,下面给大家分享一下如何在NotePad配置C/C编译环境。 工具: NoteP…

Altium Designer中如何在顶层中添加对应端口

转载说明: 大众深度科普 https://jingyan.baidu.com/article/c33e3f4889f327ea15cbb584.html 版权归原作者所有;感谢原作者的分享; 转载到此,主要为了后期查看方便; 本经验简要介绍Altium Designer中如何在顶层中添…

neo4j结合gds实现最短路径算法

背景:Neo4j自带的cypher语句中的 shortestpath allShortestPaths 返回值内容非常有限,不易处理, 在实际生产环境中可用性极低, 且若带where条件查询时,查询效率极低 因此,使用Neo4j自带的插件如apoc来进行最短路径查询…

精妙绝伦的算法之舞:解密力扣“删除有序数组中的重复项”

本篇博客会讲解力扣“26. 删除有序数组中的重复项”这道题,这是题目链接。 老规矩,先来审题: 题目有对判题标准的详细解释: 接下来是2个示例: 还有提示: 其实这道题考察的是“去重算法”,即…

HBASE入门 基本shell命令(一)

一、登录连接shell $HBASE_HOME/bin/hbase shell二、基本命令 2.1help命令 help创建命名空间 create_namespace bigdata;查看命名空间 list_namespace命名空间default和habase是系统自带的 三、DDL 3.1创建表 create bigdata:student, {NAME > name, VERSIONS> 5}…

nvm管理node版本与node对应的npm的版本

五一长假回来打开电脑发现自己的vscode不能成功运行项目,npm run dev之后没有任何反应,于是我想着使用cmd打开试试,结果cmd打开后画面只有一个横着的光标再闪,停几秒后就自动关闭了,看其他的博主写的解决方法一一试过了…

npm install 安装包时,常用的-S 、-D 、-g与直接npm 有什么区别?

一、主要区别就是依赖配置写入package.json文件的位置不同而已 npm install 本身就有一个别名 npm i 👉 npm i module_name -S 即 npm install module_name --save 写入dependencies,发布到生产环境 这样安装是局部安装的,会写进…