阿里云CentOS7.3安装MySQL以及配置远程连接

CentOS7默认数据库是MariaDB, 但是我用的是MySQL。

1、查看当前是否有MySQL进程

rpm -qa|grep -i mysql

有的话则需要kill掉,再删除;没有直接到第2步

pkill -9 mysqld
yum -y remove mysql-community-client-5.6.38-2.el7.x86_64

卸载不掉的用rpm -ev mysql-community-client-5.6.38-2.el7.x86_64
依次卸载,直到没有

2、创建目录,下载MySQL5.7

mkdir /usr/local/mysql
cd /usr/local/mysql
wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm
rpm -ivh mysql57-community-release-el7-8.noarch.rpm
yum -y install mysql-server

根据步骤安装就可以了

默认配置文件路径:
配置文件:/etc/my.cnf
日志文件:/var/log/var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid

3、配置my.cnf

vim /etc/my.cnf
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
server_id = 1
expire_logs_days = 3

# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

注意:安装完成后,密码为随机密码,需要重置密码。

4、启动服务,修改密码

service mysqld restart
grep "password" /var/log/mysqld.log

可以在信息中查看到一串临时密码,使用临时密码登录,再修改密码

mysql -u root -p

进入后需要重置密码(5.7.20为了安全密码,必须包含数字字母符号)

// Root!!2018为参考密码
alter user 'root'@'localhost' identified by 'Root!!2018';
// 刷新
flush privileges;

5、开放远程连接

// 赋予root用户所有权限,远程登录密码是Root!!2018
grant all privileges on *.* to 'root' @'%' identified by 'Root!!2018';
// 刷新
flush privileges;

退出MySQL。

6、允许服务自启动

chkconfig --add mysql
chkconfig mysql on

7、开放防火墙端口
在CentOS 7或RHEL 7或Fedora中,防火墙由FirewalID来管理

通过systemctl status firewalld查看FirewalID状态,发现当前是dead状态,即防火墙未开启。
通过systemctl start firewalld开启防火墙,没有任何提示即开启成功。
执行firewall-cmd --permanent --zone=public --add-port=3306/tcp --permanent,提示success
(–permanent永久生效,没有此参数重启后失效)
执行firewall-cmd --reload,提示success

或者关闭防火墙:systemctl stop firewalld

最后一步:

阿里云CentOS7.3安装MySQL以及配置远程连接_第1张图片

你可能感兴趣的:(【数据库】MySQL,【Java基础】Linux)