查询
1 2 3 4 5 6 7
| -- 查看表创建时间 select * from information_schema.tables where 1 = 1 and table_schema = 'schema' -- and table_name = 'table' order by create_time desc
|
DDL
1 2
| -- 额外添加自增主键 ALTER TABLE `t_user` ADD `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;
|
用户管理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| -- 查看用户 select user, host from mysql.user;
-- 创建用户 create user 'root'@'%' identified by 'root'; -- 查看用户授权 show grants for 'root'@'%';
-- 授权整个数据库权限给指定用户 grant all privileges on `aneat`.* to 'aneat'@'%';
-- 授权数据库对象给指定用户 grant all privileges on `dbname`.`tablename` to 'aneat'@'host';
-- 授权用户select, insert, update, delete权限 grant select, insert, update, delete on 'nd'.* to 'bxview'@'%';
-- 撤销用户select权限 revoke select on 'nd'.* from 'bxview'@'%';
-- 撤销数据库对象给指定用户 revoke all privileges on `dbname`.`tablename` from 'aneat'@'host';
-- 刷新权限 flush privileges;
-- 修改密码 set password for 'root'@'%' = password('root');
-- 删除用户 drop user 'root'@'%';
|
创建数据库和用户(示例)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| -- 查看数据库列表 show databases;
-- 创建数据库 create database laravel5;
-- 查看用户 select user, host from mysql.user;
-- 创建用户 create user 'laravel5'@'%' identified by 'laravel5';
-- 授权整个数据库权限给指定用户 grant all privileges on `laravel5`.* to 'laravel5'@'%';
-- 刷新权限 flush privileges;
|
数据迁移
导出执行结果
1 2 3 4 5
| -- mysqldump 导出sql,多个表使用空格隔开 mysqldump -h192.168.1.11 -uroot -proot dbname tablename --where="name = '1'" > result.sql
-- mysql 输出查询结果 mysql -h192.168.1.11 -uroot -proot dbname -e "SELECT * FROM tablename" > result.sql
|
导出数据库对象mysqldump方式
1 2 3 4 5
| -- mysqldump 导出整个数据库包括存储过程和函数 (--routines) mysqldump -uroot -proot -R dbname > dbname.sql
-- mysqldump 导出仅数据库结构 (--no-data, -d) mysqldump -uroot -proot -d dbname > dbname.sql
|
导出数据库对象mysqlpump方式(5.7.8之后)
MySQL 5.7 mysqlpump 备份工具说明
导入
1 2 3
| source path/script.sql 或 mysql -h192.168.1.11 -uroot -proot [--default-character-set=utf8] dbname < result.sql
|
更改MySQL数据库存储位置
修改my.ini属性datadir = 更改后的路径[如 D:/resource/db/mysql5.6/data/],重启服务生效
注意
- Windows中不要使用powershell执行导出命令(或使用
--result-file
代替>
符号)