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
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/
cp -a ./support-files/mysql.server /etc/init.d/mysqld
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
原因沒有目錄並且沒許可權
mkdir /var/log/mariadb
touch /var/log/mariadb/mariadb.log
chown -R mysql:mysql /var/log/mariadb/
發現並沒有/var/log/mariadb/mariadb.log這個路徑,那我們就建立, 並給mysql使用者授權即可
mkdir /var/lib/mysql
chown -R mysql:mysql /var/lib/mysql
啟動成功
cat /root/.mysql_secret
修改初始密碼
SET PASSWORD FOR 'root'@localhost=PASSWORD('123456');
vim /etc/profile
在最後面增加
export PATH=$PATH:/usr/local/mysql/bin
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