mysql

1.1 登陸數據庫。

1

mysql -uroot -pTMD123 -S /data/3306/mysql.sock

 

1.2 查看數據庫版本及當前登錄用戶是什麼。

1

2

select version();

select user();

 

1.3 創建GBK字符集的數據庫TMD,並查看已建庫的完整語句。

1

2

create database TMD character set gbk collate gbk_chinese_ci;

show create database TMD\G

 

1.4 創建用戶TMD,使之可以管理數據庫TMD。

1

grant all on TMD.* to 'TMD'@'localhost' identified by 'TMD123';

 

1.5 查看創建的用戶TMD擁有哪些權限。

1

show grants for TMD@localhost\G

 

1.6 查看當前數據庫裏有哪些用戶。

1

select user,host from mysql.user;

 

1.7 創建管理員帳戶admin

1

grant all on *.* to 'admin'@'localhost' identified by 'admin123' with grant option;

 

1.8 進入TMD數據庫

1

use TMD;

 

1.9 創建test表:innodb引擎,字符集爲GBK,字段爲id int(4)和namevarchar(16),查看建表結構及SQL語句。

1

2

3

4

5

6

7

create table test (

id int(4),

name varchar(16)

)ENGINE=innodb DEFAULT CHARSET=gbk;

 

desc test; #<==等價於命令:show columns from test;

show create table test\G

 

1.10 插入一條數據 1,TMD

1

insert into test values('1','TMD');

 

1.11 批量插入數據 2,殺人回憶,3,etiantian。要求中文不能亂碼。

1

insert into test values('2','殺人回憶'),('3','etiantian');

 

1.12 查詢插入的所有記錄,查詢名字爲TMD的記錄。查詢id大於1的記錄。

1

2

select * from test where name='TMD';

select * from test where id>1;

 

1.13 把數據id等於1的名字TMD更改爲god。

1

update test set name='god' where id=1;

 

1.14 在字段name前插入age字段,類型tinyint(2)。

alter table test add nametinyint(2) after id;

 

1.15 備份TMD庫及mysql庫。

mysqldump -uroot -pTMD123 -S/data/3306/mysql.sock --events -B TMD mysql >/opt/bak_$(date +%F).sql

egrep -v"#|^$|--|\/" /opt/bak_2017-06-06-13時36分37秒.sql

 

1.16 刪除表中的所有數據,並查看。

1

2

truncate table test; #<==物理刪除,一次性清空,不可以rollback

delete from test; #<==邏輯刪除,一行一行的刪,比較慢,可以rollback

 

1.17 刪除表test和TMD數據庫並查看

1

2

drop table test;

drop database TMD;

 

1.18 Linux命令行恢復以上刪除的數據。

1

mysql -uroot -pTMD123 -S /data/3306/mysql.sock </opt/bak_2017-06-07-22時13分20秒.sql

 

1.19 把GBK字符集修改爲UTF8(可選,注意,此題有陷阱)。

1.先導出表中數據

1

2

mysqldump -uroot -pTMD123 -S /data/3306/mysql.sock -B TMD >/opt/test.sql

egrep -v "^$|--|\/" /opt/test.sql #<==可以看到這一行:ENGINE=MyISAM DEFAULT CHARSET=gbk;

 

2.修改字符集

1

2

sed -i 's#CHARSET=gbk#CHARSET=utf8#g' /opt/test.sql

egrep -v "^$|--|\/" /opt/test.sql #<==驗證:) ENGINE=MyISAM DEFAULT CHARSET=utf8;

 

3.恢復數據

1

2

在sql文件中添加一條 set names utf8;並恢復

mysql -uroot -pTMD123 -S /data/3306/mysql.sock  TMD </opt/test.sql

 

 

1.20 MySQL密碼丟了,如何找回實戰?


[root@db01 ~]# netstat -tunlp|grep 3306 #<==先查看服務是否正常

tcp        0      0 0.0.0.0:3306            0.0.0.0:*          LISTEN      62358/mysqld

[root@db01 ~]# kill 62358 #<==kill掉進程的pid

[root@db01 ~]# netstat -tunlp|grep 3306 #<==mysql進程已關閉

[root@db01 ~]# mysqld_safe --help #<==利用mysqld_safe命令指定配置文件,跳過授權表來破密碼

[root@db01 ~]# mysqld_safe --defaults-file=/data/3306/my.cnf --skip-grant-tables 2>&1 >/dev/null &

[root@db01 ~]# mysql -S /data/3306/mysql.sock #<==無密碼登錄進多實例3306

mysql> select user,host,password from mysql.user where user='root' and host='localhost';

+------+-----------+-------------------------------------------+

| user | host      | password                                  |

+------+-----------+-------------------------------------------+

| root | localhost | *FE28814B4A8B3309DAC6ED7D3237ADED6DA1E515 |

+------+-----------+-------------------------------------------+

1 row in set (0.00 sec)#<==先看下mysql庫,user表裏的字段內容

mysql> update mysql.user set password=PASSWORD("TMD123") where user='root' and host='localhost'; #<==利用update命令來更新root@localhost用戶的密碼

mysql> flush privileges; #<==記得刷新授權表,否則不會立馬生效的

Query OK, 0 rows affected (0.00 sec)

[root@db01 ~]# sed -i 's#mysql_pwd="TMD456"#mysql_pwd="TMD123"#g' /data/3306/mysql

[root@db01 ~]# grep mysql_pwd= /data/3306/mysql #<==修改啓動腳本的密碼,才能利用命令來停止服務

mysql_pwd="TMD123"

[root@db01 ~]# /data/3306/mysql stop #<==先停掉服務,因爲有跳過授權表的參數在

Stoping MySQL...

[1]+  Done   mysqld_safe --defaults-file=/data/3306/my.cnf --skip-grant-tables 2>&1 > /dev/null

[root@db01 ~]# /data/3306/mysql start #<==啓動

Starting MySQL...

[root@db01 ~]# ss -tunlp|grep 3306 #<==偵聽正常

tcp    LISTEN     0      600          *:3306        *:*      users:(("mysqld",66695,12))

[root@db01 ~]# mysql -uroot -pTMD123 -S /data/3306/mysql.sock #<==成功登錄

 

1.21 MySQL內中文數據亂碼的原理及如何防止亂碼?(可選)。

保證以下的字符集一致即可

1

2

3

4

5

6

7

8

9

10

11

12

13

14

mysql> show VARIABLES like 'character_set%';               

+--------------------------+--------------+

| Variable_name            | Value        |

+--------------------------+--------------+

| character_set_client     | utf8         |#<==客戶端字符集

| character_set_connection | utf8         |#<==客戶端連接字符集,配置文件指定或建庫表指定

| character_set_database   | utf8         |#<==數據庫的字符集

| character_set_filesystem | binary       |#<==文件系統字符集

| character_set_results    | utf8         |#<==客戶端返回結果字符集

| character_set_server     | utf8         |#<==服務器字符集,配置文件指定或建庫表指定

| character_set_system     | utf8         |#<==Linux系統的字符集

| character_sets_dir       | /application/mysql-5.5.49/share/charsets/ |

+--------------------------+-------------------------------------------+

8 rows in set (0.01 sec)

 

1.22 在把id列設置爲主鍵,在name字段上創建普通索引。

1

2

alter table test add primary key (id);

alter table test add index index_name (name);

 

1.23 在字段name後插入手機號字段(shouji),類型char(11)。

1

alter table test add shouji char(11);

 

1.24 所有字段上插入2條記錄(自行設定數據)

1

insert into test val-ues('4','18','chen','15298914487'),('5','19','he','15298913929');

 

1.25 在手機字段上對前8個字符創建普通索引。

1

alter table test add index index_shouji (shouji(8));

 

1.26 查看創建的索引及索引類型等信息。

1

2

3

desc test;

show create table test\G

show index from test\G

 

1.27 刪除name,shouji列的索引。

1

2

alter table test drop index index_name;

alter table test drop index index_shouji;

 

1.28 對name列的前6個字符以及手機列的前8個字符組建聯合索引。

1

alter table test drop index index_shouji;

 

1.29 查詢手機號以152開頭的,名字爲chen的記錄(此記錄要提前插入)。

select * from test wherename='chen' and shouji like '152%';

1.30 查詢上述語句的執行計劃(是否使用聯合索引等)。

explain select * from testwhere name='chen' and shouji like '152%'\G

 

1.31 把test表的引擎改成MyISAM。

alter table testENGINE=MYISAM;

 


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章