官网下载压缩包
官网下载地址_点这里下载
解压
tar -zxf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
重命名
cd /usr/local/
mv mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz/ mysql
创建Data目录 以及mysql用户和用户组
mkdir data
groupadd mysql
useradd -r -s /sbin/nologin -g mysql mysql -d /usr/local/mysql/
更改mysql目录权限
chown -R mysql:mysql /usr/local/mysql/
初始化数据库
./bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/
mysql加入本地服务
cp -a ./support-files/mysql.server /etc/init.d/mysqld
编辑my.cnf配置文件
vim /etc/my.cnf
[mysqld]
bind-address=0.0.0.0 #绑定地址为0.0.0.0只是允许它接受远程连接的步骤的一部分
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/var/lib/mysql/mysql.sock
symbolic-links=0
user=mysql
port=3306
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
symbolic-links=0
max_connections=200
default-storage-engine=INNODB
lower_case_table_names=1
max_allowed_packet=32M
explicit_defaults_for_timestamp=true
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log #日志目录需要赋权限 chown -R mysql:mysql /var/lib/mariadb
pid-file=/var/run/mariadb/mariadb.pid #目录需要赋权限 chown -R mysql:mysql /var/run/mariadb
#注意:mysql连接localhost通常通过一个Unix域套接字文件进行,一般是/tmp/mysql.sock,这个socket路径不要修改,不然连本地mysql的时候回报错:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock'
[client]
port=3306
socket=/var/lib/mysql/mysql.sock
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
启动服务
service mysqld start
错误1
原因没有目录并且没权限
mkdir /var/log/mariadb
touch /var/log/mariadb/mariadb.log
chown -R mysql:mysql /var/log/mariadb/
错误2
发现并没有/var/log/mariadb/mariadb.log这个路径,那我们就创建, 并给mysql用户授权即可
mkdir /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
启动成功
查看初始密码登录mysql
cat /root/.mysql_secret
修改初始密码
SET PASSWORD FOR 'root'@localhost=PASSWORD('123456');
增加环境变量
vim /etc/profile
在最后面增加
export PATH=$PATH:/usr/local/mysql/bin
设置Mysql开机自启
chmod +x /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig --list
远程连接
mysql -u root -p
grant all privileges on *.* to root@"%" identified by "123456!@#$";
grant all privileges on *.* to root@"localhost" identified by "123456!@#$";
flush privileges;
service mysqld restart
开启3306接口
firewall-cmd --zone=public --add-port=3306/tcp --permanent //permanent永久生效,没有此参数重启后失效
CentOS 7上默认安装了firewalld建议关闭并禁用:
或关闭firewall 使用iptables
systemctl stop firewalld
systemctl mask firewalld
#允许本地回环接口(即运行本机访问本机)
iptables -A INPUT -i lo -j ACCEPT
# 允许已建立的或相关连的通行
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
#允许所有本机向外的访问
iptables -A OUTPUT -j ACCEPT
# 允许访问22端口
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#允许访问80端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#允许访问443端口
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
#允许FTP服务的21和20端口
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A INPUT -p tcp --dport 20 -j ACCEPT
#如果有其他端口的话,规则也类似,稍微修改上述语句就行
#允许ping
iptables -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
#禁止其他未允许的规则访问
iptables -A INPUT -j REJECT #(注意:如果22端口未加入允许规则,SSH链接会直接断开。)
iptables -A FORWARD -j REJECT