MySql教程

MySQL面试题简单整理

本文主要是介绍MySQL面试题简单整理,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

(1)基础笔试命令考察
1.开启MySQL服务
/etc/init.d/mysqld start
service mysqld start
systemctl start mysqld

2.检测端口是否运行
lsof -i :3306
netstat -lntup |grep 3306

3.为MySQL设置密码或者修改密码
设置密码
mysql -hlocalhost -uroot -ppassword -e "set passoword for root = password('passowrd')"
mysqladmin -hlocalhost -uroot password "NEWPASSWORD"
更改密码
mysqladmin -hlocalhost -uroot password oldpassword "NEWPASSWORD";
update mysql.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进入数据库执行上面的修改密码即可
MysQL 5.7以上版本修改默认密码命令
alter user 'root'@'localhost' identified by 'root'

4.登陆MySQL数据库
mysql -hlocalhost -uroot -ppassword

5.查看当前数据库的字符集
show create database DB_NAME;

6.查看当前数据库版本
mysql -V
mysql -hlocalhost -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语句
desc test;
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表中的所有数据,并查看
delete from test;
select * from test;

22.删除表test和mingongge数据库并查看
drop table test;
show tables;
drop database mingongge;
show databases;

23.不退出数据库恢复以上删除的数据
system mysql -uroot -pMgg123.0.

24.把库表的GBK字符集修改为UTF8
alter database mingongge default character set utf8;
alter table test default character set utf8;

25.把id列设置为主键,在Name字段上创建普通索引
alter table test add primary key(id);
create index mggindex on test(name(16));

26.在字段name后插入手机号字段(shouji),类型char(11)
alter table test add shouji char(11);
#默认就是在最后一列后面插入新增列

27.所有字段上插入2条记录(自行设定数据)
insert into test values('4','23','li','13700000001'),('5','26','zhao','13710000001');

28.在手机字段上对前8个字符创建普通索引
create index SJ on test(shouji(8));

29.查看创建的索引及索引类型等信息
show index from test;
show create table test\G
#下面的命令也可以查看索引类型
show keys from test\G

30.删除Name,shouji列的索引
drop index SJ on test;
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;

35.收回mingongge用户的select权限
revoke select on mingongge.* from mingongge@localhost;

36.删除mingongge用户
drop user migongge@localhost;

37.删除mingongge数据库
drop database mingongge

38.使用mysqladmin关闭数据库
mysqladmin -uroot -pMgg123.0. shutdown
lsof -i :3306

39.MySQL密码丢了,请找回?
mysqld_safe --skip-networking --skip-grant-tables & #启动数据库服务
mysql -hlocalhost -uroot -ppassowrd -e "use mysql;update user set passowrd = PASSWORD('newpassword') where user = 'root';flush privileges;"

这篇关于MySQL面试题简单整理的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!