美文网首页
Centos 7.2 安装并配置 mysql5.7

Centos 7.2 安装并配置 mysql5.7

作者: dimdark | 来源:发表于2018-02-24 19:43 被阅读0次

    添加mysql源

    CentOS 7.2 的yum源中默认没有mysql,故要先添加mysql的repo源

    wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
    rpm -ivh --nodeps --force mysql57-community-release-el7-8.noarch.rpm
    

    这样后在目录/etc/yum.repos.d下会有mysql源:

    mysql源

    安装mysql并启动mysql服务

    安装mysql:

    yum -y install mysql-server
    

    启动mysql:

    service mysqld start
    

    附:
    查看mysql服务状态的命令:
    service mysqld status or systemctl status mysqld
    开启mysql服务状态的命令:
    service mysqld start or systemctl start mysqld
    关闭mysql服务状态的命令:
    service mysqld stop or systemctl stop mysqld
    重启mysql服务状态的命令:
    service mysqld restart or systemctl restart mysqld

    登录mysql并修改密码

    MySQL5.7会在安装后为root用户生成一个随机密码, 查看该随机密码:

    grep 'temporary password' /var/log/mysqld.log
    
    root的随机密码

    利用该随机密码登录mysql, 然后修改root用户的密码:

    set password for 'root'@'localhost' = "new_password";
    flush privileges;
    exit
    

    注意: new_password 代表root用户的新密码的值, 需要你自己替换

    设置字符集(防止中文乱码)

    运行命令:

    mysqld --help --verbose | grep -A1 'Default options'
    
    mysql配置文件

    附:

    • grep中的-A1after one line 表示显现匹配行的下一行
    • mysqld默认按以下顺序加载配置文件:
      /etc/my.cnf /ect/mysql/my.cnf /usr/etc/my.cnf ~/.my.cnf

    编辑文件/ect/my.cnf:

    [client][mysql]下加入语句:default-character-set=utf8, 在[mysqld]下加入语句:
    init_connect='SET collation_connection = utf8_general_ci'
    init_connect='SET NAMES utf8'
    character-set-server=utf8
    collation-server=utf8_general_ci

    mysql配置字符集
    配置字符集的验证

    创建(删除)用户并授予(撤销)权限

    注意: 要确保自己有执行下列命令的权限

    创建用户的语句格式:

    create user '用户名'@'主机ip地址' identified by '密码';
    

    删除用户的语句格式:

    drop user '用户名'@'主机ip地址';
    

    授予用户权限的语句格式:

    grant 权限 on 数据库.数据表 to '用户名'@'主机ip地址' [ with grant option];
    

    附:
    []里面的内容是可选的, 即:with grant option 是可选参数, 例如, 当对A用户进行的授权时附带with grant option, 则A可以授予给其他用户(权限),当收回对A的授权时,A授予给其他用户的权限不会被级联收回
    撤销用户权限的语句格式:

    revoke 权限 on 数据库.数据表 from '用户名'@'主机ip地址';
    

    例子:

    -- 创建用户a和b, 其密码分别为a123456和b123456
    create user 'a'@'localhost' identified by 'a123456';
    create user 'b'@'localhost' identified by 'b123456';
    -- 授予用户a和b对mybatis_test.user表的insert, update, select, delete权限, 但用户b无法授予其他用户(权限)
    grant insert, update, delete, select on mybatis_test.user to 'a'@'localhost' with grant option;
    grant insert, update, delete, select on mybatis_test.user to 'b'@'localhost';
    -- 撤销用户a和b对mybatis_test.user表的delete权限
    revoke delete on mybatis_test.user from 'a'@'localhost';
    revoke delete on mybatis_test.user from 'b'@'localhost';
    

    附: 查看指定用户权限的命令:
    show grants for '用户名'@'主机ip地址';

    相关文章

      网友评论

          本文标题:Centos 7.2 安装并配置 mysql5.7

          本文链接:https://www.haomeiwen.com/subject/xbrcxftx.html