zoukankan      html  css  js  c++  java
  • Mysql基本操作总结

    数据库的基本操作

    1.创建数据库

    # create database xxx;

    2.查看数据库

    # show databases;

    3.删除数据库

    # drop database xxx;

    4.使用/切换数据库

    # use xxx;

    5.退出数据库

    # exit;

    表的基本操作

    1.查看数据库中所有的表

    # show tables;

    2.创建表

    # create table xxx;

    3.查看表结构

    # desc xxx;

    4.查看表详细结构

    # show create table xxx;

    5.删除表

    # drop table xxx;

    6.修改表名

    # alter table xxx rename yyy;

    7.修改字段的数据类型

    # alter table 表名 modify 属性名 新数据类型; (注意外键不能改)
    # alter table B modify user_name char(50);

    8.修改字段名与数据类型

    # alter table 表名 change 旧属性名 新属性名 数据类型;
    # alter table GG change user_name your_name char(50);

    9.增加字段

    # alter table 表明 add 新字段名 数据类型
    # alter table GG add phone varchar(20); # alter table GG add age int(4) not null; # alter table GG add address varchar(30) not null after phone;

    10.删除字段

    # alter table GG drop age;

    11.删除某条记录

    # delete from 表名 where 条件
    # delete from 表名                 //删除所有记录

    12.修改表的存储引擎

    # alter table A engine=MyISAM;

    13.删除表的外键约束

    # alter table 表名 drop foreign key 外键别名
    # alter table yy2 drop foreign key y_fk;

    14.删除表

    # drop table xxx;
    删除父表:先删除所有关联子表的外键,再删除父表

    用户登录与管理相关操作

    1. 创建用户

    -> create user 'test1'@'localhost'identified by '123456';
    -> create user 'test2'@'localhost'identified by 'PASSWORD(123456)';
    -> insert into mysql.user(Host,User,Password,ssl_cipher,x509_issuer,x509_subject)values('hostname','test3',PASSWORD('123456'),'','','');

    2. 查看用户信息

    -> select Host,User,Password from mysql.user where User='test1' or User='test2' or User='test3';
    +-----------+-------+-------------------------------------------+
    | Host | User | Password |
    +-----------+-------+-------------------------------------------+
    | localhost | test1 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
    | localhost | test2 | *A4734F92867FE18E0DD415197D5C33D2E705F0DC |
    | hostname | test3 | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
    +-----------+-------+-------------------------------------------+

    3. 删除用户

    -> drop user 'test2'@'localhost';
    -> delete from mysql.user where Host='localhost' and User='test3';

    4. 修改root用户密码

    # mysqladmin -u root -p password '新密码'
      Enter password:                   --->在这里要输入正确的旧密码
    
    -> update mysql.user set Password=PASSWORD('新密码') where User='root' and Host='localhost';

    5. 修改普通用户密码

    # mysql -u test1 -p
    -> set password=PASSWORD('aixocm');
    
    -> set password for 'test1'@'localhost'=PASSWORD('aixocm');

    6. 解决忘记root用户密码问题

    # /etc/init.d/mysqld start --skip-grant-tables
    # mysql -u root -p --->这样可以无密码登陆进去了 -> update mysql.user set Password
    =PASSWORD('新密码') where User='root' and Host='localhost';

    用户权限管理操作

    1. 添加权限

    grant 权限
    on 数据库名.表名
    to 'username'@'Host'
    
    //新建用户并增加权限 grant select,update -> on *.* --->对所有数据库的所有表 -> to 'test5'@'localhost' identified by 'aixocm' -> with grant option; grant delete -> on *.* -> to 'test5'@'localhost' -> with grant option;

    2. 取消权限

    revoke delete
    -> on *.*
    -> from 'test5'@'localhost';
    
    //取消所有权限 revoke all privileges,grant option from 'test5'@'localhost';

    3. 查看权限

    -> select User,Host,Select_priv,Update_priv,Delete_priv from mysql.user where User='test5';
    +-------+-----------+-------------+-------------+-------------+
    | User | Host | Select_priv | Update_priv | Delete_priv |
    +-------+-----------+-------------+-------------+-------------+
    | test5 | localhost |    Y  |     Y       |      Y      |
    +-------+-----------+-------------+-------------+-------------+
    
    -> show grants for 'root'@'localhost'G;
    *************************** 1. row ***************************
    Grants for root@localhost: GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY PASSWORD '*5DBA547161E5D9C6508C3C6CF5D1B26940E3BC13' WITH GRANT OPTION
    *************************** 2. row ***************************
    Grants for root@localhost: GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION

    4.刷新(加载)权限

    -> flush privileges;

    存储引擎相关操作

    1.查看数据库支持的存储引擎

    # show engines;
    # show enginesG;
    Engine:名称
    Support:是否支持(YES/DEFAULT)
    Comment:说明
    Transaction:是否支持事务
    XA:是否支持分布式交易处理的XA规范
    savapoint:是否支持保存点

    2.查看存储引擎详细信息

    # show engine innodb statusG;

    3.查看默认存储引擎 

    # show variables like 'storage_engine';

    4.修改默认存储引擎

    # vim /etc/my.cnf(配置文件)
    default-storage-engine=xxx
  • 相关阅读:
    SpringCloud分布式配置中心
    SpringCloud服务降级案列
    SpringCloud断路器(Hystrix)
    SpringCloud服务过滤filter
    SpringCloud路由网关Zuul
    SpringCloud微服务实现生产者消费者+ribbon负载均衡
    SpringCloud微服务的Eureka
    忘记MySQL密码以及无法登陆等解决办法
    MySQL备份
    实现两个MySQL数据库之间的主从同步
  • 原文地址:https://www.cnblogs.com/snsdzjlz320/p/5768725.html
Copyright © 2011-2022 走看看