mysql常用命令

连接mysql

mysql -h 主机地址  -u 用户名 -p 用户密码

1、本机连接

cd mysqlin
mysql -u root -p
  • 新安装的mysql,超级用户root没有密码

mysql服务启动/停止

net start mysql
net stop mysql

增加新用户

grant 权限 on 数据库.* to 用户名@登录主机 identified by 密码

grant select,insert,update,delete on *.* to user@localhost Identified by 'password1'

修改密码

mysqladmin -u 用户名 -p 旧密码 password 新密码

创建数据库

create database <数据库名>;

显示数据库

show databases;

显示数据库表的结构

describe 表名;

删除数据库

drop database <数据库名>;

* drop database if exists <数据库名>;

连接数据库

use <数据库名>;

查看当前选择的数据库

1、显示mysql版本

select version();

2、显示当前选择的数据库

select database();

3、显示当前时间

select now();

4、显示当前年月日

select year(current_date);

select month(current_date);

select dayofmonth(current_date);

创建数据表

create table <表名>(<字段名1><类型1>,<字段名2><类型2>)

create table myClass(
    id int(4) not null primary key auto_increment,
    name char(20) not null,
    sex int(4) not null default '0',
    degree double(16,2)
);

删除数据表

drop table <表名>;

清空表中记录

delete from 表名;

表插入数据

insert into <表名> [<字段名1>,<字段名2>] values (值1),(值2);
eg:
insert into myClass values(1,'Tom',96.45),(2,'Joan',96.59);

查询表格数据

select <字段名> from <表名> where <表达式>
select * from myClass order by id limit 0,2;

删除表格数据

delete from 表名 where 表达式;
delete from myClass where id=1;

修改表中数据

update 表名 set 字段=新值 where 条件;

增加字段

alter table myClass add school char(28);

加索引

alter table <表名> add index <索引名>;

加主关键字的索引

alter table <表名> add primary key(字段名)
alter table myClass add primary key(id);

删除某个索引

alter table 表名 drop index 索引名;

修改表名

rename table 原表名 to 新表名;
rename table myClass to employee;

备份数据库

1、导出整个数据库

mysqldump -u 用户名 -p 数据库名 > 导出的文件名
mysqldump -u root -p123456 --databases dbname> mysql.dbname
把数据库dbname导出到文件mysql.dbname中

2、导入数据

mysqlimport -u root -p123456 < mysql.dbname

sql脚本创建实例

drop database if exists school;
create database school;
use school;
create table teacher(
    id int(3) auto_increment not null primary key;
    name char(10) not null,
    address varchar(50) default '深圳'
);

// 插入字段
insert into teacher values('','老王','大连一中','1988-10-10');
insert into taacher values('','老李','大连二中','1990-11-11');

dos下: mysql -uroot -p密码 < school.sql
linux下: source school.sql

本地文件拷贝到远程服务端

scp school.sql root@47.99.240.172:/usr/local/home

ecs连接mysql数据库

mysql -h rm-bp1s96527g3211auu125010.mysql.rds.aliyuncs.com  -u mydb01 -p mysql

原文地址:https://www.cnblogs.com/nanhuaqiushui/p/12326186.html