1.查看当前服务器是否安装了mysql
rpm -qa | grep -i mysql
2.如果安装过请先卸载
rpm -e mysql-libs-5.1.71-1.el6.x86_64
如果出现以下情况可以这样卸载
用这个命令卸载:
rpm -e --nodeps mysql-libs-5.1.71-1.el6.x86_64
3.下载安装包
wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
4.解压
tar -zxvf mysql-5.7.26-linux-glibc2.12-x86_64.tar.gz
5.添加mysql用户,并禁止登陆
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
6.创建需要的文件夹
mkdir /usr/local/mysql
//存放数据文件
mkdir /usr/local/mysql/data
//存放日志文件
mkdir /usr/local/mysql/logs
//存放pid文件
mkdir /usr/local/mysql/pid
//存放socket文件
mkdir /usr/local/mysql/sock
//存放binlog文件,用来做主从复制
mkdir /usr/local/mysql/binlog
7.移动文件
mv mysql-5.7.26-linux-glibc2.12-x86_64/* /usr/local/mysql
8.修改文件夹所属用户为mysql
chown -R mysql:mysql /usr/local/mysql
9.在 / e t c \color{red}{/etc} /etc目录先创建配置文件 m y . c n f \color{red}{my.cnf} my.cnf,内容如下
[mysqld]
bind-address=0.0.0.0
user=mysql
port=3306
basedir=/usr/local/mysql/
datadir=/usr/local/mysql/data
socket=/usr/local/mysql/sock/mysql.sock
log-error=/usr/local/mysql/logs/mysql.err
log-bin=/usr/local/mysql/binlog
pid-file=/usr/local/mysql/pid/mysql.pid
#开启binlog
log-bin=mysql-bin
#二进制日志格式 基于行的日志格式
binlog_format=row
binlog_row_image=minimal
#设置server-id 集群内必须唯一,并且主从配置不能相同
server-id=1
character-set-server=utf8mb4
10.初始化mysql
/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf --initialize
11.添加开机启动
cp support-files/mysql.server /etc/init.d/mysql
12.启动服务
service mysql start
13.初始化数据库(修改root密码)
密码在 / u s r / l o c a l / m y s q l / l o g s / m y s q l . e r r \color{red}{/usr/local/mysql/logs/mysql.err} /usr/local/mysql/logs/mysql.err文件里
/usr/local/mysql/bin/mysql -uroot -p password
alter user 'root'@'localhost' identified by '密码';
flush privileges;
14.添加一个用户可以远程登录
create user '用户名@%' identified by '密码';
grant all privileges on *.* to '用户名'@'%' identified by '密码';
flush privileges;