win10下MySQL5.7基本操作指南
一、基本操作
1、下載安裝
這個地址提供所有的MySQL Community Server版本下載(包括歷史版本)。
2、新增環境配置
桌面【我的電腦】----右鍵【屬性】----高階系統設定----環境變數----系統變數:雙擊Path----新建,將MySQL安裝目錄下bin資料夾路徑複製進去----確定 3次。
3、在MySQL安裝根目錄下新建my.ini檔案,並編輯,內容如下:注意格式為ANSI
,否則會在下一步中報錯
mysqld: [ERROR] Found option without preceding group in config file D:\MySQL\my.ini at line 1! mysqld: [ERROR] Fatal error in defaults handling. Program aborted!
my.ini
檔案內容
[Client]
port=3306
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
port=3306
basedir=D:\MySQL
datadir=D:\MySQL\data
max_connections=200
character-set-server=utf8
default-storage-engine=INNODB
4、管理員方式開啟cmd,切換到MySQL安裝目錄bin下,初始化MySQL:
D:\MySQL\bin>mysqld --initialize --user=mysql --console
2018-11-01T01:29:23.532353Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-11-01T01:29:26.971841Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-11-01T01:29:27.523754Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-11-01T01:29:27.773737Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 92c04cf6-dd75-11e8-8742-44334c06f63c.
2018-11-01T01:29:27.867507Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-11-01T01:29:27.975723Z 1 [Note] A temporary password is generated for [email protected]: iO%F;L>rN7iV
初始化成功後,會自動在MySQL目錄下建立data
目錄,並生成初始密碼。將密碼儲存。
5、安裝MySQL:
D:\MySQL\bin>mysqld --install mysql
如果之前安裝過MySQL,而未清理乾淨,可能出現如下錯誤提示:
D:\MySQL\bin>mysqld --install mysql The service already exists! The current server installed: D:\mysql5718\bin\mysqld MySQL
解決方案:以管理員身份執行cmd
①查詢是否有名為mysql的服務:
C:\WINDOWS\system32>sc query mysql
SERVICE_NAME: mysql
TYPE : 10 WIN32_OWN_PROCESS
STATE : 1 STOPPED
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x7d0
②如有,刪除mysql
C:\WINDOWS\system32>sc delete mysql
[SC] DeleteService 成功
接著就能安裝成功了。
6、啟動MySQL服務:
D:\MySQL\bin>net start mysql
mysql 服務正在啟動 .
mysql 服務已經啟動成功。
關閉/停止:net stop mysql
控制檯:mysqld --console
7、登入MySQL:輸入剛才的初始密碼
D:\MySQL\bin>mysql -u root -p
Enter password: ************
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.18
Copyright (c) 2000, 2017, 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.
也可以一條語句完成,但會暴露密碼,不推薦:
D:\MySQL\bin>mysql -uroot -p(p字母后接著輸入密碼,不用空格)
8、修改密碼:
mysql> set password=password('新密碼');
Query OK, 0 rows affected, 1 warning (0.00 sec)
也可以用這條語句,略顯複雜:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '密碼';
9、cmd中退出mysql:
mysql> quit
Bye
二、MySQL指令碼基本組成
與常規指令碼語言類似,MySQL也具有一套對 字元、單詞、特殊符號的使用規定,MySQL通過執行SQL指令碼來完成對資料庫的操作,該指令碼由一條或多條MySQL語句(SQL語句+擴充套件語句)組成,儲存時指令碼檔案字尾為.sql
。當然,在控制檯下,MySQL客戶端也可對語句進行單句執行,而不用儲存為.sql
檔案。
- 識別符號
識別符號用來命名一些物件, 如資料庫、表、列、變數等, 以便在指令碼中的其他地方引用。MySQL識別符號命名規則稍微有點繁瑣, 這裡我們使用萬能命名規則: 識別符號由字母、數字或下劃線(_)組成, 且第一個字元必須是字母或下劃線。
對於識別符號是否區分大小寫取決於當前的作業系統, Windows下是不敏感的, 但對於大多數 linux\unix 系統來說, 這些識別符號大小寫是敏感的。
- 關鍵字
MySQL的關鍵字眾多, 這裡不一一列出, 在學習中學習。 這些關鍵字有自己特定的含義, 儘量避免作為識別符號。
- 語句
MySQL語句是組成MySQL指令碼的基本單位, 每條語句能完成特定的操作, 它是由 SQL 標準語句 + MySQL 擴充套件語句組成。
- 函式
MySQL函式用來實現資料庫操作的一些高階功能, 這些函式大致分為以下幾類: 字串函式、數學函式、日期時間函式、搜尋函式、加密函式、資訊函式。
三、MySQL中的資料型別
MySQL有三大類資料型別, 分別為數字、日期\時間、字串, 這三大類中又更細緻的劃分了許多子型別:
- 數字型別
整數:tinyint、smallint、mediumint、int、bigint
浮點數:float、double、real、decimal
- 日期和時間:
date、time、datetime、timestamp、year
- 字串型別
字串:char、varchar
文字:tinytext、text、mediumtext、longtext
二進位制(可用來儲存圖片、音樂等):tinyblob、blob、mediumblob、longblob
四、日常使用MySQL資料庫的基本操作
文字均以管理員執行cmd下操作。
1、啟動MySQL服務
C:\WINDOWS\system32>net start mysql
mysql 服務正在啟動 .
mysql 服務已經啟動成功。
【任務管理----服務,可檢視mysql是否啟動。】
關閉:
net stop mysql
2、登入MySQL,進入mysql>
模式:如果連線遠端主機加上-h
指令
mysql -u root [-h IP] -p
或
mysql -u root -p#安全模式,輸入密碼顯示為*號。
或
mysql -uroot -p(p字母接著密碼,不用空格)
退出mysql>
模式:quit
mysql>quit
3、建立資料庫
mysql>create database <資料庫名稱> character set utf8;
會配置MySQL時的(如)D:\MySQL\data資料夾下建立資料庫資料夾。
如需在指定資料夾下建立資料庫,則需要修改.ini
檔案中的datadir
值。
刪除資料庫:
drop database <資料庫名>;
刪除一個已確定存在的資料庫:
mysql>drop database <資料庫名>;
刪除一個不確定存在的資料庫:
mysql>drop database if exists <資料庫名>;
3、選擇/切換要操作的資料庫,兩種方式:
①在登入MySQL時指定,命令:
mysql -D <資料庫名> -u root -p
②登入後使用use
語句指定,命令:
mysql> use <資料庫名>;
4、建立資料庫表,用create table
語句:
create table 表名稱(列宣告);
以建立students
表為例,該表存放 學號id、姓名name、性別sex、年齡age、聯絡電話tel
:
create table students(
id int unsigned not null auto_increment primary key,
name char(8) not null,
sex char(4) not null,
age tinyint unsigned not null,
tel char(13) null default "-"
);
上述MySQL指令碼語句可直接在任何文字編輯器(如txt、SublimeText、NotePad++)下輸入後將其儲存為createtable.sql
檔案。
再通過cmd下的檔案重定向執行該指令碼:
mysql -D <資料庫名> -u root -p < createtable.sql
建立成功的話會在同文件夾下新增students.idb
檔案。createtable.sql.sql
檔案若不在當前工作目錄下,需指定該檔案的完整路徑。
在mysql>
模式下:
檢視已建立了的表名稱
show tables;
檢視已建立表的結構
describe <表名>;
列出所有資料庫
show databases;
5、操作MySQL資料庫
向表中插入資料,語法:
insert [into] 表名 [(列名1,列名2,列名3,...)] values (值1,值2,值3,...);
例:
insert into students values(NULL, "王剛","男", 20,"13812341234");
有時插入部分資料 或不按列的順序插入,可使用如下形式:
insert into students (name, sex, age) values("佳兒","男",21);
查詢表中的資料,語法:
select 列名稱 from 表名稱 [查詢條件];
例:查詢students表 中所有學生的名字和年齡
select name,age from students;
使用萬用字元 *
查詢表中所有的內容,語句:
select * from students;
按特定條件查詢:where
關鍵詞用於指定查詢條件,語法:
select 列名稱 from 表名稱 where 條件;
例:查詢所有性別為男的資訊,語句:
select * from students where sex="男";
where
子句不僅支援“where 列名稱=值
”查詢形式,還支援比較運算子,如=、>、<、>=、<=、!=
,以及擴充套件運算子如 is [not] null、in、like
等,還可使用or
和and
進行組合查詢。
更新表中的資料,語法:
update 表名稱 set 列名稱=新值 where 更新條件;
例 id為4的手機號改為15200901235:
update students set tel="15200901235" where id=4;
例 將所有人年齡減1:
update students set age= age-1;
將手機號為13812341234的姓名改為“李新宇”、年齡為17:
update students set name="李新宇",age=17 where tel="13812341234";
刪除表中的資料,語法:
delete from 表名稱 where 刪除條件;
例 刪除id為2的行:
delete from students where id=2;
刪除表中所有資料:
delete from students;
建立後 表的修改:alter table
語句用於建立後對錶的修改。
新增列
alter table 表名 add 列名 列資料型別 [after 插入位置];
例:在表的最後追加列 address:
alter table students add address char(60);
修改列
alter table 表名 change 列名稱 列新名稱 新資料型別;
例:將students表中列tel改為telphone:
alter table students change tel telphone char(13);
刪除列
alter table 表名 drop 列名稱;
重命名錶
alter table 表名 rename 新表名;
刪除整張表
drop table 表名;
新增、刪除使用者,授權(如果在使用,MySQL8.0加密方式修改了)
新增使用者:
create user "test"@"localhost" identified by "123456";
添加了一個名為 test、密碼為123456的使用者,其中localhost代表只允許本地IP訪問(即只能在本地登入MySQL),將localhost 改為 % 將可在任何一臺電腦上登入,也可指定某個外網IP(將localhost改為某指定IP)可遠端登入。
授權:
grant all privileges on *.* to "test"@"localhost" with grant option;
其中all privileges
代表全部許可權,可改為部分許可權如 select,insert,update
;第一個*
表示允許該使用者訪問所有資料庫檔案,可改成某個資料庫如 testdb。
重新整理許可權:
flush privileges
可退出當前某使用者登入,用剛新增的使用者名稱和密碼登入了,之後就可以在授權範圍內操作資料庫了,如show databases;
。
檢視使用者許可權:
show grants for "root"@"localhost";
修改指定使用者密碼:
alter user "test"@"localhost" identified by "test123";
刪除使用者:
drop user "test"@"localhost";