MySQL產品簡介及鏈接操做mysql
MySQLs是一個真正的多線程,多用戶的SQL數據庫服務軟件,憑藉查詢速度快,好性能,高可靠和易於使用等特性,成爲服務領域中最受歡迎的開源關係型數據庫系統。在2008年以前,MySQL醒目由MySQL AB公司進行開發,發佈與支持,SUN在2008年以10億美圓收購了MySQL。4月,Oracle甲骨文宣佈將以74億美圓併購SUN。目前MySQL項目有Oracle公司負責運營和維護。linux
MySQL與MariaDB:
MariaDB數據庫管理系統是MySQL的一個分支,主要由開源社區在維護,採用GPL受權許可。開發這個分支的緣由之一是:甲骨文公司收購了MySQL後,有將MySQL閉源的潛在風險,所以社區採用分支的方式來避開這個風險。MariaDB的目的是徹底兼容MySQL,包括API和命令行,使之能輕鬆成爲MySQL的代替品。redis
MariaDB由MySQL的創始人麥克爾·維德紐斯主導開發,他早前曾以10億美圓的價格,將本身建立的公司MySQL賣給了SUN,此後,隨着SUN被甲骨文收購,MySQL的全部權也落入Oracle的手中。MariaDB名稱來自麥克爾·維德紐斯的女兒瑪麗亞(英語:Maria)的名字。MariaDB直到5.5版本,均依照MySQL的版本。所以,使用MariaDB5.5的人會從MySQL5.5中瞭解到MariaDB的全部功能。從2012年11月12日起發佈的10.0.0版開始,再也不依照MySQL的版號。10.0.x版以5.5版爲基礎,加上移植自MySQL5.6版的功能和自行開發的新功能。CentOS7系列Linux操做系統中採用MariaDB替代了MySQL數據庫產品。sql
關係型數據庫管理系統RDBMS:數據庫
商業:Oracle , Sybase, Infomix, SQL Server, DB2
開源:MySQL PostgreSQL , pgsql,EnterpriseDBtomcat
銀行軟件:
操做系統:AIX(IBM-->Unix小機)RedHat安全
數據庫:Oracle DB2
中間件:Weblogic Websphere Jboss服務器
非關係型數據庫 NoSQL:
MongoDB, Redis, HBase ,Memcached
MySQL版本:
Community Edtion 社區版,免費,由社區人員維護,測試及更新多線程
Enterprise Edtion 企業版,收費,MySQL官方維護團隊人員維護,測試及更新架構
一、安裝MySQL
(1)二進制安裝MySQL
(2)用rpm安裝mariadb步驟:
yum -y install mariadb mariadb-server
systemctl start mariadb
它的端口是3306
dhcp:服務端67,客戶端68
ftp:20,21還有端口範圍
dns:TCP/UDP53,9
http:80
https:43
ssh:22
telnet:23
tomcat:8080,8005,8009
mysql:3306
redis:63,79
mamached:11211
Windows遠程控制:3389
MySQL默認經過TCP:3306端口提供服務,可經過編輯/etc/my.cnf文件中"port=3306"行進行修改。
MySQL 數據庫系統也是典型的C/S(客戶端/服務器)架構的應用,鏈接時須要專用的客戶端工具,Linux下經過mysql命令工具(若是是經過rpm格式安裝軟件須要安裝mysql軟件包)。
鏈接並登陸到MySQL操做環境
mysql
-u 指定用戶名
-p 指定密碼(選項和密碼之間不能有空格)
-h 指定主機
-P 指定端口
-S 指定Socket文件
-e 指定SQL命令語句(非交互模式)
[root@localhost ~]# mysql -u root -p123456;history -c 默認root爲Mysql管理用戶
MySQL語句及客戶端工具:
SQL語句分類:
一、數據定義語言(DDL)
建立、修改或刪除數據庫中各類對象,包括表、視圖、索引等。
命令 :CREATE TABLE , CREATE VIEW, CREATE INDEX、ALTER TABLE ,DROP TABLE , DROP VIEW, DROP INDEX
二、查詢語言(DQL)
按照指定的組合、條件表達式或排序檢索已存在的數據庫中數據,不改變數據庫中數據。
命令:SELECT...FROM...WHERE...
三、數據操縱語言(DML)
對已經存在的數據庫進行元組的插入、刪除、修改等操做
命令:INSERT、UPDATE、 DELETE
四、數據控制語言(DCL)
用來授予或收回訪問數據庫的某種特權、
控制數據操縱事務的發生時間及效果、對數據庫進行監視
命令:GRANT、REVOKE、COMMIT、ROLLBACK
設置數據庫用戶的初始密碼(剛開始沒有密碼,給它設置一個密碼):
[root@localhost ~]# mysqladmin -u root password '123456
後期修改數據庫用戶的密碼:
[root@localhost~]# mysqladmin -u root-p舊的密碼 password'新的密碼'
[root@localhost~]# mysqladmin -u root -p'TvC:Rm1ZlxtG' password '123456'
MySQL是一套關係型數據庫管理系統,在每臺MySQL服務器中,均支持運行多個數據庫(文件系統中的目錄)(分類,按照產品劃分),每一個庫至關於一個容器,其中存放着許多數據表,表中的每一行包含一條具體的數據關係信息,這些信息被稱爲數據記錄。
數據庫的基本概念
數據
描述事物的符號記錄稱爲數據(Data),包括數字,文字、圖形、圖像、聲音、檔案記錄等以「記錄」形式按統一的格式進行存儲。
數據表
將不一樣的記錄組織在一塊兒,就造成了「表」,是用來存儲具體數據的
數據庫
數據庫就是表的集合,是存儲數據表的倉庫,以必定的組織方式存儲的相互有關的數據
查詢當前服務器中有哪些庫:
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| auth |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)
產看數據庫中的數據表信息:
MariaDB [(none)]> use mysql
Database changed
查看有哪些表:
MariaDB [mysql]> show tables;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
24 rows in set (0.00 sec)
MySQL數據庫的數據文件存放在/usr/local/mysql/data中,每一個子目錄對應一個數據庫,在MyISAM存儲引擎時每一個表對應三個文件:
[root@localhost~]# Is /usr/local/mysql/data/mysql |grep user
user.frm 表的結構定義
user.MYD 表的數據
user.MYI 表的索引
顯示數據表的結構(字段(列)):
MariaDB [mysql]> desc user;
DESCRIBE [數據庫名.]表名 = desc
建立新的數據庫:
MariaDB [(none)]> create database crushlinux;
建立新的數據表:
MariaDB [crushlinux]> create table users(user_name char(18) not null,user_passwd char(48) default '',primary key(user_name));
Query OK, 0 rows affected (0.02 sec)
MariaDB [crushlinux]> show tables;
+----------------------+
| Tables_in_crushlinux |
+----------------------+
| users |
+----------------------+
1 row in set (0.00 sec)
MariaDB [crushlinux]> desc users;
+-------------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+----------+------+-----+---------+-------+
| user_name | char(18) | NO | PRI | NULL | |
| user_passwd | char(48) | YES | | | |
+-------------+----------+------+-----+---------+-------+
2 rows in set (0.01 sec)
刪表:
MariaDB [crushlinux]> drop table users;
Query OK, 0 rows affected (0.01 sec)
刪庫:
MariaDB [crushlinux]> drop database crushlinux;
Query OK, 0 rows affected (0.01 sec)
向數據表中插入新的數據記錄:
(1)MariaDB [crushlinux]> insert into users(user_name,user_passwd) values('zhangsan','123');
Query OK, 1 row affected (0.01 sec)
(2)MariaDB [crushlinux]> insert into users values('zhaosi','123');
Query OK, 1 row affected (0.01 sec)
(3)MariaDB [crushlinux]> insert into users values('lisi',password('123')),('liuneng',password('123'));
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
查看錶中的數據:
(1)MariaDB [crushlinux]> select * from users;
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| liuneng | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| zhangsan | 123 |
| zhaosi | 123 |
+-----------+-------------------------------------------+
4 rows in set (0.00 sec)
(2)MariaDB [crushlinux]> select user_name from users;
+-----------+
| user_name |
+-----------+
| lisi |
| liuneng |
| zhangsan |
| zhaosi |
+-----------+
4 rows in set (0.00 sec)
(3)MariaDB [crushlinux]> select * from users where user_name='zhangsan';
+-----------+-------------+
| user_name | user_passwd |
+-----------+-------------+
| zhangsan | 123 |
+-----------+-------------+
1 row in set (0.01 sec)
修改、更新數據表中的數據信息:
MariaDB [crushlinux]> update users set user_passwd=password('321') where user_name='zhangsan';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [crushlinux]> select * from users;
+-----------+-------------------------------------------+
| user_name | user_passwd |
+-----------+-------------------------------------------+
| lisi | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| liuneng | *23AE809DDACAF96AF0FD78ED04B6A265E05AA257 |
| zhangsan | *7297C3E22DEB91303FC493303A8158AD4231F486 |
| zhaosi | 123 |
+-----------+-------------------------------------------+
4 rows in set (0.00 sec)
在數據庫中修改root用戶的密碼:
MariaDB [mysql]> update user set password=password('123123') where user='root';
Query OK, 3 rows affected (0.01 sec)
Rows matched: 3 Changed: 3 Warnings: 0
刷新受權表並Crul+d退出:
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
[root@localhost ~]# mysql -uroot -p123123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.41-MariaDB MariaDB Server
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
在數據表中刪除指定的數據記錄:
MariaDB [crushlinux]> delete from users where user_name='zhaosi';
Query OK, 1 row affected (0.00 sec)
設置用戶權限(用戶不存在是,則新建用戶)
MySQL數據庫的root用戶帳戶擁有對全部庫,表的所有權限,頻繁使用root帳號會給數據庫服務器帶來 定的安全風險,實際工做中,一般會創建一些低權限的用戶,只負責一部分庫,表的管理和維護操做,甚至能夠對查詢,修改,刪除記錄等各類操做作進一步的細化限制,從而下降數據庫的風險。
格式:grant 權限列表 on 數據庫名.表名 to 用戶名@來源地址[identified by‘密碼’];
權限列表:用於列出受權的各類數據庫操做,經過逗號進行分割,如:select,insertupdate等,all表示全部權限,能夠執行任意操做。
庫名.表名:用於指定受權操做的數據庫和表的名稱,可使用通配符(*)表示全部用戶名@來源地址:用於指定用戶和容許訪問的客戶機地址;來源地址能夠是IP地址,域名,%通配符表示全部(但不能表示localhost)
MySQL通配符:
_:任意單個字符 192.168.1_
%:任意長度的任意字符 192.168.1.%
案例:(只授予了查看權限)
MariaDB [crushlinux]> grant select on crushlinux.* to 'teacher'@'localhost' identified by '123';
Query OK, 0 rows affected (0.01 sec)
[root@localhost ~]# mysql -uteacher -p123
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 5.5.41-MariaDB MariaDB Server
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| crushlinux |
| test |
+--------------------+
3 rows in set (0.00 sec)
查看用戶的權限:
MariaDB [(none)]> show grants for 'teacher'@'localhost';
+----------------------------------------------------------------------------------------------------------------+
| Grants for teacher@localhost |
+----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'teacher'@'localhost' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
| GRANT SELECT ON `crushlinux`.* TO 'teacher'@'localhost' |
+----------------------------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)
撤銷用戶的權限:
MariaDB [crushlinux]> revoke select on crushlinux.* from 'teacher'@'localhost';
Query OK, 0 rows affected (0.01 sec)
MariaDB [crushlinux]> show grants for 'teacher'@'localhost';
+----------------------------------------------------------------------------------------------------------------+
| Grants for teacher@localhost |
+----------------------------------------------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'teacher'@'localhost' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |
+----------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
用於顯示普遍的服務器狀態信息:
MariaDB [(none)]> show status;
顯示建立特定數據庫或表:
help create databases;
help create tables;
顯示受權用戶的安全權限:
MariaDB [(none)]> show grants;
顯示服務器錯誤或警告信息:
show erroes;
show warnings;
顯示當前的時間:
MariaDB [(none)]> select now();
+---------------------+
| now() |
+---------------------+
| 2019-10-13 18:13:47 |
+---------------------+
1 row in set (0.01 sec)
顯示當前用戶及時間:
(1)MariaDB [(none)]> select current_user(),current_timestamp;
+----------------+---------------------+
| current_user() | current_timestamp |
+----------------+---------------------+
| root@localhost | 2019-10-13 18:14:52 |
+----------------+---------------------+
1 row in set (0.01 sec)
(2)MariaDB [(none)]> select user(),now();
+----------------+---------------------+
| user() | now() |
+----------------+---------------------+
| root@localhost | 2019-10-13 18:15:32 |
+----------------+---------------------+
1 row in set (0.00 sec)
受權Windows客戶機地址擁有訪問權限:
MariaDB [(none)]> grant all on *.* to 'root'@'192.168.200.110' identified by '123';
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
能夠在這裏打命令。
MariaDB [crushlinux]> show grants for 'teacher'@'localhost';+----------------------------------------------------------------------------------------------------------------+| Grants for teacher@localhost |+----------------------------------------------------------------------------------------------------------------+| GRANT USAGE ON *.* TO 'teacher'@'localhost' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257' |+----------------------------------------------------------------------------------------------------------------+1 row in set (0.00 sec)