一、系统环境
系统:CentOS7
MySQL版本:MySQL 5.7
二、检查是否已安装
通过查看文件安装路径来判断是否安装mysql。
在终端输入:
whereis mysql
回车,如果你安装好了mysql,就会显示文件安装的地址,例如我的显示(安装地址可能会不同)
说明:/usr/lib64/mysql,是指:MySQL的安装路径。
如果你是用rpm安装, 检查一下RPM PACKAGE:
rpm -qa | grep -i mysql
检查MySQL service:
service mysql status
三、安装步骤
1、yum安装
1.1、更新yum
yum update -y
这里会更新好多内容,需要等待一会。
1.2、使用wget下载mysql yum源:
wget http://dev.mysql.com/get/mysql57-community-release-el7-7.noarch.rpm
如果提示wget命令未找到,则可使用命令,安装wget
sudo yum install -y wget
1.3、添加 mysql yum 源:
sudo yum localinstall -y mysql57-community-release-el7-7.noarch.rpm -y
1.4、安装 yum 工具 yum-utils :
sudo yum install -y yum-utils
1.5、查看可用的 mysql :
yum repolist enabled | grep "mysql.*-community.*"
1.6、查看所有的 mysql 版本
yum repolist all | grep mysql
1.7、使用指定版本MySQL
假如我想使用MySQL5.7,那么我就需要先关闭MySQL8.0
关闭MySQL8.0
sudo yum-config-manager --disable mysql80-community
开启MySQL5.7
sudo yum-config-manager --enable mysql57-community
1.8、查看当前启用的MySQL版本
yum repolist enabled | grep mysql
1.9、安装MySQL
sudo yum install -y mysql-community-server
期间等待很长时间,等待安装完成。,如果下载速度只有几十kb,请更换网络,或者使用手机热点进行下载。更换热点后,再次执行此命令即可。
有些同学会出现此问题
执行此代码块。
yum module disable mysql
sudo yum install -y mysql-community-server
2、离线安装
2.1、下载文件
https://dev.mysql.com/downloads/mysql/
解压
tar xvf mysql-5.7.38-1.el7.x86_64.rpm-bundle.tar
2.2、安装
rpm -ivh mysql-community-common-5.7.21-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.21-1.el7.x86_64.rpm
rpm -ivh mysql-community-devel-5.7.21-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-compat-5.7.21-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.21-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.21-1.el7.x86_64.rpm
3、配置、启动
3.1、启动MySQL
sudo service mysqld start
# 或
systemctl start mysqld
3.2、查看MySQL服务状态
sudo service mysqld status
# 或
systemctl status mysqld
3.3、初始化MySQL
查看初始化密码:
sudo grep 'temporary password' /var/log/mysqld.log
使用初始密码进行登录:
mysql -u root -p
初始化密码
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
3.4、设置MySQL密码策略
查询MySQL初始的密码策略
SHOW VARIABLES LIKE 'validate_password%';
3.4.1、修改密码验证强度
set global validate_password_policy=LOW;
3.4.2、修改密码长度
set global validate_password_length=6;
3.4.3、此时密码已经可以设置简单密码了
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
注意,密码长度最低为4位,设置的密码长度小于4时,默认改成4.
3.5、设置MySQL远程连接,在sql里面设置
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
这里的密码是你自己的设置的密码
3.5.1、刷新权限
FLUSH PRIVILEGES
3.6、设置 MySQL开机启动,在linux里面设置
不在sql里面配置,在Linux命令行执行。!!!不是在sql里面
systemctl enable mysqld
(这只是提示目录所在)MySQL的配置文件目录:
/etc/my.cnf
3.7、配置防火墙
设置3306端口开放
firewall-cmd --zone=public --add-port=3306/tcp --permanent
看到success表示添加成功
重载防火墙规则
firewall-cmd --reload
验证3306是否开放成功
firewall-cmd --zone=public --query-port=3306/tcp
3.8、测试链接
四、错误排查
1、连接报错
Starting MySQL.2021-07-27T06:13:48.399826Z mysqld_safe error: log-error set to '/var/log/mariadb/mariadb.log', however file don't exists. Create writable for user 'mysql'.
ERROR! The server quit without updating PID file (/usr/local/mysql/data/localhost.localdomain.pid).
解决办法:依次执行下面语句
mkdir /var/log/mariadb
touch /var/log/mariadb/mariadb.log
chown -R mysql.mysql /var/log/mariadb/
2、密码过期
java.sql.SQLException: Your password has expired. To log in you must change it using a client that supports expired passwords.
解决办法:依次执行下面语句
use mysql;
select host,user,password_expired from user;
update user set password_expired='N' where user='root';
flush privileges;
3、设置任意IP连接
use mysql;
select user,host from user;
update user set host='%' where host='localhost';
4、修改密码
use mysql;
update user set authentication_string=password("你的新密码") where user="root";
5、修改忽略大小写
查找配置文件路径
locate my.cnf
[mysqld]
lower_case_table_names=1
说明:
lower_case_table_names=1(不区分大小写)
lower_case_table_names=0(区分大小写)
6、修改字符集
[mysqld]
character-set-server=utf8mb4
[client]
default-character-set=utf8mb4
[mysql]
default-character-set=utf8mb4
请注意这几个参数配置的位置,不然可能会启动不起来mysql服务:
修改端口后无法启动,参考:centos7 修改mysql5.7默认端口后启动异常
7、groupBy有错误
使用group by
报错
[Err] 3065 - Expression #1 of ORDER BY clause is not in SELECT list, references column 'bwss_jinniu_dev.oa.create_time' which is not in SELECT list; this is incompatible with DISTINCT
7.1、原因
mysql5.7.34及以上版本将sql_mode的ONLY_FULL_GROUP_BY模式默认设置为打开状态,会导致一些错误:
- 1、我们使用GROUP BY查询时,出现在SELECT字段后面的只能是GROUP BY后面的分组字段,或使用聚合函数包裹着的字段,否则会报错如下信息:
Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'database.table.
column' which is not functionally dependent on columns in GROUP BY clause;
this is incompatible with sql_mode=only_full_group_by
- 2、当使用ORDER BY查询时,不能使用SELECT DISTINCT去重查询。否则会报错如下信息
Expression #1 of ORDER BY clause is not in SELECT list,
references column 'database.table.column' which is not in SELECT list;
this is incompatible with DISTINCT
查询验证:
select version(); //查询版本
select @@global.sql_mode //查询sql_mode
7.2、解决办法
解决方法:
去除ONLY_FULL_GROUP_BY
- 1、通过命令关闭:
set global sql_mode=‘STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION’;
但该方法在重启Mysql服务后会失效,重启服务后会失效
- 2、通过修改mysql的配置文件关闭ONLY_FULL_GROUP_BY SQL模式
sudo vim /etc/mysql/conf.d/mysql.cnf
文件底部追加:
[mysqld]
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
- 3、保存并重启mysql
sudo service mysql restart