MySQL使用者管理及SQL入門

來源:互聯網
上載者:User

標籤:mysql

第1章 Mysql使用者管理:1.1 使用者的定義:  使用者名稱+主機域  mysql> select user,host,password from mysql.user;+------+-----------+----------+| user | host      | password |+------+-----------+----------+| root | localhost |          || root | db01      |          || root | 127.0.0.1 |          |1.2 使用者的作用:1.      用來登入資料庫2.      管理資料的對象(表和庫)說明:類比:Linux中建立使用者,更改某個目錄或者檔案的許可權來對資料進行管理1.2.1 建立一個使用者權限設定: grant 許可權 on 許可權範圍 to 使用者  identified by ‘密碼’;1.2.2 許可權管理:對資料庫的讀寫操作等,許可權可用來管理某個使用者可以對資料庫做什麼(insert update、select、delete、drop、create等)1.2.3 角色:對資料庫讀,寫等操作(insert;update;select)1.2.4 許可權範圍:1.      全庫層級: *.*2.      單庫層級: test.*3.      單表層級: test.table_name1.2.5 使用者:    'clsn'@'localhost'  本地    'clsn'@'192.168.66.149'    'clsn'@'192.168.66.%''clsn'@'192.168.66.14%'1.3 練習:按照要求建立一個使用者使用者只能通過10.0.0.0/24網段訪問,使用者名稱為jiang,密碼為123jiang使用者只能對jiang資料庫下的對象進行增insert;create;改update;查select1.3.1 建立使用者並授權grant insert,select,create,update on jiang.* to ‘[email protected]%’ identified by ‘123’;1.3.2 查看使用者的許可權:mysql> show grants for [email protected]'10.0.0.%'\G*************************** 1. row ***************************Grants for [email protected]%: GRANT ALL PRIVILEGES ON *.* TO 'root'@'10.0.0.%' IDENTIFIED BY PASSWORD '*23AE809DDACAF96AF0FD78ED04B6A265E05AA257'1 row in set (0.01 sec)1.3.3 標準建立使用者的方法:create user 'web'@'172.16.1.%' identified by 'web123';   這樣建立的使用者只有串連的許可權1.3.4 刪除使用者:mysql> drop user [email protected]'10.0.0.%';1.3.5 回收許可權revoke insert on *.* from [email protected]’localhos’;1.4 Mysql忘記密碼的修改辦法:1.      停掉資料庫/etc/init.d/mysqld stop2.      停掉串連層的授權功能和遠程登入動能,並啟動cd /application/mysql/bin/mysqld_safe --skip-grant-table --user=mysql --skip-networking &說明:這種啟動模式下,無密碼登入,網路使用者無法登入,只能本地登入,和授權有關的命令都無法執行3.      直接修改密碼mysql> update mysql.user set password=password('123') where user='root' and host='localhost';mysql> flush privileges;MySQL5.7版本修改密碼的修改欄位為: authentication_string4.      退出,重啟服務,正常restart就可以,我這裡失敗了,所以先停止在啟動/etc/init.d/mysqld stop/etc/init.d/mysqld start5.      登入資料庫進行驗證mysql -uroot -p123第2章 Mysql用戶端工具及SQL入門2.1 MySQL用戶端命令有哪些?1.      mysql     ---用來串連資料庫---將使用者的SQL語句發送到服務端2.      mysqladmin---命令列管理工具3.      mysqldump---備份資料庫和表的內容2.2 mysql命令功能詳解:1.      使用者串連資料庫2.      用於管理資料庫2.2.1 mysql命令介面內建功能:命令    命令說明    \h 或 help 或 ?     擷取協助    \G    格式化輸出(行轉列)    \T 或 tee    記錄動作記錄  tee /tmp/mysql.log    \c 或 CTRL+c    退出mysql    \s 或 status    查看資料庫狀態資訊    \. 或 source    mysql> source   /tmp/world.sql    \!    使用shell中的命令 mysql> \!   cat /etc/redhat-releaseCentOS   release 6.9 (Final)     \u 或use       use  worldshow   databases  看當前所有資料庫的名字show   tables   查看當前use到的資料庫所有的表show  tables   from world   查看目標資料庫下的表    快速鍵    上下翻頁、TAB鍵、ctrl   +C 、ctrl +L    2.2.2 mysql命令中help協助說明:contents查看完整的sql類別列表協助mysql> help contents查看特定sql類別或語句的協助mysql> help Account Management;查看grant協助mysql> help grant與狀態相關的sql語句協助mysql> help statusmysqladmin命令說明:功能選項    說明    mysqladmin -u使用者 -p密碼 ping    “強制回應 (Ping)”伺服器。    mysqladmin -u使用者 -p密碼 shutdown    關閉伺服器。    mysqladmin -u使用者 -p密碼 create   databasename    建立資料庫。    mysqladmin -u使用者 -p密碼drop   databasename    刪除資料庫    mysqladmin -u使用者 -p密碼 version    顯示伺服器和版本資訊    mysqladmin -u使用者 -p密碼 status    顯示或重設伺服器狀態變數    mysqladmin -u使用者 -p密碼 password    設定口令    mysqladmin -u使用者 -p密碼 flush-privileges    重新重新整理授權表。    mysqladmin -u使用者 -p密碼 flush-logs    重新整理記錄檔和快取。     2.3 SQL語句入門:2.3.1 DDL:   資料定義語言 (Data Definition Language)定義範圍:對庫名和庫的特性      對錶名和表中的列查看資料庫查看所有資料庫mysql> show databases;+--------------------+| Database           |+--------------------+| information_schema || mysql              || performance_schema || test               || world              || zabbix             |+--------------------+查看當前所在資料庫mysql> select database();+------------+| database() |+------------+| zabbix     |+------------+1 row in set (0.00 sec)對資料庫的操作建立一個庫mysql> create database zabbix character set utf8;Query OK, 1 row affected (0.01 sec)查看庫中的表mysql> show tables;+------------------+| Tables_in_zabbix |+------------------+| stu              |+------------------+1 row in set (0.00 sec)查看庫的建立語句mysql> show create database zabbix;  +----------+-----------------------------------------------------------------+| Database | Create Database                                                 |+----------+-----------------------------------------------------------------+| zabbix   | CREATE DATABASE `zabbix` /*!40100 DEFAULT CHARACTER SET utf8 */ |+----------+-----------------------------------------------------------------+1 row in set (0.00 sec)修改庫的屬性:只能修改字元集和校對規則mysql> alter database zabbix charset utf8mb4;刪除一個庫:mysql> drop database zabbix;切庫mysql> use zabbix;對錶的操作建立一個表mysql> create table stu (id int,name varchar(20),age int ,gender int);查看錶的建立語句mysql> show create table stu;在表最後一列添加:mysql> alter table stu add addr varchar(20);在表的頭部添加:mysql> alter table stu add stu_id int first;在某一列後面添加:mysql> alter table stu add qq int after name;在age後添加tel_num,在最後一行添加emailmysql> alter table stu add tel_num int after age,add email varchar(20);刪除某一列:mysql> alter table stu drop email;修改列名字:mysql> alter table stu change qq QQ int;修改列的資料類型:mysql> alter table stu modify gender varchar(20);建立一個表結構相同的空表mysql> create table stu_0 like stu;mysql> show tables;+------------------+| Tables_in_zabbix |+------------------+| stu              || stu_0            |+------------------+2 rows in set (0.00 sec)建立一個表結構相同的備份表mysql> create table stu_0 as select * from stu;2.3.2 DCL      資料庫控制語言使用者授權GRANT ALL ON *.* TO 'jiang'@'localhost';SHOW GRANTS FOR 'jiang'@'localhost'\G建立使用者的同時進行授權grant insert,select,create,update on jiang.* to [email protected]'10.0.0.%' identified by '123';回收許可權REVOKE INSERT ON *.* FROM [email protected];2.3.3 DML      資料行操作語言(增刪改)insert語句指定列插入mysql> insert into stu (stu_id,QQ) values(1,777);所有列插入mysql> insert into stu values(2,777,'j',56,'hao',2,'r','q',1);insert複製表結構及內容---表已經存在的情況下,在可以複製mysql> create table stu_1 like stu;mysql> insert into stu_1 select * from stu;修改和刪除操作刪除資料:mysql> delete from stu where stu_id=1        邏輯刪除mysql> truncate table stu_3;                 物理刪除修改資料,要加上where條件mysql> update stu_3 set QQ=5656 where age='hao';delete    刪除之後可以用二進位日誌反解insert命令把資料找回來truncate   物理刪除,資料找不回來一般刪除大表的時候,先truncate然後在drop整個表,效率會比較高使用update替代delete,偽刪除mysql> alter table stu add state int default 1;Query OK, 0 rows affected (0.54 sec)Records: 0  Duplicates: 0  Warnings: 0 mysql> select * from stu;+--------+------+-------+-----------+------+---------+--------+------+-------+| stu_id | id   | name  | QQ        | age  | tel_num | gender | addr | state |+--------+------+-------+-----------+------+---------+--------+------+-------+|      1 |  123 | jiang |       222 | ni   |   15555 | boy    | hao  |     1 ||      1 |  123 | jiang |       222 | ni   |   15555 | boy    | hao  |     1 ||      1 |  123 | jiang |       222 | ni   |   15555 | boy    | hao  |     1 ||      7 | NULL | da ya | 850144102 | NULL |    NULL | NULL   | NULL |     1 |+--------+------+-------+-----------+------+---------+--------+------+-------+4 rows in set (0.00 sec)修改state值,等於0即為刪除,mysql> update stu set state=0 where name='jiang';mysql> select * from stu where state=1;+--------+------+-------+-----------+------+---------+--------+------+-------+| stu_id | id   | name  | QQ        | age  | tel_num | gender | addr | state |+--------+------+-------+-----------+------+---------+--------+------+-------+|      7 | NULL | da ya | 850144102 | NULL |    NULL | NULL   | NULL |     1 |+--------+------+-------+-----------+------+---------+--------+------+-------+1 row in set (0.01 sec)防止誤刪除:  -U, --safe-updates  Only allow UPDATE and DELETE that uses keys.mysql命令加上-U後   在執行update和delete語句時,不加where條件不會執行2.3.4 DQL:資料行查詢語言select查詢語句查看stu表中所有資訊mysql> select * from stu;    查看某一列的資訊mysql> select name from stu;     多個列用逗號分隔查看id號碼為777的qq號碼mysql> select QQ from stu where id=777;where子句使用: 將資料按照指定條件,進行過濾處理等值過濾:mysql> select stu_id from stu where QQ=777;比較過濾:mysql> select stu_id from stu where QQ<777;SQL過濾練習:排序:按照人口從少到多的順序排列顯示:SELECT NAME,Population FROM cityWHERE CountryCode='chn' ORDER BY Population;按照從多到少進行排序顯示:SELECT NAME,Population FROM cityWHERE CountryCode='chn' ORDER BY Population DESC;顯示人口排名最多的前10名:SELECT NAME,Population FROM cityWHERE CountryCode='chn'ORDER BY Population DESC LIMIT 10;顯示人口排名 第50行到第60行SELECT NAME,Population FROM cityWHERE CountryCode='chn'ORDER BY Population DESC LIMIT 50,10;顯示世界上人口小於100的城市:SELECT NAME,Population FROM city WHERE Population<1000;顯示PCN是那個國家:SELECT NAME FROM city WHERE country='PCN';顯示世界上人口小於100的城市所在國家的國家名字SELECT country.name FROM country,cityWHERE city.Population<100 AND country.Code=city.countrycode;以上命令可以簡寫成:SELECT  co.name FROM city AS  ci ,country AS coWHEREci.population<100AND co.code=ci.CountryCode;


MySQL使用者管理及SQL入門

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.