Mysql相關(guān)的命令記錄
記錄一些用到的命令绪抛,同步更新在我的Blog上。
一怕犁、與docker相關(guān)
1边篮、創(chuàng)建一個(gè)mysql容器
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=password -d mysql
2、docker登錄進(jìn)入到容器中
docker exec -it (container_name/id) /bin/bash
二因苹、數(shù)據(jù)庫(kù)整體相關(guān)
1苟耻、查看數(shù)據(jù)庫(kù)的編碼
show variables like 'character%';
2、查看數(shù)據(jù)表的定義
-- 兩者都行
desc (table_name);
describe (table_name);
3扶檐、創(chuàng)建新用戶
首先使用root
賬戶登錄凶杖。
執(zhí)行如下操作:
insert into mysql.user(Host,User,authentication_string,ssl_cipher,x509_issuer,x509_subject) values("localhost","your-name",password("123456"),"","","”);
需要注意的是在mysql5.7之前的版本中authentication_string
字段是對(duì)應(yīng)Password
字段的。
另外Host指定為localhost
款筑,表示只能本地訪問(wèn)智蝠,如果想要遠(yuǎn)程訪問(wèn)需要改為%
。
登錄新用戶前需要重啟一下mysql
的服務(wù)奈梳。否則會(huì)報(bào)出錯(cuò)誤杈湾。錯(cuò)誤代碼如下:ERROR 1045 (28000)
。
4攘须、刪除用戶
使用root
賬戶登錄漆撞。
執(zhí)行如下操作:
use mysql;
Delete from user where User="your-name";
5、將數(shù)據(jù)庫(kù)的權(quán)限授予某個(gè)用戶
使用root
賬戶登錄于宙。
-- 授權(quán)(所有權(quán)限)
grant all privileges on test_g.* to name@localhost identified by '123456';
-- 刷新權(quán)限表
flush privileges;
-- 授予某用戶test數(shù)據(jù)庫(kù)的某些權(quán)限(select, update)
grant select,update on test.* to name@localhost identified by '123456';
-- 授予某用戶所有數(shù)據(jù)庫(kù)的某些權(quán)限(此處被非本地主機(jī))
-- 要對(duì)本地主機(jī)授權(quán)浮驳,使用localhost替換 %
grant select,delete,update,create,drop on *.* to name@"%" identified by "123456";
其命令格式為:
grant 權(quán)限 on 數(shù)據(jù)庫(kù).* to 用戶名@登錄主機(jī) identified by "密碼";
6、展示當(dāng)前運(yùn)行的線程捞魁。
展示出當(dāng)前正在運(yùn)行的所有線程至会,子角色只能看到自己發(fā)起的線程。
show processlist;
7谱俭、修改數(shù)據(jù)庫(kù)的編碼
mysql默認(rèn)的編碼是latin1
奉件。但是我們?cè)谑褂玫臅r(shí)候由于需要一些中文或者其他原因。需要更改數(shù)據(jù)庫(kù)的編碼格式昆著。
創(chuàng)建數(shù)據(jù)庫(kù)的時(shí)候可以指定編碼格式县貌。
create database db_name character set utf8;
a.命令行方式
此種方式不推薦使用,并且只在當(dāng)前窗口有效凑懂。
set character_set_client=utf8;
set character_set_connection=utf8;
...
b.修改配置文件my.cnf
文件窃这,位于/etc/mysql/my.cnf
需要重啟生效
-- 增加或者修改。
[client]
default-character-set=utf8
[mysqlId]
character-set-server=utf8
default-character-set=utf8
8、展示數(shù)據(jù)表的狀態(tài)信息
命令語(yǔ)法:
SHOW TABLE STATUS
[{FROM | IN} db_name]
[LIKE 'pattern' | WHERE expr]
顯示出當(dāng)前數(shù)據(jù)庫(kù)中的數(shù)據(jù)表的各種信息杭攻。
eg:
show table status like "table_name";
持續(xù)更新中祟敛。。兆解。馆铁。