mysql开发常用SQL

创建数据库

CREATE DATABASE IF NOT EXISTS $database DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
CREATE DATABASE $database DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

创建用户并提供相应权限

只是创建用户

CREATE USER phpergao@'localhost' IDENTIFIED BY 'yourpasswd';

赋予权限

GRANT select,update on phpergao.* to phpergao@'localhost';
GRANT index ON phpergao.* TO phpergao@'192.168.0.%';

创建用户并赋予权限

GRANT ALL PRIVILEGES ON phpergao.* TO 'phpergao'@'localhost' IDENTIFIED BY 'yourpasswd';

相反的revoke 跟 grant 的语法差不多,只需要把关键字 “to” 换成 “from” 即可:

REVOKE ALL PRIVILEGES ON phpergao.* FROM 'phpergao'@'localhost';

ALL PRIVILEGES 可以换为 select,insert,update,delete,create,drop,index,alter,grant,references,reload,shutdown,process,file 等14个权限。

删除用户

DELETE FROM user WHERE User='phpergao' and Host='localhost';

修改用户密码

UPDATE USER SET PASSWORD = PASSWORD ('newpasswd') WHERE USER = 'phpergao' AND HOST = 'localhost';

刷新权限:

FLUSH PRIVILEGES;

查看用户权限

查看自己的权限:

SHOW GRANTS;

查看其他人的权限:

SHOW GRANTS FOR 'phpergao'@'%';

新建数据表

DROP TABLE IF EXISTS `workers_info`;  
CREATE TABLE `workers_info` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `workername` varchar(20) NOT NULL,
  `sex` enum('F','M','S') DEFAULT 'S',
  `salary` int(11) DEFAULT '0',
  `email` varchar(30) DEFAULT NULL,
  `EmployedDates` date DEFAULT NULL,
  `department` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

忘记了mysql密码

修改配置文件

[mysqld] 
datadir=/var/lib/mysql 
socket=/var/lib/mysql/mysql.sock 

ADD:

skip-name-resolve 
skip-grant-tables`

然后重启MySQL服务并免密码登录

service mysqld restart
mysql

执行修改密码SQL:将root用户的密码统一设为’admin’

UPDATE mysql.user SET Password=password('123456') WHERE User='root';

还原MySQL配置文件并重启服务

修改用户登录HOST:

UPDATE mysql.user SET Host='&' WHERE User='root';

参考

http://renxiangzyq.iteye.com/blog/763837

    原文作者:whis
    原文地址: https://segmentfault.com/a/1190000002539866
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞