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源
[[email protected] ~]# yum -y install wget
···

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

[[email protected] ~]# 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
[[email protected] ~]# 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並設置開機自動啓動
[[email protected] ~]# systemctl enable --now mysqld
[[email protected] ~]# systemctl status mysqld

#確保3306端口已經監聽起來
[[email protected] ~]# ss -antl

#在日誌文件中找出臨時密碼
[[email protected] ~]# grep "password" /var/log/mysqld.log

#使用獲取到的臨時密碼登錄mysql
[[email protected] ~]# mysql -uroot -p'ykp<ibcSF5ra'
或者
[[email protected] ~]# 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源
[[email protected] ~]# rpm -e mysql57-community-release

2.3 mariadb安裝與配置

安裝mariadb相關包


[[email protected] ~]# yum -y install mariadb*

2.4 mariadb安裝與配置

#啓動mariadb並設置開機自動啓動
[[email protected] ~]# systemctl enable --now mariadb
[[email protected] ~]# systemctl status mariadb

#確保3306端口已經監聽起來

#默認root賬戶登陸,不需要密碼
[[email protected] ~]# 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');

//登陸
[[email protected] ~]# 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語句後退出,常用於腳本


[[email protected]-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';


[[email protected]-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選項,然後交互式輸入密碼
[[email protected]-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語句後退出,常用於腳本
[[email protected]-linux ~]# mysql -uroot -p -h192.168.67.131 -e 'show databases;'
Enter password: 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+

[[email protected]-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                      |
+---------------------------+
[[email protected]-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
[[email protected] ~]# 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)

























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