标签:mysql命令
mysql基本操作
对于一个企业来说,数据的重要性是毋庸置疑的。作为一个运维人员,必须要掌握基本的数据库操作,为了方便以后查看,我把一些简单的数据库命令写出来。
查看数据库
MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+
创建数据库
MariaDB [(none)]> create database xhk;
进入数据库并创建一个表
MariaDB [(none)]> use xhk; Database changed MariaDB [xhk]> create table stu (id int auto_increment primary key,name varchar(20) unique key);
查看表的结构,有2种方法
MariaDB [xhk]> DESC stu; +-------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(20) | YES | UNI | NULL | | +-------+-------------+------+-----+---------+----------------+ MariaDB [xhk]> show create table stu; +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | stu | CREATE TABLE `stu` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `name` (`name`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 | +-------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
在表中插入多条数据
MariaDB [xhk]> insert into stu(id,name) value(1,‘xhk‘),(2,‘xxx‘);
查询表的内容
MariaDB [xhk]> select * from stu; +----+------+ | id | name | +----+------+ | 1 | xhk | | 2 | xxx | +----+------+
也可以按照需要查询
MariaDB [xhk]> select * from stu where id = 1; +----+------+ | id | name | +----+------+ | 1 | xhk | +----+------+
使用*号查询所有,但线上数据很大,使用*可能导致数据库压力增大,影响性能,建议使用特定的值来查询
MariaDB [xhk]> select name from stu; +------+ | name | +------+ | xhk | | xxx | +------+
修改表中的数据
MariaDB [xhk]> update xhk set name=‘qqq‘ where id=2;
删除表中的一行
MariaDB [xhk]> delete from stu where id=2 ;
增加表的字段
MariaDB [xhk]> alter table stu add age int;
删除表的字段
MariaDB [xhk]> alter table stu drop age;
修改字段的类型
MariaDB [xhk]> alter table stu modify name varchar(20);
修改字段的名字以及类型
MariaDB [xhk]> alter table stu change name names varchar(10);
修改表的名字
MariaDB [xhk]> rename table stu to student;
删除表中的全部数据
MariaDB [xhk]> delete from student;
创建用户和设置密码并且指定用户对所有库都有所有权,并且允许所有主机
MariaDB [xhk]> grant all on *.* to "xhk1"@"%" identified by "123456";
修改用户的密码
MariaDB [xhk]> update mysql.user set password=password(‘redhat‘) where user="xhk1";
查看用户的权限
MariaDB [xhk]> show grants for "xhk1"@"localhost"; +----------------------------------------------------------------------------------------------------------------------+ | Grants for xhk1@localhost | +----------------------------------------------------------------------------------------------------------------------+ | GRANT ALL PRIVILEGES ON *.* TO ‘xhk1‘@‘localhost‘ IDENTIFIED BY PASSWORD ‘*6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9‘ | +----------------------------------------------------------------------------------------------------------------------+
创建用户只有对xhk库的student表有查询权限,并且只允许本地登录
MariaDB [xhk]> grant select,update,delete,insert on xhk.student to "xhk1"@"localhost" identified by "123456";
收回用户的某个权限
MariaDB [xhk]> revoke update,delete,insert on xhk.student to "xhk1"@"localhost";
删除用户
MariaDB [xhk]> delete from mysql.user where name="xhk1" and host="localhost";
本文出自 “xhk__运维” 博客,转载请与作者联系!
标签:mysql命令
原文地址:http://xhk777.blog.51cto.com/13405744/1980888