当前位置:Gxlcms > mysql > MySQL命令行下18个常用命令

MySQL命令行下18个常用命令

时间:2021-07-01 10:21:17 帮助过:16人阅读

在日常的网站维护和管理中,会用到非常多的SQL语句,
熟练使用对网站管理有很多好处,尤其是站群管理的时候。

下面列一些常用的命令做备记。

1、显示数据库
show databases
显示表
show tables;

 2、创建用户
创建root用户密码为123

  1. use mysql;
  2. grant all on *.* to root@'%' identified by '123' with grant option;
  3. commit;

 3、修改密码

  1. grant all on *.* to xing@'localhost' identified by '123456' with grant option;
  2. update user set password = password('newpwd') where user = 'xing' and host='localhost';
  3. flush privileges;

 4、创建数据库testdb:

  1. create database testdb;

 5、预防性创建数据库:

  1. create database if not testdb;

 6、创建表:

  1. use testdb;
  2. create table table1(
  3. username varchar(12),
  4. password varchar(20));

 7、预防性创建表aaa:

  1. create table if not exists aaa(ss varchar(20));

 8、查看表结构:

  1. describe table1;

 9、插入数据到表table1:

  1. insert into table1(username,password) values
  2. ('leizhimin','lavasoft'),
  3. ('hellokitty','hahhahah');
  4. commit;

 10、查询表table1:

  1. select * from table1;

 11、更改数据:

  1. update table1 set password='hehe' where username='hellokitty';
  2. commit;

 12、删除数据:

  1. delete from table1 where username='hellokitty';
  2. commit;

13、给表添加一列:

  1. alter table table1 add column(
  2. sex varchar(2) comment '性别',
  3. age date not null comment '年龄'
  4. );
  5. commit;

14、修改表结构

从查询创建一个表table1:

  1. create table tmp as
  2. select * from table1;

15、删除表table1:

  1. drop table if exists table1;
  2. drop table if exists tmp;

16、备份数据库testdb

  1. mysqldump -h 192.168.3.143 -u root -p pwd -x --default-character-set=gbk >C:\testdb.sql

17、删除数据库testdb

  1. drop database testdb;

18、恢复testdb数据库
首先先建立testdb数据库,然后用下面命令进行本地恢复

  1. mysql -u root -pleizhimin testdb <C:\testdb.sql

这18个MYSQL命令都是管理员在日常维护中经常会用到的,熟练使用这些命令会使你的工作非常轻松

您可能感兴趣的文章:

  • mysql命令行爱好者必备工具mycli
  • Mysql经典高逼格/命令行操作(速成)(推荐)
  • MySQL 5.7安装好后打开命令行窗口闪退的解决方法
  • C#实现MySQL命令行备份和恢复
  • mysql命令行中执行sql的几种方式总结
  • MySQL命令行中给表添加一个字段(字段名、是否为空、默认值)
  • 简单了解操作mysql数据库的命令行神器mycli

人气教程排行