本文介绍mysql 5.7.37版本安装流程。
1、卸载系统上的mariadb
yum remove mysql mysql-devel mysql-server mysql-libs compat-mysql51
2、下载安装包并安装
wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar
3、解压并按顺序安装
tar -zxvf mysql-5.7.37-1.el7.x86_64.rpm-bundle.tar
cd mysql-5.7.37-1.el7.x86_64.rpm-bundle
rpm -ivh mysql-community-common-5.7.37-1.el7.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.37-1.el7.x86_64.rpm
rpm -ivh mysql-community-client-5.7.37-1.el7.x86_64.rpm
rpm -ivh mysql-community-server-5.7.37-1.el7.x86_64.rpm
# 5.7最新版本
wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
yum -y install mysql57-community-release-el7-11.noarch.rpm
# 80版本
sudo yum localinstall https://repo.mysql.com/mysql80-community-release-el7-1.noarch.rpm
按1或者2步骤安装完成后,可以直接启动mysql服务,但是一般需要手动指定一下数据目录等,将其指定在挂载磁盘较大的SSD盘等,方便运营维护。
修改mysql配置文件,设置数据文件目录以及应用相关配置
vi /etc/my.cnf
# 修改数据目录
datadir=/home/mysql/data
## 修改模式(不严格)
sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
1、初始化
mysqld --initialize
2、启动MySQL服务
systemctl start mysqld
systemctl status mysqld
3、查看初始密码
# sudo grep 'temporary password' /var/log/mysqld.log
A temporary password is generated for root@localhost: 6YsX6o4-S/7z
1、修改密码-8.0版本
mysql -uroot -p
set global validate_password.policy=0;
set global validate_password.length=6;
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
2、修改密码-5.7版本
mysql -uroot -p
set global validate_password_policy=LOW;
set global validate_password_length=6;
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
3、配置MySQL允许外部访问
use mysql;
select user,host from user;
update user set host = '%' where user ='root';
flush privileges;
4、创建用户
create user 'gogs'@'%' identified by '123456';
grant all privileges on gogs.* to 'gogs'@'%';
flush privileges;
5、修改默认最大连接
# 查看当前连接使用情况
show status like 'Threads%';
# 查看最大连接数
show variables like '%max_connections%';
# 方法一:临时修改数据库配置,重启后失效
show variables like '%max_connections%';
set GLOBAL max_connections=1000;
# 方法二:修改配置文件,修改完成后,需要重启mysql服务才能生效
vi my.cnf
max_connections=1000
6、获取具体线程查询SQL,通过此方式可以定位具体问题SQL
# 获取线程ID
top 获取mysql进程ID
top -Hp 进程ID 获取指定占用最高的线程ID
# 进入SQL命令行
SELECT * FROM performance_schema.threads where thread_os_id = [线程ID];
1、mysql holdoff time over
# 配置mysql主目录权限
chown mysql:mysql -R /home/mysql