Mysql 常用命令

1 远程连接数据库

mysql -h<服务器IP> -P<端口> -u<用户> -p<密码> -D<实例名>

### 执行SQL脚本
mysql -h<服务器IP> -P<端口> -u<用户> -p<密码> -D<实例名> -e "slect ..."
mysql -h<服务器IP> -P<端口> -u<用户> -p<密码> -D<实例名> < a.sql
-h --服务器IP
-P --端口
-u --用户
-p --密码
-D --实例名
-e --执行SQL
-f --强制执行SQL,出错不退出
-v --显示出错的SQL
--default-character-set=utf8mb4--字符集

### 执行SQL导出csv
mysql -h<服务器IP> -P<端口> -u<用户> -p<密码> -D<实例名> < a.sql|sed 's/\t/","/g;s/^/"/g;s/$/"/g' >/tmp/1.csv

2 权限

2.1 创建用户

mysql> create user 'test'@'localhost'  identified by '123456'; -- 本地登录 
mysql> create user 'test'@'%'  identified by '123456'; -- 远程登录 

2.2 修改密码

mysql> alter user 'test'@'%' identified by '12356';
mysql> flush privileges;
mysql> SET PASSWORD=PASSWORD('123456');

2.3 授权

mysql> grant all privileges on testdb.* to 'test'@'%';
mysql> flush privileges;

### 查询权限
mysql> select * from mysql.db where user='test'\G;
mysql> show grants for test;

3 SQL

3.1 建库

mysql> show databases;    -- 查看数据库
mysql> create database test;    -- 创建库
mysql> drop database test;      -- 删除库

3.2 建表

### 建表
mysql> 
create table emp(
    deptno int(10)),
    ename varchar(10),
    hiredate date,
    sal decimal(10,2));
 
### 删表
mysql> drop table emp;

mysql> create table tmp01 like emp;    --备份表
mysql> insert into tmp01 select * from emp;
 
mysql> desc emp;    -- 查看表定义
mysql> show create table emp \G;

3.3 修改表字段

mysql> alter table emp modify ename varchar(20);    -- 修改字段
mysql> alter table emp add column age int(3);    -- 增加字段
mysql> alter table emp change age age1 int(4);    -- 修改字段名
mysql> alter table emp add birth date after ename;    -- 增加字段,指定位置
mysql> alter table emp modify age1 int(5) first;
mysql> alter table emp rename emp1;    -- 修改表名

3.4 DML SQL

### insert
mysql> insert into emp value(1001'lin','1985-04-19',20000);
mysql> insert into emp value(1002,'xin','1988-06-29',10000);
mysql> insert into emp value(1003,'zhao','1989-01-23',12000);

### update
mysql> update emp set sal=50000 where ename='lin';

### delete/truncate
mysql> delete from emp where ename='zhao';
mysql> truncate tbale emp;

### select
mysql> select * from emp where deptno=1;    --select
mysql> select * from emp order by sal desc;
mysql> select * from emp order by sal limit 10;
mysql> select count(1) from emp;
 
mysql> select deptno,count(1) from emp group by deptno;        --group by
mysql> select deptno,count(1) from emp group by deptno having count(1)>10;
 
mysql> select sum(sal),max(sal),min(sal) from emp;
 
mysql> select ename,deptname from emp,dept where emp.deptno=dept.deptno;    -- 内连接
mysql> select ename,deptname from emp left join dept on emp.deptno=dept.deptno;    -- 外连接
mysql> select ename,deptname from emp right join dept on emp.deptno=dept.deptno;
 
mysql> select * from emp where deptno in(select deptno from dept);    -- 子查询,表连接性能优于子查询
 
mysql> 
select deptno from emp
union all
select deptno from dept;

3.5 DDL INDEX

### 创建单一索引
mysql> create index sal_index on emp(sal);
 
### 创建联合索引
mysql> create index deptno _sal_index on emp(deptno,sal);
 
### 验证是否走索引
mysql> explain select * from emp where sal='10000' \G

### 删除索引
mysql> drop index sal_index on emp;

你可能感兴趣的:(Mysql,数据库,linux,运维)