1.下载mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz 上传到Linux服务器 /opt 目录下。
2.安装依赖
yum install -y cmake make gcc gcc-c++ libaio ncurses ncurses-devel
3.解压文件
tar -zxvf mysql-5.7.27-linux-glibc2.12-x86_64.tar.gz
4.将解压文件复制到/usr/local/mysql 目录
cp -r mysql-5.7.27-linux-glibc2.12-x86_64 /usr/local/mysql
5.添加系统mysql组和mysql用户
创建组:
groupadd mysql
创建用户:
useradd -r -g mysql mysql
(添加完成后可用id mysql
查看)
6.切到mysql目录
cd /usr/local/mysql
修改当前目录拥有者为mysql用户
chown -R mysql:mysql ./
7.安装数据库
bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data
8.如果以下错误
bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解决方法:
yum install -y libaio
//安装后在初始化就OK了。
9.执行以下命令创建RSA private key
bin/mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data
10.修改文件权限
修改当前目录拥有者为mysql用户
chown -R mysql:mysql ./
修改当前data目录拥有者为mysql用户
chown -R mysql:mysql data
11.配置my.cnf(一定要注意空格换行等特殊字符,会导致my.cnf 无法加载)
vim /etc/my.cnf
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
# 不区分大小写
lower_case_table_names = 1
# 不开启sql严格模式
sql_mode = "STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/usr/local/mysql/data/mysqld.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
12.添加开机项
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
vim /etc/init.d/mysqld
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
13.启动mysql
service mysqld start
加入开机启动
chkconfig --add mysqld
14.登录密码修改及远程连接
登录mysql
mysql -uroot -p
输入安装数据库的初始密码,如果忘记输入下面命令查询
cat /root/.mysql_secret
修改密码
## 修改密码
alter user 'root'@'localhost' identified by 'root';
## 刷新权限
FLUSH PRIVILEGES;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'some_pass' WITH GRANT OPTION;
## 开启远程登录
use mysql;
update user set host = '%' where user = 'root';
FLUSH PRIVILEGES;
15.如果还是无法远程访问
关闭防火墙(CentOS 7.0默认使用的是firewall作为防火墙)
systemctl stop firewalld.service #停止firewall
systemctl disable firewalld.service #禁止firewall开机启动