下载地址

https://dev.mysql.com/downloads/mysql/5.6.html#downloads

下载方式使用wget下载

wget https://dev.mysql.com//Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz

如没有wget,使用yum安装命令 yum install wget

解压缩mysql安装包

tar -zxvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz

卸载maridb

rpm -qa | grep mariadb #查看是否安装了mariadb
yum -y remove mari* #卸载关于mariadb的相关依赖
rm -rf /var/lib/mysql/*

创建文件夹

mkdir /usr/local/mysql/

将解压后的安装包移动到新建的文件夹下面

mv mysql-5.7.26-linux-glibc2.12-x86_64 /usr/local/mysql/

创建mysql用户组和mysql用户

groupadd mysql
useradd -r -g mysql mysql

初始化mysql

查询是否安装libaio依赖包

yum search libaio

如果没安装,可以用下面命令安装

yum install libaio

给mysql数据库文件与用户组mysql进行管理

chown -R mysql:mysql  /usr/local/mysql
chown -R mysql  /usr/local/mysql/

进入到mysql bin目录下面

./mysqld --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --initialize

初始化过程中需要注意的是[Note] A temporary password is generated for root@localhost: o*s#gqh)F4Ck 最后显示安装之后的临时密码

修改配置文件来启动

vim /etc/my.cnf

修改配置为

[client]
default-character-set=utf8mb4
#mysqlde utf8字符集默认为3位的,不支持emoji表情及部分不常见的汉字,故推荐使用utf8mb4

[mysql]
default-character-set=utf8mb4
[mysqld]
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data/
port=3306
socket=/tmp/mysql.sock
character_set_server=utf8mb4
pid-file=/usr/local/mysql/data/mysql.pid
log_error=/usr/local/mysql/log/mysql-error.log #错误日志路径
slow_query_log_file=/usr/local/mysql/log/mysql-slow.log
lower_case_table_names=1 #其中0:区分大小写,1:不区分大小写
# 设置log_bin记录数据可用于恢复数据
server-id=77 # 可设置为ip的最后数字,用户主从复制
log_bin = /usr/local/mysql/data/mysql-bin
binlog_format = mixed
expire_logs_days = 30 #超过30天的binlog删除
max_binlog_size = 20m #每个日志文件大小20m
# 开启mysql 日志开启后操作的日志会保存到general_log设置的目录下面
general_log=ON
general_log_file=/usr/local/mysql/log/mysql.log
sql_mode= STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

创建目录以及日志文件

mkdir /usr/local/mysql/log
vim /usr/local/mysql/log/mysql-error.log

启动mysql服务

sh /usr/local/mysql/support-files/mysql.server start

设置密码

先进入到/usr/local/mysql/bin目录

./mysql -u root -p
set password for 'root'@'localhost'=PASSWORD('leapsoulcn');

如果没有"%"这个host值,就执行下面这两句:

update user set host='%' where user='root';
flush privileges;

授权用户
任意主机以用户root和密码mypwd连接到mysql服务器

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'mypwd' WITH GRANT OPTION;
flush privileges;

添加开机启动

复制文件

cp support-files/mysql.server /etc/init.d/mysql

赋予可执行权限

chmod +x /etc/init.d/mysql

添加服务

chkconfig --add mysql 

显示服务列表

chkconfig --list