zoukankan      html  css  js  c++  java
  • 记一次Mysql魔鬼实训

    1.查看某个Mysql数据库当前使用的字符集

    show create database 【库名称】

    2.查看当前书库版本信息

    #mysql -V

    MariaDB [(none)]> use mysql;

    MariaDB [mysql]> select version();

    3.查看当前登录的用户

    MariaDB [mysql]> select user();

    4.创建GBK字符集的数据库test1;

    MariaDB [mysql]> create database test1 default charset=gbk;

     5.查看某个Mysql用户拥有的权限(show grants for [用户@‘来源IP’])

    MariaDB [mysql]> show grants for root@'localhost';

    6.创建user01,并授予管理 数据库testdb权限;格式为:grant 【权限列表】on 【库.表】【用户@来源IP】 identified by 【密码】

    grant all on testdb.*  user01@‘localhost’ identified by ‘123.com’

    7.查看当前数据库中有哪些用户

    select user from mysql.user;

    8.创建一个test表,要求存储引擎为INNODB,字符集为gbk,字段ID,长度为4,name 

    MariaDB [testdb]> create table test(id int(4),name varchar(16))engine=INNODB default charset=gbk;

    9.查看test表结构以及表结构的SQL语句

    MariaDB [testdb]> desc test

    MariaDB [testdb]> show create table testG

    10.向test表插入某条数据;或者批量插入多行数据

    MariaDB [testdb]> insert into test values(2,'test02'),(3,'test03'),(4,'test04');

    11.过滤查询,查看某个字段下的某个名称的记录,如查询test02的单行记录

    MariaDB [testdb]> select * from test where name='test02';

     

    12.替换表中某个字段的记录,如将id为2的名称更改为BBB

    MariaDB [testdb]> update test set name = 'BBB' where id = '2';

     

    13.在表中添加某个字段alter table 【表名称】add 【字段名称】【字段类型】 after 【需要在某个字段后面插入的字段名称】

    如,我现在需要在baidu表中name字段后面加上“CCTV”字段, 类型为tinyint(2)

    MariaDB [testdb]> alter table baidu add CCTV tinyint(2) after name;

     

     14.删除表中的某个字段(alter table 【表名称】drop 【字段】)

    MariaDB [testdb]> alter table baidu drop cctv;

     15.不退出数据,完成备份testdb数据库(system mysqldump -u【用户】 -p【密码】  【需要备份的数据库名称】> 【备份路径/*.sql】)

    MariaDB [(none)]> system mysqldump -uroot testdb > /root/testdb.sql

    同理,不退出数据库,完成数据恢复

    MariaDB [(none)]> system mysql -uroot testdb < /root/testdb.sql

     16.删除表中的所有数据(delete from )

    delete from test;

    17.修改库/表中的字符集alter 【库/表】 【库/表名称】 default charset 【字符集】

    MariaDB [testdb]> alter table baidu default charset utf8;

     MariaDB [testdb]> alter table baidu default character set  gbk;

    MariaDB [(none)]> alter database testdb default charset=utf8;

    MariaDB [(none)]> alter database testdb default character set gbk;

    18.在某个设置主键(alter table 【表名称】add primary key(字段))

    alter table test add primary key(id)

    19.在某个字段创建普通索引create index 【索引自定义名称】 on 【表名称】【字段】 

    MariaDB [testdb]> create index hexunindex on hexun(name(16));

    20.在指定表中插入某个字段alter table 【表名称】add [字段名称/char(11)]

    MariaDB [testdb]> alter table hexun add caiji char(11);

     21.查看表中的索引

    MariaDB [testdb]> show index from hexun;

    MariaDB [testdb]> show create table hexunG

     22.查看数据表的索引类型

    MariaDB [testdb]> show keys from hexunG

     23.删除指定表中的索引(drop index 【索引名称】on  【表名称】)

    MariaDB [testdb]> drop index hexunindex on hexun

     24.修改数据表的存储引擎(alter table 【表名称】 engine=【存储引擎类型】)

    MariaDB [testdb]> alter table hexun engine=innodb;

     25.撤回某个用户对某个库中的权限(revoke 【权限列表】 on 【库.*】 from 【用户@'来源IP'】)

    MariaDB [testdb]> revoke select on testdb.* from zhangsan@'localhost';

     26,跳过mysql密码验证,如何找回?

    #skip-grabt-tables   #将此配置写入/etc/my.conf重启即可免密登录

    #mysqld_safe --skip-grant-tables & 启动数据库服务(不推荐此种方式)

  • 相关阅读:
    程序员眼中的 SQL Server-执行计划教会我如何创建索引?
    SQL Server死锁排查
    详解Java中的clone方法 -- 原型模式
    sql-索引的作用(超详细)
    java.util.ConcurrentModificationException 解决办法
    SqlServer索引的原理与应用
    数据库性能优化三:程序操作优化
    数据库性能优化二:数据库表优化
    数据库性能优化一:数据库自身优化(大数据量)
    SQL索引一步到位
  • 原文地址:https://www.cnblogs.com/bixiaoyu/p/9733750.html
Copyright © 2011-2022 走看看