一、數據庫操作
1.連接數據庫
#本地連接
mysql -u 用戶名 -p
#錯誤:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysqld.sock' (2)
#需要指定mysql.sock
mysql -u root -S mysql.sock
#遠程連接
mysql -u 用戶名 -P 端口 -h 遠程IP地址 -p
2.顯示數據庫
SHOW DATABASES;
#默認數據庫
#mysql - 用戶權限相關數據
#test - 用於用戶測試數據
#information_schema - MySQL本身架構相關數
3.創建數據庫
#utf8
CREATE DATABASE 數據庫名稱 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
#gbk
CREATE DATABASE 數據庫名稱 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci
4.刪除數據庫
drop database 數據庫名;
5.使用數據庫
USE 數據庫名;
6.顯示當前使用的數據庫中所有表
SHOW TABLES;
7.用戶管理
#查看用戶
select user,host,password from mysql.user;
#創建用戶
create user '用戶名'@'IP地址' identified by '密碼';
#刪除用戶
drop user '用戶名'@'IP地址';
#修改用戶
rename user '用戶名'@'IP地址' to '新用戶名'@'IP地址';;
#修改密碼
set password for '用戶名'@'IP地址' = Password('新密碼')
8.授權管理
show grants for '用戶'@'IP地址' -- 查看權限
grant 權限 on 數據庫.表 to '用戶'@'IP地址' -- 授權
revoke 權限 on 數據庫.表 from '用戶'@'IP地址' -- 取消權限
對於權限
all privileges 除grant外的所有權限
select 僅查權限
select,insert 查和插入權限
...
usage 無訪問權限
alter 使用alter table
alter routine 使用alter procedure和drop procedure
create 使用create table
create routine 使用create procedure
create temporary tables 使用create temporary tables
create user 使用create user、drop user、rename user和revoke all privileges
create view 使用create view
delete 使用delete
drop 使用drop table
execute 使用call和存儲過程
file 使用select into outfile 和 load data infile
grant option 使用grant 和 revoke
index 使用index
insert 使用insert
lock tables 使用lock table
process 使用show full processlist
select 使用select
show databases 使用show databases
show view 使用show view
update 使用update
reload 使用flush
shutdown 使用mysqladmin shutdown(關閉MySQL)
super 使用change master、kill、logs、purge、master和set global。還允許mysqladmin調試登陸
replication client 服務器位置的訪問
replication slave 由複製從屬使
用對於目標數據庫以及內部其他
數據庫名.* 數據庫中的所有
數據庫名.表 指定數據庫中的某張表
數據庫名.存儲過程 指定數據庫中的存儲過程
*.* 所有數據庫
對於用戶和IP
用戶名@IP地址 用戶只能在改IP下才能訪問
用戶名@192.168.1.% 用戶只能在改IP段下才能訪問(通配符%表示任意)
用戶名@% 用戶可以再任意IP下訪問(默認IP地址爲%)
9.其他操作
flush privileges 將數據讀取到內存中,從而立即生效。
select version(); 查看mysql版本
show variables like 'lower%'; 查看大小寫,0不需分大小寫,1區分大小寫