创建数据库

# utf8mb4_unicode_ci更准
CREATE DATABASE IF NOT EXISTS typecho DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci;
# utf8mb4_general_ci更快
CREATE DATABASE IF NOT EXISTS typecho DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;
CREATE DATABASE typecho 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

标签: mysql, sql, developers

添加新评论