参考文件:https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html
官网地址:https://dev.mysql.com/downloads/mysql/
基于Centos7.6
选择对应版本进行安装(mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz)
确保机器没有安装过MySQL或者已经删除干净,如果存在则删除
查询命令:rpm -qa | grep mysql
groupadd mysql
useradd -r -g mysql -s /bin/false mysql
检查:id mysql
rpm -qa|grep libaio
yum -y install libaio
tar -zxvf mysql-5.7.35-linux-glibc2.12-x86_64.tar.gz
mv mysql-5.7.35-linux-glibc2.12-x86_64 mysql
mkdir data
chown -R mysql:mysql /usr/local/mysql
chmod -R 755 /usr/local/mysql
vim /etc/my.cnf
[mysqld]
datadir=/usr/local/mysql/data
basedir=/usr/local/mysql
socket=/tmp/mysql.sock
port=3306
# 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=/usr/local/mysql/data/mysql.log
pid-file=/usr/local/mysql/data/mysql.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
cd mysql/bin/
./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data/ --basedir=/usr/local/mysql/
执行完毕会生成临时密码,需要备份下来(重要)
/usr/local/mysql/support-files/mysql.server start
进行完第三步就可以直接使用service mysql start或者service mysqld start来进行服务的开启
[root@localhost bin]# ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
[root@localhost bin]# ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
[root@localhost bin]# service mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
将MySQL加入到服务中
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql.server
vim /etc/profile
在文件底下添加以下两段
export MYSQL_HOME=/usr/local/mysql
export PATH=$MYSQL_HOME/bin:$PATH
保存退出并重新加载配置文件
source /etc/profile
实现在任意地方使用 mysql -u root -p 登录
3.4 设置开机启动
chkconfig mysql on
3.5 登陆前配置
使用 mysql -u root -p 进行数据库登录
输入 2.6 记录下来的临时密码
登录成功!
会提示修改密码,执行以下命令即可
SET PASSWORD = PASSWORD('123456'); //密码自定义
ALTER USER 'root'@'localhost' PASSWORD EXPIRE NEVER; //设置密码用不过期
FLUSH PRIVILEGES; //重载配置
3.6 远程连接数据库
这一步之前,就可以在机器上通过mysql -u root -p的方式进行登录,如果需要远程连接数据库,则需要进行如下操作:
登录:
mysql:mysql -u root -p
切换数据库:
use mysql;
执行:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
刷新权限表:
FLUSH PRIVILEGES;
3.7 防火墙
配置完成之后仍然无法远程连接,有可能是防火墙没有开放的原因,
如果是开发环境或者虚拟机,可以直接将防火墙关闭:
# systemctl stop firewalld.service
如果是预生产或者生产环境,建议修改端口号并且只打开这一个端口
查看防火墙状态:
# systemctl status firewalld
显示active就是防火墙已经开启
如果不是显示active状态,需要打开防火墙:
# systemctl start firewalld
查看所有已开放的临时端口(默认为空):
# firewall-cmd --list-ports
# 查看所有永久开放的端口(默认为空)
# firewall-cmd --list-ports --permanent
# 添加临时开放端口(例如:223端口)
# firewall-cmd --add-port=223/tcp
# 添加永久开放的端口(例如:223端口)
# firewall-cmd --add-port=223/tcp --permanent
# 关闭临时端口
# firewall-cmd --remove-port=80/tcp
# 关闭永久端口
# firewll-cmd --remove-port=80/tcp --permanent
# 配置结束后需要输入重载命令并重启防火墙以生效配置
# firewall-cmd --reload
# systemctl restart firewalld