一、登錄數(shù)據(jù)庫
- 命令行登錄使用的主要參數(shù):
mysql -h{host ip} -u{username} -p{password} -P{port} -S{sock}
例:
mysql -uroot -p -h localhost -P 3306 -S /var/lib/mysql/mysql.sock
- 不用輸入密碼登錄數(shù)據(jù)庫
?在用戶家目錄新建.my.cnf
文件(修改權(quán)限為600)转绷,文件內(nèi)容如下
[client]
user=root
password=xxxxxxxxx
host=127.0.0.1
default-character-set=utf8mb4
免密碼登錄效果
二瓢颅、用戶和用戶權(quán)限管理
- 修改用戶密碼
?① 在數(shù)據(jù)庫外修改(只有數(shù)據(jù)庫的root用戶能用mysqladmin):
有密碼:
mysqladmin -uroot -p老密碼 password '新密碼'
無密碼:
mysqladmin -uroot password '新密碼'
?② 數(shù)據(jù)庫內(nèi)修改:
修改當(dāng)前登錄用戶密碼:
set password=password('新密碼')
修改其它用戶密碼(root用戶修改mysql.user表中的密碼字段):
MySQL 5.7以前:
update mysql.user set password=password('新密碼') where User='用戶名' and Host='xxx';
MySQL 5.7:
update mysql.user set authentication_string=password(“新密碼”) where User='xxxx' and Host='xxxx';
也可用以下方式
set password for 'root'@'%' = password("123456")
刷新權(quán)限
flush privileges佛寿;
?③ root用戶忘記密碼
1. 修改配置文件/etc/my.cnf
[msyqld]
skip-grant-tables
2. 重啟mysql服務(wù)
3. 用update的方式修改root用戶密碼
- 創(chuàng)建用戶及分配權(quán)限
?① 創(chuàng)建用戶不進(jìn)行授權(quán)操作
create user 用戶名@'主機名(IP)' identified by '密碼';
例:
create user lcs@'localhost' identified by '123456';
?② 創(chuàng)建用戶并進(jìn)行授權(quán)操作
grant 授予的權(quán)限 數(shù)據(jù)庫.表 用戶名@'主機名(IP)' identified by '密碼';
例:
grant all on mydb.* to zhangsan@'172.16.1.1' identified by '123456';
grant授予權(quán)限的種類:
all:設(shè)置除了grant option之外的所有簡單權(quán)限
alter:允許使用alter table
create:允許使用create table
create user:允許使用create user、drop user贬循、rename user、revoke all privilege
create view:允許使用create view
delete:允許使用delete
drop:允許使用drop
execute:允許用戶運行已存儲的子程序
file:允許使用select ... into outfile 和 load data infile
index:允許使用create index 和drop index
insert:允許使用insert
lock tables:允許對您擁有select權(quán)限的表使用lock tables(鎖表)
process:允許使用show full processlist
reload:允許使用flush
replication client:允許用戶詢問從屬服務(wù)器或主服務(wù)器的地址
replication slave:復(fù)制型從服務(wù)器
select:允許使用select
show databases:show databases顯示所有數(shù)據(jù)庫
shutdown:允許使用mysqladmin shutdown
update:允許使用update
usage:無權(quán)限
grant option:允許使用授予權(quán)限(只能授予本用戶擁有的權(quán)限)
??FILE, PROCESS, RELOAD, REPLICATION CLIENT, REPLICATION SLAVE, SHOW DATABASES,SHUTDOWN和SUPER權(quán)限是管理性權(quán)限桃序,只能進(jìn)行全局授權(quán)(使用ON .語法)
?? 對于一個表杖虾,可以指定SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, GRANT OPTION, INDEX和ALTER
?③ 查看和收回賦予用戶的權(quán)限
查看用戶的權(quán)限:
show grants for 用戶名@'主機名(IP)';
查看用戶權(quán)限
收回授權(quán): revoke 權(quán)限1,權(quán)限2…… on 數(shù)據(jù)庫.數(shù)據(jù)表 from 用戶@'IP';
例:
revoke create,drop,delete on *.* from zhangsan@'localhost';
三媒熊、創(chuàng)建和刪除數(shù)據(jù)庫(表)
創(chuàng)建數(shù)據(jù)庫
create database test character set utf8mb4 collate utf8mb4_general_ci;
刪除數(shù)據(jù)庫
drop database test;
創(chuàng)建數(shù)據(jù)表
create table 表名(表結(jié)構(gòu));
刪除數(shù)據(jù)表
drop table 表名;
示例建表語句
create table books(
id int unsigned not null auto_increment primary key comment "自增主鍵",
book_name char(30) not null comment "書名",
author char(20) not null default 'unknow' comment "作者",
price int(4) unsigned not null default '0' comment "價格",
info char(60) null comment "簡介"
)ENGINE=InnoDB DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
查看建表語句
show create table {tablename}
復(fù)制表結(jié)構(gòu)
create table {new_table_name} like {exists_table};
四奇适、修改表屬性
1.修改表名
alter table {table_name} rename {new_table_name};
- 修改列名和列屬性
alter table {table_name} change {field_name} {new_field_name} {attribute};
例:
alter table book change info infomation char(50) default "暫無簡介信息" comment "簡介";
- 添加屬性列
# first 是添加到第一列,after field_name 是添加到某一屬性列后面
alter table {table_name} add {field_name} {attribute} [first | after field_name] ;
例:
alter table book add date char(20) not null default '1970-01-01 00:00:00' first;
- 刪除屬性列
alter talble {table_name} drop {field_name}
- 主鍵和索引相關(guān)的操作
刪除主鍵
alter table {table_name} drop primary key泛释;
添加主鍵
alter table {table_name} add constraint primary key ({field_name})滤愕;
添加索引
alter table {table_name} add index {index_name} (field_name);
添加唯一限制條件索引
alter table {table_name} add unique {unique_name} (field_name);
刪除索引
alter table {table_name} drop index emp_name;
五、MySQL 系統(tǒng)變量
顯示MySQL的系統(tǒng)變量
show variables怜校;(比較多 间影,大約200多個)
auto_increment_increment :表示自增變量的增長步長
auto_increment_offset :表示自增變量的起始增長數(shù)值
set @@auto_increment_increment=10 :設(shè)置自增變量的增長步長為10
set @@auto_increment_offset=5 :設(shè)置自增數(shù)值起始為5
div_precision_increment :設(shè)置進(jìn)行除法操作的小數(shù)點后精確的位數(shù)
顯示MySQL的狀態(tài)變量:
show status;
顯示insert語句執(zhí)行了多少次
show status like 'Com_insert'
顯示MySQL運行狀態(tài)(寫監(jiān)控腳本用)
mysql -e "show global status;"|egrep "Com_insert|Com_update|Com_select|Com_delete"