Linux_服务管理—mysql基础

1. 关系型数据库介绍


1.1 数据结构模型

数据结构模型主要有: 层次模型、网状结构、关系模型

关系模型:

  • 二维关系:row,column

数据库管理系统:DBMS
关系型数据库管理系统:Relational,RDBMS

1.2 RDBMS专业名词

常见的关系型数据库管理系统:

  • MySQL:MySQL,MariaDB,Percona-Server
  • PostgreSQL:简称为pgsql
  • Oracle
  • MSSQL

SQL: Structure Query Language,结构化查询语言

约束: constraint,向数据表提供的数据要遵守的限制

  • 主键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。且必须提供数据,不能为空(NOT NULL)。
    • 一个表只能存在一个
  • 惟一键约束:一个或多个字段的组合,填入的数据必须能在本表中唯一标识本行。允许为空(NULL)
    • 一个表可以存在多个
  • 外键约束:一个表中的某字段可填入数据取决于另一个表的主键已有的数据
  • 检查性约束

索引: 将表中的一个或多个字段中的数据复制一份另存,并且这些数据需要按特定次序排序存储

1.3 关系型数据库的常见组件

关系型数据库的常见组件有:
数据库database、表:table,由行(row)和列(column)组成、索引:index、视图:view、用户:user、权限:privilege、存储过程:procedure、存储函数:function、触发器:trigger、事件调度器:event scheduler

1.4 SQL语句

SQL语句有三种类型:

  • DDL:Data Defination Language,数据定义语言
    • CREATE:创建
    • DROP:删除
    • ALTER:修改
  • DML:Data Manipulation Language,数据操纵语言
    • INSERT:向表中插入数据
    • DELETE:删除表中数据
    • UPDATE:更新表中数据
    • SELECT:查询表中数据
  • DCL:Data Control Language,数据控制语言
    • GRANT:授权
    • REVOKE:移除授权

2. mysql安装与配置


2.1 mysql安装

mysql安装方式有三种:

  • 源代码:编译安装
  • 二进制格式的程序包:展开至特定路径,并经过简单配置后即可使用
  • 程序包管理器管理的程序包:
    • rpm:有两种
      • OS Vendor:操作系统发行商提供的
      • 项目官方提供的
    • deb
#配置mysql的yum源
wget -O /usr/src/mysql57-community-release-el7-10.noarch.rpm \
http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
rpm -Uvh /usr/src/mysql57-community-release-el7-10.noarch.rpm

#安装mysql5.7
yum -y install mysql-community-server mysql-community-client  \
mysql-community-common mysql-community-devel
#配置mysql的yum源
[root@node01-linux ~]# yum -y install wget
···

[root@node01-linux ~]# wget -O /usr/src/mysql57-community-release-el7-10.noarch.rpm http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm

[root@node01-linux ~]# rpm -Uvh /usr/src/mysql57-community-release-el7-10.noarch.rpm
warning: /usr/src/mysql57-community-release-el7-10.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql57-community-release-el7-10 ################################# [100%]

#安装mysql5.7
[root@node01-linux ~]# yum -y install mysql-community-server mysql-community-client mysql-community-common mysql-community-devel
···
Installed:
  mysql-community-client.x86_64 0:5.7.30-1.el7                           
  mysql-community-common.x86_64 0:5.7.30-1.el7                           
  mysql-community-devel.x86_64 0:5.7.30-1.el7                            
  mysql-community-libs.x86_64 0:5.7.30-1.el7                             
  mysql-community-libs-compat.x86_64 0:5.7.30-1.el7                      
  mysql-community-server.x86_64 0:5.7.30-1.el7                           

Dependency Installed:
  net-tools.x86_64 0:2.0-0.25.20131004git.el7                            

Dependency Updated:
  postfix.x86_64 2:2.10.1-9.el7                                          

Replaced:
  mariadb-libs.x86_64 1:5.5.56-2.el7                                     

Complete!



2.2 mysql配置

#启动mysql并设置开机自动启动
[root@node01-linux ~]# systemctl enable --now mysqld
[root@node01-linux ~]# systemctl status mysqld

#确保3306端口已经监听起来
[root@node01-linux ~]# ss -antl

#在日志文件中找出临时密码
[root@node01-linux ~]# grep "password" /var/log/mysqld.log

#使用获取到的临时密码登录mysql
[root@node01-linux ~]# mysql -uroot -p'ykp<ibcSF5ra'
或者
[root@node01-linux ~]# mysql -uroot -p
Enter password: 	//此处输入密码,可以直接复制你的密码粘贴至此处,也可手动输入
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30

Copyright (c) 2000, 2020, 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登录密码
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.01 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'mysql123456';
Query OK, 0 rows affected (0.01 sec)

mysql> quit
Bye


//为避免mysql自动升级,这里需要卸载最开始安装的yum源
[root@node01-linux ~]# rpm -e mysql57-community-release

2.3 mariadb安装与配置

安装mariadb相关包


[root@node02-linux ~]# yum -y install mariadb*

2.4 mariadb安装与配置

#启动mariadb并设置开机自动启动
[root@node02-linux ~]# systemctl enable --now mariadb
[root@node02-linux ~]# systemctl status mariadb

#确保3306端口已经监听起来

#默认root账户登陆,不需要密码
[root@node02-linux ~]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
Server version: 5.5.65-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)]> quit
Bye

//修改mariadb登录密码,用password函数加密
MariaDB [(none)]> set password = password('mysql123456');

//登陆
[root@node02-linux ~]# mysql -uroot -p'mysql123456'
MariaDB [(none)]> quit
Bye

3. mysql的程序组成

客户端

  • mysql:CLI交互式客户端程序( 图形化navicat收费workbench免费开源 )
  • mysql_secure_installation:安全初始化,强烈建议安装完以后执行此命令
  • mysqldump:mysql备份工具
  • mysqladmin

服务器端

  • mysqld

3.1 mysql工具使用

//语法:mysql [OPTIONS] [database]
//常用的OPTIONS:
	-uUSERNAME      //指定用户名,默认为root
    -hHOST          //指定服务器主机,默认为localhost,推荐使用ip地址
    -pPASSWORD      //指定用户的密码
    -P#             //指定数据库监听的端口,这里的#需用实际的端口号代替,如-P3307
    -V              //查看当前使用的mysql版本
    -e          //不登录mysql执行sql语句后退出,常用于脚本


[root@node01-linux ~]# mysql -V
mysql  Ver 14.14 Distrib 5.7.30, for Linux (x86_64) using  EditLine wrapper

//mysql数据库设置远程连接权限
mysql> grant all on *.* to 'root'@192.168.67.131 identified by 'mysql123456';


[root@node01-linux ~]# mysql -uroot -p'mysql123456' -h192.168.67.131
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 15
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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> 


//注意,不推荐直接在命令行里直接用-pPASSWORD的方式登录,而是使用-p选项,然后交互式输入密码
[root@node01-linux ~]# mysql -uroot -p -h192.168.67.131
Enter password: mysql123456
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 16
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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执行sql语句后退出,常用于脚本
[root@node01-linux ~]# mysql -uroot -p -h192.168.67.131 -e 'show databases;'
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

[root@node01-linux ~]# mysql -uroot -p'mysql123456' -h192.168.67.131 -e 'use mysql;show tables'
mysql: [Warning] Using a password on the command line interface can be insecure.
+---------------------------+
| Tables_in_mysql           |
+---------------------------+
| columns_priv              |
| db                        |
| engine_cost               |
| event                     |
| func                      |
| general_log               |
| gtid_executed             |
| help_category             |
| help_keyword              |
| help_relation             |
| help_topic                |
| innodb_index_stats        |
| innodb_table_stats        |
| ndb_binlog_index          |
| plugin                    |
| proc                      |
| procs_priv                |
| proxies_priv              |
| server_cost               |
| servers                   |
| slave_master_info         |
| slave_relay_log_info      |
| slave_worker_info         |
| slow_log                  |
| tables_priv               |
| time_zone                 |
| time_zone_leap_second     |
| time_zone_name            |
| time_zone_transition      |
| time_zone_transition_type |
| user                      |
+---------------------------+
[root@node01-linux ~]# mysql -uroot -p'mysql123456' -h192.168.67.131 -e 'use mysql;show tables'|grep plugin
mysql: [Warning] Using a password on the command line interface can be insecure.
plugin



3.2 服务器监听的两种socket地址

socket类型
ip socket:

  • 默认监听在tcp的3306端口,支持远程通信

unix sock:

  • 监听在sock文件上(/tmp/mysql.sock,/var/lib/mysql/mysql.sock)
  • 仅支持本地通信
  • server地址只能是:localhost,127.0.0.1
[root@node01-linux ~]# mysql -uroot -p'mysql123456' -S /var/lib/mysql/mysql.sock
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 25
Server version: 5.7.30 MySQL Community Server (GPL)

Copyright (c) 2000, 2020, 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> quit
Bye

4. mysql数据库操作

4.1 DDL操作

4.1.1 数据库操作

//创建数据库
//语法:CREATE DATABASE [IF NOT EXISTS] 'DB_NAME';

//创建数据库school
mysql> CREATE DATABASE IF NOT EXISTS school;
Query OK, 1 row affected (0.00 sec)

//查看当前实例有哪些数据库
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| school             |
| sys                |
+--------------------+
5 rows in set (0.01 sec)

//删除数据库
//语法:DROP DATABASE [IF EXISTS] 'DB_NAME';
//删除数据库shcool
mysql> DROP DATABASE IF EXISTS school;
Query OK, 0 rows affected (0.01 sec)

mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)


4.1.2 表操作

//创建表
//语法:CREATE TABLE table_name (col1 datatype 修饰符,col2 datatype 修饰符) ENGINE='存储引擎类型';
//在数据库wangqingge里创建表school

mysql> CREATE DATABASE school;
Query OK, 1 row affected (0.00 sec)

mysql> USE school
Database changed


mysql> CREATE TABLE linux (id int NOT NULL,name VARCHAR(50) NOT NULL,age tinyint NOT NULL);
Query OK, 0 rows affected (0.01 sec)

//查看当前数据库有哪些表
mysql> SHOW TABLES;
+------------------+
| Tables_in_school |
+------------------+
| linux            |
+------------------+
1 row in set (0.00 sec)

//查看具体表的结构
mysql> DESC linux;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   |     | NULL    |       |
| name  | varchar(50) | NO   |     | NULL    |       |
| age   | tinyint(4)  | NO   |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.04 sec)

//查找数据
mysql> SELECT * FROM linux;
Empty set (0.00 sec)

//删除表
mysql> DROP TABLE linux;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW TABLES;
Empty set (0.00 sec)


4.1.3 用户操作

mysql用户帐号由两部分组成,如’USERNAME’@‘HOST’,表示此USERNAME只能从此HOST上远程登录

这里(‘USERNAME’@‘HOST’)的HOST用于限制此用户可通过哪些主机远程连接mysql程序,其值可为:
IP地址,如:172.16.12.129

通配符

  • %:匹配任意长度的任意字符,常用于设置允许从任何主机登录
  • _:匹配任意单个字符
//数据库用户创建
//语法:CREATE USER 'username'@'host' [IDENTIFIED BY 'password'];
//创建数据库用户linux

密码策略问题异常信息:
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

mysql> CREATE USER 'linux'@'127.0.0.1' IDENTIFIED BY 'linux123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

解决办法:

1、查看 mysql 初始的密码策略,
输入语句 “ SHOW VARIABLES LIKE 'validate_password%'; ” 进行查看,

mysql> SHOW VARIABLES LIKE 'validate_password%'; 
+--------------------------------------+--------+
| Variable_name                        | Value  |
+--------------------------------------+--------+
| validate_password_check_user_name    | OFF    |
| validate_password_dictionary_file    |        |
| validate_password_length             | 8      |
| validate_password_mixed_case_count   | 1      |
| validate_password_number_count       | 1      |
| validate_password_policy             | MEDIUM |
| validate_password_special_char_count | 1      |
+--------------------------------------+--------+
7 rows in set (0.01 sec)

2、首先需要设置密码的验证强度等级,设置 validate_password_policy 的全局参数为 LOW 即可,
输入设值语句 “ set global validate_password_policy=LOW; ” 进行设值,

mysql> set global validate_password_policy=LOW;
Query OK, 0 rows affected (0.00 sec)

mysql> SHOW VARIABLES LIKE 'validate_password%';
+--------------------------------------+-------+
| Variable_name                        | Value |
+--------------------------------------+-------+
| validate_password_check_user_name    | OFF   |
| validate_password_dictionary_file    |       |
| validate_password_length             | 8     |
| validate_password_mixed_case_count   | 1     |
| validate_password_number_count       | 1     |
| validate_password_policy             | LOW   |
| validate_password_special_char_count | 1     |
+--------------------------------------+-------+
7 rows in set (0.00 sec)

mysql> CREATE USER 'liunx'@'127.0.0.1' IDENTIFIED BY '12345678';
Query OK, 0 rows affected (0.00 sec)

























發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章