1、系統管理
(1)連接MySQL
格式: mysql -h 主機地址 -u用戶名 -p用戶密碼
例 1:連接到本機上的 MySQL。
hadoop@ubuntu:~$ mysql -uroot -pmysql;
例 2:連接到遠程主機上的 MYSQL。
hadoop@ubuntu:~$ mysql -h 127.0.0.1 -uroot -pmysql;
(2)修改新密碼
在終端輸入:mysql -u用戶名 -p密碼,回車進入Mysql。
> use mysql;
> update user set password=PASSWORD('新密碼') where user='用戶名';
> flush privileges; #更新權限
> quit; #退出
(3)增加新用戶
格式:grant select on 資料庫.* to 用戶名@登錄主機 identified by '密碼'
舉例:
例 1:增加一個用戶 test1 密碼為 abc,讓他可以在任何主機上登錄,并對所有資料庫有
查詢、插入、修改、洗掉的權限。首先用以 root 用戶連入 MySQL,然后鍵入以下命令:
mysql>grant select,insert,update,delete on *.* to root@localhost identified by 'mysql';
或者
grant all privileges on *.* to root@localhost identified by 'mysql';
然后重繪權限設定。
flush privileges;
例 2:如果你不想 root 有密碼操作資料庫“mydb”里的資料表,可以再打一個命令將密碼消掉。
grant select,insert,update,delete on mydb.* to root@localhost identified by '';
(4)洗掉用戶
hadoop@ubuntu:~$ mysql -u用戶名 -p密碼
mysql>delete from user where user='用戶名' and host='localhost';
mysql>flush privileges;
//洗掉用戶的資料庫
mysql>drop database dbname;
2、資料庫操作
(1)顯示所有的資料庫
mysql> show databases;(注意:最后有個 s)
(2)創建資料庫
mysql> create database test;
(3)連接資料庫
mysql> use test;
(4)查看當前使用的資料庫
mysql> select database();
(5)當前資料庫包含的表資訊
mysql> show tables; (注意:最后有個 s)
(6)洗掉資料庫
mysql> drop database test;
3、表操作
備注:操作之前使用“use <資料庫名>”應連接某個資料庫。
(1)建表
命令:create table <表名> (<欄位名 1> <型別 1> [,..<欄位名 n> <型別 n>]);
例子:
mysql> create table MyClass(
> id int(4) not null primary key auto_increment,
> name char(20) not null,
> sex int(4) not null default '0',
> degree double(16,2));
(2)獲取表結構
命令: desc 表名,或者show columns from 表名
例子:
mysql> describe MyClass
mysql> desc MyClass;
mysql> show columns from MyClass;
(3)洗掉表
命令:drop table <表名>
例如:洗掉表名為 MyClass 的表
mysql> drop table MyClass;
(4)插入資料
命令:insert into <表名> [( <欄位名 1>[,..<欄位名 n > ])] values ( 值 1 )[, ( 值 n )]
例子:
mysql> insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);
(5)查詢表中的資料
查詢所有行
mysql> select * from MyClass;
查詢前幾行資料
例如:查看表 MyClass 中前 2 行資料
mysql> select * from MyClass order by id limit 0,2;
或者
mysql> select * from MyClass limit 0,2;
(6)洗掉表中資料
命令:delete from 表名 where 運算式
例如:洗掉表 MyClass 中編號為 1 的記錄
mysql> delete from MyClass where id=1;
(7)修改表中資料
命令:update 表名 set 欄位=新值,... where 條件
mysql> update MyClass set name='Mary' where id=1;
(8)在表中增加欄位
命令:alter table 表名 add 欄位 型別 其他;
例如:在表 MyClass 中添加了一個欄位 passtest,型別為 int(4),默認值為 0
mysql> alter table MyClass add passtest int(4) default '0'
(9)更改表名
命令:rename table 原表名 to 新表名;
例如:在表 MyClass 名字更改為 YouClass
mysql> rename table MyClass to YouClass;
(10)更新欄位內容
命令:update 表名 set 欄位名 = 新內容
update 表名 set 欄位名 = replace(欄位名, '舊內容', '新內容');
例如:文章前面加入 4 個空格
update article set content=concat(' ', content);
3、資料庫匯入匯出
***從資料庫匯出資料庫檔案***
使用“mysqldump”命令
首先進入 DOS 界面,然后進行下面操作。
1)匯出所有資料庫
格式:mysqldump -u [資料庫用戶名] -p -A>[備份檔案的保存路徑]
2)匯出資料和資料結構
格式:mysqldump -u [資料庫用戶名] -p [要備份的資料庫名稱]>[備份檔案的保存路徑]
舉例:
例 1:將資料庫 mydb 匯出到 e:\MySQL\mydb.sql 檔案中。
打開開始->運行->輸入“cmd”,進入命令列模式。
c:\> mysqldump -h localhost -u root -p mydb >e:\MySQL\mydb.sql
然后輸入密碼,等待一會匯出就成功了,可以到目標檔案中檢查是否成功。
例 2:將資料庫 mydb 中的 mytable 匯出到 e:\MySQL\mytable.sql 檔案中。
c:\> mysqldump -h localhost -u root -p mydb mytable>e:\MySQL\mytable.sql
例 3:將資料庫 mydb 的結構匯出到 e:\MySQL\mydb_stru.sql 檔案中。
c:\> mysqldump -h localhost -u root -p mydb --add-drop-table >e:\MySQL\mydb_stru.sql
備注:-h localhost 可以省略,其一般在虛擬主機上用。
3)只匯出資料不匯出資料結構
格式:
mysqldump -u [資料庫用戶名] -p -t [要備份的資料庫名稱]>[備份檔案的保存路徑]
4)匯出資料庫中的Events
格式:mysqldump -u [資料庫用戶名] -p -E [資料庫用戶名]>[備份檔案的保存路徑]
5)匯出資料庫中的存盤程序和函式
格式:mysqldump -u [資料庫用戶名] -p -R [資料庫用戶名]>[備份檔案的保存路徑]
***從外部檔案匯入資料庫中***
1)使用“source”命令
首先進入“mysql”命令控制臺,然后創建資料庫,然后使用該資料庫。最后執行下面操作。
mysql>source [備份檔案的保存路徑]
2)使用“<”符號
首先進入“mysql”命令控制臺,然后創建資料庫,然后退出 MySQL,進入 DOS 界面。最后執行下面操作。
mysql -u root –p < [備份檔案的保存路徑]
uj5u.com熱心網友回復:
先收藏了,正好在學習轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/186547.html
標籤:MySQL
