zoukankan      html  css  js  c++  java
  • Mysql常用命令

    原创:转载需注明原创地址 https://www.cnblogs.com/fanerwei222/p/11772821.html

    1. 服务

      启动mysql服务
      net start mysql

      停止mysql服务
      net stop mysql

    2. 登陆

      登陆本机
      mysql -u root -p

      登陆别的机器
      mysql -u root -p -h 192.168.2.111

    3. 密码

      给没有密码的root用户加上密码
      mysqladmin -u root -password 123

      将root的密码从123修改为456(p后面不能有空格)
      mysqladmin -u root -p123 password 456

      (正常登陆)设置root用户localhost机器的密码为1234(8.0以前)
      set password for root@localhost = password('1234');

      (正常登陆)通过更新名称为mysql的数据库中的user表来更新root用户在主机localhost上的密码为1234(8.0以前)
      use mysql;
      update user set password=password('1234') where user='root' and host='localhost';
      flush privileges;

      (正常登陆)修改root用户在主机localhost上的密码为1234(8.0以后)
      use mysql;
      update user set authentication_string='' where user='root';
      alter user 'root'@'localhost' identified by '1234';
      如果提示 ERROR 1396 (HY000): Operation ALTER USER failed for 'root'@'localhost'
      select user, host from user;
      mysql> select user, host from user;
      +------------------+-----------+
      | user             | host      |
      +------------------+-----------+
      | root             | %         |
      | mysql.infoschema | localhost |
      | mysql.session    | localhost |
      | mysql.sys        | localhost |
      +------------------+-----------+
      看到我的root对应的host为%,则继续下面的命令
      alter user 'root'@'%' identified by '1234';
      flush privileges;
      如果alter那一步失败了,关闭当前cmd, 用管理员身份打开cmd, mysql -u root -p直接回车登陆,此时没有密码;
      use mysql;
      alter user 'root'@'%' identified by '1234';
      上面的是% 还是 localhost 视情况而定;
      flush privileges;

      (忘记密码)修改root用户在主机localhost上的密码为1234(8.0以后)
      除了修改密码指令略有不同,上面不同之处已经写出,其他步骤类似;
      进入到mysql/bin目录下,运行cmd,后者运行cmd后进入到mysql/bin目录下;
      net stop mysql;
      mysqld --shared-memory --skip-grant-tables;
      打开新cmd窗口
      开始这个步骤-->(正常登陆)修改root用户在主机localhost上的密码为1234(8.0以后)

      (忘记密码)修改root用户在主机localhost上的密码为1234(8.0以前)
      进入到mysql/bin目录下,运行cmd,后者运行cmd后进入到mysql/bin目录下;
      net stop mysql;
      mysqld --skip-grant-tables
      打开新cmd窗口
      开始这个步骤-->(正常登陆)通过更新名称为mysql的数据库中的user表来更新root用户在主机localhost上的密码为1234(8.0以前)

    4. 用户

      创建一个名称为student主机地址为localhost密码为1234的用户,主机地址可以是具体的ip,也可以是%通配符(@后面不能有空格)
      create user 'student' @'localhost' identified by '1234';

      删除一个名称为student主机地址为localhost的用户
      drop user 'student' @'localhost';

      创建一个用户userFanwei并且赋予他select,insert,update,delete权限,允许他在localhost机器上登陆,密码为 passwdFanwei,允许他访问userDb数据库中的所有表,用*代替userDb  则代表可以访问所有数据库,passwdFanwei为空的话说明不设密码.
      grant select,insert,update,delete on userDb.* to userFanwei@localhost identified by "passwdFanwei";

    5. 授权

      授权命令
      grant 权限 on 数据库.数据表 to '用户' @'主机';

      给主机地址为localhost的student用户授予所有库(create,drop)中的所有表(select,insert,update,delete)的所有权限
      grant all on *.* to 'student' @'localhost';
      create databse student;

      给主机地址为localhost的student用户授予student库(create,drop)中的所有表(select,insert,update,delete)的所有权限
      grant all on student.* to 'student' @'localhost';

      给主机地址为localhost的student用户授予student库(create,drop)中的所有表(select,insert,update,delete)的所有权限
      grant all privileges on student.* to 'student' @'localhost';

      给主机地址为localhost的student用户授予student库(create,drop)中的所有表(select,insert,update,delete)的所有权限
      grant select,insert,update,delete,create,drop on student.* to 'student' @'localhost';

      取消student用户所有数据库(表)的所有权限
      revoke all on *.* from student;

    6. 数据库操作

      显示所有的库
      show databases;

      创建一个名称为dbName的数据库
      create database dbName;

      删除一个名称为dbName的数据库
      drop database dbName;

      切换到一个名称为dbName的数据库
      use dbName;

    7. 表操作

      创建一个名称为tableName的表
      create table tableName();

      设置外键
      constraint 外键名(默认为fk_标识加上本表名称简写或全称stu_加上外表名称简写或全称class) foreign key(本表的外键列名) references 表名(关联表的关联字段)

    create table class_user(
        cid int(11) not null auto_increment primary key,
        cname varchar(20) not null,
       createTime timestamp not null default now( ) comment '创建时间'
    )engine=innodb default charset=utf8; create table stu_user( sid int(11) not null auto_increment primary key, sname varchar(20) not null, gender char(2) not null, class_id int(11) not null, constraint fk_stu_class foreign key (class_id) references class_user(cid) )engine=innodb default charset=utf8; create table stu_user_wice( sid int not null, pid int not null, primary key(sid, pid) );
    添加列
    alter table 表名 add 列名 类型
    
    删除列
    alter table 表名 drop column 列名
    修改列
    alter table 表名 modify column 列名 类型;  -- 类型
    alter table 表名 change 原列名 新列名 类型; -- 列名,类型
            
      
    添加主键
    alter table 表名 add primary key(列名);
            
    删除主键
    alter table 表名 drop primary key;
    alter table 表名  modify  列名 int, drop primary key;
            
      
    添加外键
    alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);
    
    删除外键
    alter table 表名 drop foreign key 外键名称
      
    修改默认值
    alter table 表名 alter 列名 set default 默认值;
    
    删除默认值
    alter table 表名 alter 列名 drop default;
    删除一个名称为tableName的表
    drop table tableName;
    
    清空表
    delete table '表名'
    或者
    truncate table '表名'
    拷贝表stu_user的数据到stu_user_copy
    INSERT INTO stu_user_copy ( scid, scname, sgender ) SELECT
    sid,
    sname,
    gender 
    FROM
        stu_user;

    8. 备份还原

      备份(根据不同备份需求-d或者-t可以省略)
      mysqldump -u 用户名 -p -d 数据库名 -t 表名 > 盘符:路径文件名.sql

      还原
      source 盘符:路径文件名.sql;
      

  • 相关阅读:
    jmeter(二十三)分布式测试
    jmeter(二十二)内存溢出原因及解决方法
    jmeter(二十一)jmeter常用插件介绍
    jmeter(二十)阶梯式加压测试
    jmeter(十九)HTTP属性管理器
    《jmeter:菜鸟入门到进阶》系列
    jmeter(十八)关联之XPath Extractor
    <转>jmeter(十七)目录结构
    jmeter(十六)配置元件之计数器
    Springmvc中参数的绑定
  • 原文地址:https://www.cnblogs.com/fanerwei222/p/11772821.html
Copyright © 2011-2022 走看看