MySQL数据库配置

2018-03-30  本文已影响10人  轻云之闭月

DBA 命令

创建用户

CREATE USER 'username'@'host' IDENTIFIED BY 'password';

CREATE USER 'dog'@'localhost' IDENTIFIED BY '123456';
CREATE USER 'pig'@'192.168.1.101' IDENDIFIED BY '123456';
CREATE USER 'pig'@'%' IDENTIFIED BY '123456';

授权

GRANT privileges ON databasename.tablename TO 'username'@'host'

GRANT SELECT, INSERT ON test.user TO 'pig'@'%';
GRANT ALL ON *.* TO 'pig'@'%';

注意:
用以上命令授权的用户不能给其它用户授权,如果想让该用户可以授权,用以下命令:
GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION;

设置与更改用户密码

SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword');
如果是当前登陆用户用:
SET PASSWORD = PASSWORD("newpassword");
例:
SET PASSWORD FOR 'pig'@'%' = PASSWORD("123456");

撤销用户权限

REVOKE privilege ON databasename.tablename FROM 'username'@'host';

例:
REVOKE SELECT ON *.* FROM 'pig'@'%';

删除用户

DROP USER 'username'@'host';


修改数据库编码

查看默认编码:


需要改为utf8,避免中文乱码

两种方式修改编码:

[client]
default-character-set = utf8

[mysqld]
default-storage-engine = INNODB
character-set-server = utf8
collation-server = utf8_general_ci

重启服务,检测编码:



注:如果MySQL的版本≥5.5.3,可以把编码设置为utf8mb4,utf8mb4和utf8完全兼容,但它支持最新的Unicode标准,可以显示emoji字符。

  1. /etc/mysql/mysql.conf.d/mysqld.cnf
    lc-messages-dir= /usr/share/mysql 语句后(下面)添加 character-set-server=utf8

  2. /etc/mysql/conf.d/mysql.cnf
    最后添加 default-character-set=utf8

  3. 重启服务

注意:修改编码要在创建数据库之前进行,不然修改之前创建的数据库编码并不会被修改。依旧采用默认编码

上一篇下一篇

猜你喜欢

热点阅读