1. 程式人生 > >mysql資料庫常用命令總結

mysql資料庫常用命令總結

本文主要記錄一些mysql日常使用的命令,供以後查詢。

1.更改root密碼

mysqladmin -uroot password 'yourpassword'
  • 1

2.遠端登陸mysql伺服器

mysql -uroot -p -h192.168.137.10 -P3306
  • 1

3.查詢資料庫

show databases;
  • 1

4.進入某個資料庫

use databasename;
  • 1

5.列出資料庫中的表

show tables;
  • 1

6.檢視某個表全部欄位

desc slow_log;
show create table slow_log\G; (不僅可以顯示錶資訊,還可以顯示建表語句)
  • 1
  • 2

7.檢視當前使用者

select user();
  • 1

8.檢視當前所在資料庫

select database();
  • 1

9.建立新資料庫(可以指定字符集)

create database db1 charset utf8;
  • 1

10.建立新表

create table t1 (`id` int(4), `name` char(40));
  • 1

11.檢視資料庫版本

select version();
  • 1

12.檢視資料庫狀態

show status;         當前會話狀態

show global status;  全域性資料庫狀態

show slave status\G;   檢視主從資料庫狀態資訊
  • 1
  • 2
  • 3
  • 4
  • 5

13.查詢資料庫引數

show variables;
  • 1

14.修改資料庫引數

show variables like 'max_connect%';

set global max_connect_errors = 1000;(重啟資料庫會失效,要在配置檔案中修改)
  • 1
  • 2
  • 3

15.檢視當前資料庫佇列

show processlist;
  • 1

16.建立普通使用者並授權給某個資料庫

grant all on databasename.* to 'user1'@'localhost' identified by '123456';
  • 1

17.查詢表資料

select * from mysql.db;           //查詢該表中的所有欄位
select count(*) from mysql.user; //count(*)表示表中有多少行 select db,user from mysql.db; //查詢表中的多個欄位 select * from mysql.db where host like '10.0.%';在查詢語句中可以使用萬能匹配 “%”
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

18.插入一行資料

insert into db1.t1 values (1, 'abc');
  • 1

19.更改表的某一行資料

update db1.t1 set name='aaa' where id=1;
  • 1

20.清空表資料

truncate table db1.t1;
  • 1

21.刪除表

drop table db1.t1;
  • 1

22.清空資料庫中的所有表(資料庫名是eab12)

mysql -N -s information_schema -e "SELECT CONCAT('TRUNCATE TABLE ',TABLE_NAME,';') FROM TABLES WHERE TABLE_SCHEMA='eab12'" | mysql -f eab12
  • 1

23.刪除資料庫

drop database db1;
  • 1

24.資料庫備份

mysqldump  -uroot -p'yourpassword' mysql >/tmp/mysql.sql
  • 1

25.資料庫恢復

mysql -uroot -p'yourpassword' mysql </tmp/mysql.sql
  • 1

26.新建普通使用者

CREATE USER name IDENTIFIED BY 'ssapdrow';
  • 1

27.更改普通使用者密碼

SET PASSWORD FOR name=PASSWORD('fdddfd');
  • 1

28.檢視name使用者許可權

SHOW GRANTS FOR name;
  • 1

29.指令碼中執行mysql命令

mysql -uuser -ppasswd -e"show databases"

echo "show databases"|mysql -uuser -ppassword
  • 1
  • 2
  • 3

以下是執行大量mysql語句採用的方式

mysql -uuser -hhostname -ppasswd <<EOF

mysql語句

EOF