1. 程式人生 > >python中mysql常用用法

python中mysql常用用法

python-mysql

查看

查看變量 show variables like ‘auto%‘;
信息輸出: echo "select user,host,password from mysql.user" |mysql -uroot -plingxiangxiang
查看庫 show databases;
查看都有哪些庫 show databases;
查看某個庫的表 use db; show tables \G;
查看表的字段 desc tb;
查看建表語句 show create table tb;
當前是哪個用戶 select user();

當前庫 select database();
查看數據庫版本 select version();
查看mysql狀態 show status;
查看mysql隊列 show processlist;
查詢 select count() from mysql.user; select from mysql.db; select * from mysql.db where host like ‘10.0.%‘;
查看權限 show grants for root@‘localhost‘;

select from sutdent where group by stdname a, c where a.id = c.組id group by 字段:字段進行分組

select from information_schema.processlist where info is not null;
查看表索引 show index from 表名

增加

創建庫 create database db1;
創建表 create table t1 (id int, name char(40) adress varchar(30));
創建普通用戶並授權 grant all on . to databases1.user1 identified by ‘123456‘;
grant all on db1. to ‘user2‘@‘10.0.2.100‘ identified by ‘111222‘;

grant all on db1. to ‘user3‘@‘%‘ identified by ‘231222‘;insert into tb1 (id,name) values(1,‘aming‘);
插入 update db1.t1 set name=‘aaa‘ where id=1;
創建索引 create index 庫名_表名_列名1_列名2 (列名1, 列名2)

刪除

刪除表 drop table db1.t1; 刪除表數據,表結構
刪除數據庫 drop database db1;
delete from table where 條件判斷 刪除行數據
char(10) ‘aaa ‘定長,給10個字符,只寫了3個,其他7個用空格占用
varchar(10) ‘aaa‘ ‘變長‘

修改

修改mysql參數 show variables like ‘max_connect%‘; set global max_connect_errors = 1000;
更改密碼 UPDATE mysql.user SET password=PASSWORD("newpwd") WHERE user=‘username‘ ;
清空表 truncate table db1.t1; 只刪除表數據,不刪除表結構
修復表 repair table tb1 [use frm];

授權超級用戶(對所有庫所有表有權限,還可以給其他用戶設置權限):
grant all privileges on . to ‘tangnanbing‘@‘%‘ identified by ‘1qaz@WSX‘ with grant option;

python中mysql常用用法