• MySQL用户管理与字符集


    用户管理与字符集

    一、用户管理

    用户名的概念

    用户名是由两部分组成的

    user和host

    1.1 创建用户

    create user 'yysue'@'192.168.5.38' identified by 'yysueyysue';
    create user 'yysue'@'srv%.yysue.net' identified by 'yysueyysue';
    flush privileges;
    
    结合/etc/hosts使用,对域名授权
    srv1.yysue.net 192.168.5.11
    srv2.yysue.net 192.168.5.12
    
    saltstack,ansible
    

    查看系统账号

    select user, host, password from mysql.user;
    select user, host, authentication_string from mysql.user;
    

    查看账号的权限

    show grants for user@host
    

    数据库加固命令,只在初始化数据库时执行一次

    delete from mysql.user where user!='root' or host!='localhost';
    truncate table mysql.db;
    drop database test;
    

    root@localhost与root@127.0.0.1区别

    localhost只能通过unix_domain登录,socket
    
    127.0.0.1只能通过tcp方式登录 
    mysql -h 127.0.0.1 -uroot --protocol=tcp
    

    用户迁移时,密码怎么迁移

    -- 验证用户密码
    select password('yysueyysue');
    
    create user 'a'@'ip' identified by password '加密后的hash串';
    
    1.查看用户有什么权限 
    2.查一下hash串
    

    删除账号

    drop user aaa;
    drop user aa1@'ip';
    

    用什么工具连接mysql

    mysql命令

    SQLyog

    workbench

    查看当前用户

    select user();
    

    备一个mysql手册

    https://dev.mysql.com/doc/refman/5.6/en/

    chm手册

    help create user

    1.2 授权

    移除权限

    revoke select on db1.* from yysue@'ip';
    

    授权

    grant select on db1.* to 'yysue'@'ip';
    grant select on db1.t1 to 'yysue'@'ip';
    grant select,update,insert,delete on db1.t1 to 'yysue'@'ip';
    flush privileges;
    
    精确匹配原则
    

    查询权限

    show grants for root@ip;
    

    回收权限

    新建用户精确匹配,使用精确匹配的账户测试

    1.3 一个账号连接不上mysql

    连接的mysql是否运行,ip地址正确

    端口正确

    ss替代netstat命令,netstat统计的信息不准确

    是否能ping通服务器

    账号密码是否正确

    是否flush privileges

    1.4 应该创建哪些账号

    1)读写账号

    grant select,update,delete,insert on db1.* to yysue@ip;
    

    2)管理账号

    grant all privileges on *.* to root@localhost with grant option;
    

    3)复制用的账号

    grant relication slave on *.* to user1@ip identified by 'password';
    

    replication slave > replication client

    replication client可以认为用来监控复制的

    1.5 注意问题

    1)用户名的长度 小于16个字符

    2)密码 复杂,可见字符

    1.6 MySQL密码忘了

    重启数据库

    my.cnf [mysqld] 加 skip-grant-tables
    

    不重启数据库

    set password for user1@ip = password('password')
    help set password
    
    update mysql.user set password=password('new-password') 
    where user = '' and host = '';
    flush privileges;
    
    mysql.user存储引擎是MyISAM
    show create table mysql.user;
    复制出去改好密码再复制回来
    cp user.* ../user1/
    update user1.user set password=password('new-password') 
    where user='xxx' and host = 'xxx'
    cp user.* dst/mysql
    chown mysql:mysql 
    kill -l
    重新加载配置
    kill -HUP `pidof mysqld`
    

    keypass来帮助记密码

    二、字符集

    不涉及中文的话,感觉不出来

    一般在系统升级,数据迁移时容易出问题

    2.1 理解字符集

    1)什么是字符集

    gbk,utf8,gb2312,latin1

    一套符号编码规则

    -字符串都必须有相应的字符集

    二进制没有字符集

    流式传输

    校验字符集的概念,以字为单位进行比较

    没有检验字符集的概念之前,以字节为单位进行比较

    多字节,latin1是单字节,gbk是2字节,utf8是3字节

    归类的方法,就是3类

    以二进制形式存储,区分大小写, xxx_bin

    show charset;
    

    default collation都是以xxx_general_ci结尾

    ci是case insensitive的意思,不区分大小写,是指比较的时候不区分大小写

    还有一种是xxx_general_cs结尾

    cs是case sensitive的意思,区分大小写的

    2)好几层字符集

    操作系统字符集

    echo $LANG
    locale -a
    i18n
    /etc/sysconfig/i18n
    updatedb
    locate i18n
    

    数据库服务器和数据库MySQL字符集

    client 终端字符集
    database 数据库字符集
    server 服务器的字符集
    

    查看表字符集

    查看列字符集

    三码同一,服务器,终端,DB级别

    db server ->dbname->table->column

    物理属性的字符集

    连接字符集

    结果字符集

    3)修改字符集

    # 更改客户端字符集
    set names utf8;
    set global names utf8;
    

    查看

    show variables like '%char%';
    show global variables like '%char%';
    

    带global全局,不带global是session级别

    4)5.5及之前

    早期版本的MySQL默认字符集都是latin1

    配置文件配置字符集

    [mysqld]
    character-set-server = utf8
    

    5)常见字符集

    gbk/gb2312双字节字符集

    gb2312包含于gbk

    gbk是gb18030的子集

    gbk中日韩

    utf8三字节的字符集,万国码,可变长度

    ascii 8bit,实际占用7bit

    latin1是8bit

    2.2 不同字符集进行转换

    gbk->unicode->utf8

    utf8->unicode->gbk

    iconv命令

    file 命令

    latin1字符集环境:LANG=en_US.ISO_8859_1, CRT:default
    gbk字符集环境:LANG=en_US.GBK/zh_CN.GBK, CRT:default
    utf8字符集环境:LANG=en_US.UTF-8, CRT:utf-8
    
    mysqldump --where 1=1 limit 10000
    screen
    time mysql < xxx.sql && mutt xxx
    

    2.3 怎么避免乱码

    1)三码统一

    服务器端,终端的,DB级的,程序级的

    2)真的遇到乱码

    想想 我们做过什么

    环境发生了什么变换

    2.3 gbk&utf8

    使用gbk可以省空间,省网络流量

    输入为主的网站大多数用utf8,utf8mb4

    信息类,新闻站一般用gbk,gb2312

    nginx,apache一定要开压缩

    压测工具httpwatch

    http://wubx.net/mysql-running-change-owner/

    三、实验

    3.1 验证char能存储多少个字符

    drop table if exists t1;
    create table t1(name char(30)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    insert into t1(name) values (repeat('汉', 10));
    insert into t1(name) values (repeat('汉', 20));
    insert into t1(name) values (repeat('汉', 30));
    insert into t1(name) values (repeat('汉', 31));
    
  • 相关阅读:
    mac os programming
    Rejecting Good Engineers?
    Do Undergrads in MIT Struggle to Obtain Good Grades?
    Go to industry?
    LaTex Tricks
    Convert jupyter notebooks to python files
    How to get gradients with respect to the inputs in pytorch
    Uninstall cuda 9.1 and install cuda 8.0
    How to edit codes on the server which runs jupyter notebook using your pc's bwroser
    Leetcode No.94 Binary Tree Inorder Traversal二叉树中序遍历(c++实现)
  • 原文地址:https://www.cnblogs.com/okokabcd/p/8478412.html
Copyright © 2020-2023  润新知