mysql 常用命令

  • 显示当前的缓存状态
    show variables like '%query_cache%';

  • 显示当前的存储类型
    show engines;

  • 删除数据库test_db下所有表
    select concat('drop table ',table_name,';') from information_schema.tables where table_schema='test_db';

  • 清空表数据
    truncate tablename;

  • 查询库csgw中所有表的记录数
    use information_schema
    select table_name,table_rows from tables where TABLE_SCHEMA='csgw' order by table_rows desc;

  • 显示当前数据库的user表结构
    show columns from user;

  • 切换当前数据库到whb_test
    user whb_test;

  • 创建一个user表,主键为name,20字符,passwd字段为64字符不能为空
    CREATE TABLE user(name VARCHAR(20) not null primary key, passwd VARCHAR(64) not null, id int(4));

  • 给一个用户赋予数据库权限

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    语法:mysql> grant 权限1,权限2,...权限n on 数据库名称.表名称 to 用户名@用户地址 identified by '连接口令';
    权限1,权限2,...权限n代表
    select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file等14个权限

    实例:mysql>grant select,insert,update,delete,create,drop on whb_test.employee to joe@10.163.225.87 identified by '123456';
    给来自10.163.225.87的用户joe分配可对数据库whb_test的employee表进行select,insert,update,delete,create,drop等操作的权限,并设定口令为123456
    mysql>grant all privileges on whb_test.* to whb@10.163.225.87 identified by '123456';
    给来自10.163.225.87的用户whb分配可对数据库whb_test所有表进行所有操作的权限,并设定口令为123
    mysql>grant all privileges on *.* to joe@10.163.225.87 identified by '123';
    给来自10.163.225.87的用户joe分配可对所有数据库的所有表进行所有操作的权限,并设定口令为123
    mysql>grant all privileges on *.* to joe@localhost identified by '123';
    给本机用户joe分配可对所有数据库的所有表进行所有操作的权限,并设定口令为123

    刷新权限:
    FLUSH PRIVILEGES;
  • 创建一个名为whb_test的数据库
    CREATE DATABASE whb_test CHARACTER SET 'utf8' COLLATE 'utf8_general_ci';

  • 用whb用户登录172.16.1.110服务器的mysql:
    mysql -uwhb -p -h 172.16.1.110
    使用此方法登录必须有ip

  • 删除一个用户名为whb的用户

    1
    2
    drop user 'whb'@'%'; 
    flush privileges;
  • 创建一个用户名为whb,密码为123456的用户
    create user whb identified by '123456';

  • 创建一个密码为123456的用户test_user,对所有数据库有操作权限
    grant select,insert,update,delete on *.* to test_user@"%" Identified by "123456";

  • 登录方法:
    mysql -uxxxx -pxxxx --host=192.168.1.110

  • root登录

    1
    2
    mysql -uroot -p123456 mysql
    mysql -utest_user -p123456 -h 192.168.188.125 -P 3306
  • 查看mytable表编码格式
    show create table mytable;

  • 查看test数据库的编码格式
    show create database test;

设置数据库db_name默认为utf8:
???


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!