Centos7安装Mysql5.7
1、查看linux操作系统版本和系统内核版本
# 查看操作系统版本
cat /etc/redhat-release
# 查看系统内核版本
uname -r
2、下载mysql5.7的rpm安装包
# linux下载
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar
也可以细化下载,下载须要的mysql组件,有4个:分别是 server、client、common、libs
3、卸载旧版Mysql或Mariadb
一般centos7默认安装了mariadb
--nodeps rpm在安装/卸载时,不检查依赖关系,例如安装/卸载B,B依赖C导致无法安装/卸载,使用--nodeps就可以安装/卸载成功
--force 强制安装/卸载
rpm -qa | grep mariadb
rpm -qa|grep mysq
# 使用命令rpm -e --nodeps {-file-name}进行移除操作
rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
4、安装mysql5.7所需要的依赖
yum -y install libaio
yum -y install perl
yum -y install net-tools
yum install php-devel php-pear httpd-devel
5、上传Mysql安装包解压并安装
5.1、解压
tar -xvf mysql-5.7.27-1.el7.x86_64.rpm-bundle.tar
# 解压完成后,重命名为mysql
cd mysql
5.2、安装
使用命令rpm -ivh {-file-name}进行安装操作。
按照依赖关系依次安装rpm包 依赖关系依次为common→libs→client→server
rpm -ivh mysql-community-common-5.7.27-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.27-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.27-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.27-1.el7.x86_64.rpm
注:ivh中, i-install安装;v-verbose进度条;h-hash哈希校验
6、查看mysql5.7的状态,没有启动则把mysql启动
# 查看mysql服务启动状态
service mysqld status
或
systemctl status mysqld.service
出现上面这个death就是没有启动,新装的第一次是没有启动的。
# 启动mysql服务 active(running)代表启动成功
service mysqld start
或
systemctl start mysqld.service
# 查看mysql进程
ps -ef|grep mysql
# 查看3306端口
netstat -anop|grep 3306
# 关闭mysql服务
service mysqld stop
或
systemctl stop mysqld.service
7、登陆mysql修改root密码
7.1、查看临时密码
grep password /var/log/mysqld.log
7.2、用临时密码登录mysql
[root@hadoop102 ~]# mysql -uroot –p
# 修改成新的密码
mysql> set password = password("123456");
或
mysql> set password for root@localhost=password('123456');
在5.6后,mysql内置密码增强机制,低强度密码会报错报错
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
解决方法:
1、更改密码策略
mysql> set global validate_password_policy=0;
Policy | Tests Performe(要求) |
---|---|
0 or LOW | Length |
1 or MEDIUM | numeric, lowercase/uppercase, and special characters |
2 or STRONG | Length; numeric, lowercase/uppercase, and special characters |
默认是1,即MEDIUM,所以刚开始设置的密码必须符合长度,且必须含有数字,小写或大写字母,特殊字符。
2、修改有效密码长度
mysql> set global validate_password_length=6;
不管设置 validate_password_length=1,还是2,3,4 ,‘有效密码长度’这个参数的实际值都是4。超过4后设置是多少实际就是多少。
7.3、配置my.cnf
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
[mysqld]
collation-server = utf8mb4_unicode_ci
init-connect='SET NAMES utf8mb4'
character-set-server = utf8mb4
explicit_defaults_for_timestamp=1
7.4、开启远程连接,允许远程连接数据库
远程连接需要关闭防火墙
systemctl stop firewalld
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> select user,host from user;
+---------------+-----------+
| user | host |
+---------------+-----------+
| root | % |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+---------------+-----------+
4 rows in set (0.00 sec)
mysql> show grants;
+---------------------------------------------------------------------+
| Grants for root@localhost |
+---------------------------------------------------------------------+
| GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION |
| GRANT PROXY ON ''@'' TO 'root'@'localhost' WITH GRANT OPTION |
+---------------------------------------------------------------------+
2 rows in set (0.00 sec)
授予root用户远程访问权限
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
#刷新生效
mysql> flush privileges;
mysql中可以给一个用户授予如select,insert,update,delete等其中的一个或者多个权限,主要使用grant命令,用法格式为:
grant 权限 on 数据库对象 to 用户
一、grant 普通数据用户,查询、插入、更新、删除 数据库中所有表数据的权利。
grant select on testdb.* to common_user@’%’
grant insert on testdb.* to common_user@’%’
grant update on testdb.* to common_user@’%’
grant delete on testdb.* to common_user@’%’
或者,用一条 MySQL 命令来替代:
grant select, insert, update, delete on testdb.* to common_user@’%’
问题
[root@hadoop102 ~]# rpm -ivh mysql-community-libs-5.7.22-1.el7.x86_64.rpm
warning: mysql-community-libs-5.7.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
mysql-community-common(x86-64) >= 5.7.9 is needed by mysql-community-libs-5.7.22-1.el7.x86_64
mariadb-libs is obsoleted by mysql-community-libs-5.7.22-1.el7.x86_64
解决:清除yum里所有mysql依赖包
[root@hadoop102 ~]# rpm -qa|grep mysql
[root@hadoop102 ~]# yum remove mysql-libs
注意:
有的系统可能不太一样,没有mysql-libs,而是mariadb-libs,此时要移除的则是mariadb-libs
[root@hadoop102 ~]# rpm -qa|grep mariadb
[root@hadoop102 ~]# yum remove mariadb-libs
[root@hadoop102 ~]# mysql -uroot –p
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
# 解决方式:
# 1 跳过MySQL的密码认证过程
vim /etc/my.cnf
#在 [mysqld]后面任意一行添加“skip-grant-tables”用来跳过密码验证的过程
# 2 重启mysql服务
# 3 登录
[root@hadoop102 ~]# mysql
# 4 修改密码
# 5 将添加的内容删除,后重启mysql服务
修改MySQL数据库配置文件无密码登录后,修改密码报错:
mysql> set password for root@localhost=password('123456');
ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
1.先执行:
flush privileges;
2.再执行修改密码命令,可以了:
set password for root@localhost=password(‘你的密码’);
设置密码策略最低级别
mysql> set global validate_password_policy=0;
ERROR 1193 (HY000): Unknown system variable 'validate_password_policy'
设置密码有效长度 4 位及以上
mysql> set global validate_password_length=4;
ERROR 1193 (HY000): Unknown system variable 'validate_password_length'
#解决方式:
修改my.cnf,重新启动mysql服务器以使新设置生效。
my.cnf添加文件如下:
plugin-load-add=validate_password.so
validate-password=FORCE_PLUS_PERMANENT