运维之MySQL面试题简单整理

运维之MySQL面试题简单整理

(1)基础笔试命令考察

1.开启MySQL服务

1./etc/init.d/mysqld start
2.service mysqld start
3.systemctl start mysqld

2.检测端口是否运行

1.lsof -i :3306
2.netstat -lntup |grep 3306

3.为MySQL设置密码或者修改密码

设置密码
1.mysql -uroot -ppassword -e "set passoword for root =   password('passowrd')"
2.mysqladmin -uroot password "NEWPASSWORD"
更改密码
1.mysqladmin -uroot password oldpassword "NEWPASSWORD"
2.use mysql;
3.update user set passowrd = PASSWORD('newpassword') where user = 'root';

5.7的版本改成了如下:
(update mysql.user set authentication_string =PASSWORD('mxl20152873..')where user ='root';)或者也可以用(SET PASSWORD = PASSWORD('root(密码)');)
 flush privileges;

获得初始密码
grep 'password' /usr/local/mysqld/log/mysql_error.log
如果还是不可以登录直接进入/etc/my.conf中加上skip-grant-tables跳过密码,然后MySQL进入数据库执行上面的修改密码即可

msyql 5.7以上版本修改默认密码命令
alter user 'root'@'localhost' identified by 'root' 

4.登陆MySQL数据库

mysql -uroot -ppassword

5.查看当前数据库的字符集

show create database DB_NAME;

6.查看当前数据库版本

1.mysql -V
2.mysql -uroot -ppassowrd -e "use mysql;select version();"

7.查看当前登录的用户

select user();

8.创建GBK字符集的数据库mingongge,并查看已建库完整语句

create database mingongge DEFAULT CHARSET GBK COLLATE gbk_chinese_ci;

9.创建用户mingongge,使之可以管理数据库mingongge

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

10.查看创建的用户mingongge拥有哪些权限

show grants for mingongge@localhost

11.查看当前数据库里有哪些用户

select user from mysql.user;

12.进入mingongge数据库

use mingongge

13.创建一innodb GBK表test,字段id int(4)和name varchar(16)

create table test (id int(4),name varchar(16))ENGINE=innodb DEFAULT CHARSET=gbk;

14.查看建表结构及表结构的SQL语句

1.desc test;
2.show create table test\G

15.插入一条数据“1,mingongge”

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

16.再批量插入2行数据 “2,民工哥”,“3,mingonggeedu”

insert into test values('2','民工哥'),('3','mingonggeedu');

17.查询名字为mingongge的记录

select * from test where name = 'mingongge';

18.把数据id等于1的名字mingongge更改为mgg

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

19.在字段name前插入age字段,类型tinyint(2)

alter table test add age tinyint(2) after id;

20.不退出数据库,完成备份mingongge数据库

system mysqldump -uroot -pMgg123.0. -B mingongge >/root/mingongge_bak.sql

21.删除test表中的所有数据,并查看

1.delete from test;
2.select * from test;

22.删除表test和mingongge数据库并查看

1.drop table test;
2.show tables;
3.drop database mingongge;
4.show databases;

23.不退出数据库恢复以上删除的数据

system mysql -uroot -pMgg123.0.

24.把库表的GBK字符集修改为UTF8

1.alter database mingongge default character set utf8;
2.alter table test default character set utf8;

25.把id列设置为主键,在Name字段上创建普通索引

1.alter table test add primary key(id);
2.create index mggindex on test(name(16));

26.在字段name后插入手机号字段(shouji),类型char(11)

1.alter table test add shouji char(11);
2.#默认就是在最后一列后面插入新增列

27.所有字段上插入2条记录(自行设定数据)

insert into test values('4','23','li','13700000001'),('5','26','zhao','13710000001');

28.在手机字段上对前8个字符创建普通索引

create index SJ on test(shouji(8));

29.查看创建的索引及索引类型等信息

1.show index from test;
2.show create table test\G
3.#下面的命令也可以查看索引类型
4.show keys from test\G

30.删除Name,shouji列的索引

1.drop index SJ on test;
2.drop index mggindex on test;

31.对Name列的前6个字符以及手机列的前8个字符组建联合索引

create index lianhe on test(name(6),shouji(8));

32.查询手机号以137开头的,名字为zhao的记录(提前插入)

select * from test where shouji like '137%' and name = 'zhao';

33.查询上述语句的执行计划(是否使用联合索引等)

explain select * from test where name = 'zhao' and shouji like '137%'\G

34.把test表的引擎改成MyISAM

alter table test engine=MyISAM;

1

35.收回mingongge用户的select权限

revoke select on mingongge.* from mingongge@localhost;

36.删除mingongge用户

drop user migongge@localhost;

37.删除mingongge数据库

drop database mingongge

38.使用mysqladmin关闭数据库

1.mysqladmin -uroot -pMgg123.0. shutdown
2.lsof -i :3306

39.MySQL密码丢了,请找回?

1.mysqld_safe --skip-grant-tables & #启动数据库服务
2.mysql -uroot -ppassowrd -e "use mysql;update user set passowrd = 	PASSWORD('newpassword') where user = 'root';flush privileges;"

你可能感兴趣的:(linux,mysql)