準備工作
下載安裝包:mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
選定目錄
安裝目錄:/usr/local,將安裝包上傳至/usr/local目錄下,透過ftp方式傳輸
具體步驟
cd /usr/local
解壓
tar -zxvf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
重新命名
mv mysql-5.7.38-linux-glibc2.12-x86_64 mysql
檢查系統是否自帶mariadb並解除安裝
rpm -qa | grep mariadb
rpm -e --nodes 'npm包名'
刪除/etc下的my.cnf(有就刪除),我這裡沒有,略過
建立使用者名稱和組
groupadd mysql
useradd -r -s /sbin/nologin -g mysql mysql -d /usr/local/mysql
給mysql檔案目錄更改許可權
cd /usr/local
chown -R mysql:mysql mysql/
在mysql資料夾下建立data目錄,並修改使用者組和使用者
cd /usr/local/mysql
mkdir data
chown -R mysql:mysql data
在/etc下建立my.conf檔案
vim /etc/my.conf
[client]
port = 3306
socket = /tmp/mysql.sock
[mysqld]
default-storage-engine=INNODB
max_allowed_packet=256M
innodb_log_file_size=256M
character_set_server=utf8mb4
innodb_default_row_format=DYNAMIC
innodb_large_prefix=ON
innodb_file_format=Barracuda
transaction-isolation=READ-COMMITTED
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
socket=/tmp/mysql.sock
log-error=/var/log/mysqld.log
pid-file=/usr/local/mysql/mysqld.pid
#不區分大小寫
lower_case_table_names = 1
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
max_connections=5000
修改配置檔案組和使用者
cd /etc
chown -R mysql:mysql my.conf
chmod 777 my.conf
進入mysql資料夾,安裝mysql
cd /usr/local/mysql
bin/mysql_install_db --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data
cp ./support-files/mysql.server /etc/init.d/mysqld
chmod a+x /etc/init.d/mysqld
注意:如果修改資料目錄需要修改/etc/init.d/mysqld
中的datadir欄位
配置環境變數
vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin
重新整理環境變數
source /etc/profile
啟動mysql
service mysqld start
service mysqld status
登入mysql並設定基礎環境
cat /root/.mysql_secret
mysql -u root -p
set PASSWORD=PASSWORD('123456');
flush privileges;
設定root遠端登入使用者
grant all on *.* to root@'%' identified by '123456';
flush privileges;
use mysql;
select user, host from user;
delete from user where user='root' and host='localhost';
flush privileges;
重啟mysql
service mysqld restart