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

[Mariadb] 数据库学习笔记

  • 在Linux中安装MySQL并登录,YUM方式:yum -y install mysql-common.x86_64 mysql-devel.x86_64 mysql-server.x86_64
  • mariadb 介绍
  • 安装启服务
    • 环境前提:关闭防火墙、selinux、YUM源
  • 初始配置
    • 修改密码
  • 密码策略,默认策略是1
      • show variables; 查所有变量
      • show variables like "%变量%"; 查特定的变量参数
      • 临时:
      • 永久:
  • MySQL基本操作
    • 连接SQL服务
    • SQL命令使用规则
    • SQL命令分类
    • 库管理命令:库类似于文件夹,用来存储表
      • 综合练习

在Linux中安装MySQL并登录,YUM方式:yum -y install mysql-common.x86_64 mysql-devel.x86_64 mysql-server.x86_64

1 关闭selinux:

[root@control ~]# getenforce
Enforcing

2 关防火墙
[root@control ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2023-05-16 16:38:22 CST; 21min ago
     Docs: man:firewalld(1)
 Main PID: 858 (firewalld)
    Tasks: 2 (limit: 6044)
   Memory: 32.4M
   CGroup: /system.slice/firewalld.service
           └─858 /usr/libexec/platform-python -s /usr/sbin/firewalld --nofork --nopid

May 16 16:38:20 control systemd[1]: Starting firewalld - dynamic firewall daemon...
May 16 16:38:22 control systemd[1]: Started firewalld - dynamic firewall daemon.
May 16 16:38:23 control firewalld[858]: WARNING: AllowZoneDrifting is enabled. This is conside>
[root@control ~]# systemctl stop firewalld.service
[root@control ~]# systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Tue 2023-05-16 16:59:43 CST; 1s ago
     Docs: man:firewalld(1)
  Process: 858 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, st>
 Main PID: 858 (code=exited, status=0/SUCCESS)

May 16 16:38:20 control systemd[1]: Starting firewalld - dynamic firewall daemon...
May 16 16:38:22 control systemd[1]: Started firewalld - dynamic firewall daemon.
May 16 16:38:23 control firewalld[858]: WARNING: AllowZoneDrifting is enabled. This is conside>
May 16 16:59:43 control systemd[1]: Stopping firewalld - dynamic firewall daemon...
May 16 16:59:43 control systemd[1]: Stopped firewalld - dynamic firewall daemon.


3、启服务
[root@control ~]# systemctl start mysqld.service
[root@control ~]# systemctl status mysqld.service
● mysqld.service - MySQL 8.0 database server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2023-05-16 17:00:29 CST; 4s ago
  Process: 10551 ExecStartPost=/usr/libexec/mysql-check-upgrade (code=exited, status=0/SUCCESS)
  Process: 10424 ExecStartPre=/usr/libexec/mysql-prepare-db-dir mysqld.service (code=exited, s>
  Process: 10400 ExecStartPre=/usr/libexec/mysql-check-socket (code=exited, status=0/SUCCESS)
 Main PID: 10508 (mysqld)
   Status: "Server is operational"
    Tasks: 39 (limit: 6044)
   Memory: 470.4M
   CGroup: /system.slice/mysqld.service
           └─10508 /usr/libexec/mysqld --basedir=/usr

May 16 17:00:21 control systemd[1]: Starting MySQL 8.0 database server...
May 16 17:00:21 control mysql-prepare-db-dir[10424]: Initializing MySQL database
May 16 17:00:29 control systemd[1]: Started MySQL 8.0 database server.


4、登录
[root@control ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
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,是没有密码的
进入方法:

[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)


综合练习

[MySQL / Mariadb] 数据库学习-Linux中安装MySQL,YUM方式_第1张图片

你可能感兴趣的:(数据库,数据库,mariadb,学习)