CentOS中默认安装有MariaDB,安装mysql会覆盖掉MariaDB;
环境说明:
centos7.9 64位系统 内核 3.10.0-1160.el7.x86_64
安装步骤
下载mysql社区版本源并安装
# wget https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm # yum install -y mysql57-community-release-el7-10.noarch.rpm
安装之后 #yum repolist 会更新mysql repo源
yum安装mysql server
# yum install -y mysql-community-server
启动mysql服务
# systemctl start mysqld
查看版本
# mysql --version mysql Ver 14.14 Distrib 5.7.33, for Linux (x86_64) using EditLine wrapper
Mysql的使用和授权
查找mysql的登陆临时密码
# grep 'password' /var/log/mysqld.log 2021-03-04T00:58:48.469056Z 1 [Note] A temporary password is generated for root@localhost: qjl4u:*bucfO
使用临时密码登陆mysql,登陆后第一件事需要修改密码,必须符合密码复杂度 大小写字母数字加特殊字符组合;
# mysql -uroot -p mysql> alter user root@'localhost' identified by '8hbZzAe*';
测试1:
创建一个新用户,只能从本地访问mysql,只授权mysql数据库下的所有表的select权限;
mysql> grant select on mysql.* to yong@localhost identified by '8hbZzAe*';
使用新用户登陆并测试删除表,提示没有权限;
# mysql -uyong -h localhost -p mysql> use mysql; mysql> drop table user; ERROR 1142 (42000): DROP command denied to user 'yong'@'localhost' for table 'user'
测试2:
授权star用户限制只能从172.16.80.21 地址访问,拥有最高权限,可以访问所有库所有表;
mysql> grant all privileges on *.* to 'star'@'172.16.80.21' identified by '8hbZzAe*'; mysql> flush privileges; mysql> select user,host from mysql.user; +---------------+--------------+ | user | host | +---------------+--------------+ | star | 172.16.80.21 | | mysql.session | localhost | | mysql.sys | localhost | | root | localhost | | yong | localhost | +---------------+--------------+
使用新用户star本地登陆,提示访问拒绝,使用密码正确✅;
# mysql -ustar -p Enter password: ERROR 1045 (28000): Access denied for user 'star'@'localhost' (using password: YES)
登陆172.16.80.21 服务器指定 -h mysql服务器IP访问成功(只安装mysql client客户端即可)
# wget https://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm # yum install -y mysql57-community-release-el7-10.noarch.rpm # yum install -y mysql-community-client # mysql -ustar -p -h 172.16.80.22
密码策略
在命令行模式使用-e 参数,查看密码执行策略:
# mysql -uroot -p -e "show variables like 'validate_password%';"
修改密码策略级别:
# mysql -uroot -p -e "set global validate_password_policy=LOW;"
修改密码策略长度:
# mysql -uroot -p -e "set global validate_password_length=6;"
更改mysql的字符集
status 可以查看当前状态,如登陆用户,当前数据库、数据库版本信息、字符编码集等;
mysql> status;
编辑mysql配置文件,添加以下代码,设置字符集为utf8
vi /etc/my.cnf [client] default-character-set=utf8 [mysqld] character-set-server=utf8 collation-server=utf8_general_ci
重启mysql服务生效
# systemctl restart mysqld
再次登录数据库查看status状态,已更改为utf8
# mysql -uroot -p