Linux 安裝mysql心得

第一步:mysql安裝包準備

mysql官網下載地址://downloads.mysql.com/

第二步:將mysql安裝包上傳到服務器

第三步:解壓

tar -zxvf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz

第四步:cp或mv到/usr/local/目錄下,並命名為mysql

注意:這裡不建議使用拷貝(cp),因為文件大的話就會很慢

 mv mysql-5.7.30-linux-glibc2.12-x86_64 /usr/local/mysql

第五步:創建mysql用戶組和用戶

groupadd mysql
useradd -r -g mysql mysql

第六步:創建mysql配置存放目錄data

cd /usr/local/mysql
mkdir data

第七步:為mysql目錄修改用戶權限

chown -R mysql:mysql /usr/local/mysql/

第八步:將mysql初始化,並記錄初始密碼(Bo39?ed2h*W1)

cd msyql/bin
./mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data

會出現以下:

2021-01-30T03:51:20.337410Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-01-30T03:51:21.458335Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-01-30T03:51:21.659739Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-01-30T03:51:21.722351Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 6a99bae9-62ae-11eb-8966-00163e175cc4.
2021-01-30T03:51:21.724337Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-01-30T03:51:22.941639Z 0 [Warning] CA certificate ca.pem is self signed.
2021-01-30T03:51:22.977097Z 1 [Note] A temporary password is generated for root@localhost: Bo39?ed2h*W1

第九步:配置mysql(my.cnf)

vim /etc/my.cnf

[mysqld]
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
#skip-grant-tables
port = 3306
pid-file = /usr/local/mysql/data/mysql.pid
socket = /usr/local/mysql/data/mysql.sock
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
default_authentication_plugin = mysql_native_password

第十步:加入開機自啟

cd /usr/local/mysql
cp support-files/mysql.server /etc/init.d/mysql
chmod  +x /etc/init.d/mysql   //添加可執行權限
chkconfig --add mysql  // 添加開機自啟
chkconfig --list  //查看自啟是否添加成功

第十一步:配置環境變量

vim /etc/profile
export PATH=$PATH:/usr/local/mysql/bin:/usr/local/mysql/lib
source /etc/profile

第十二步:啟動mysql

systemctl restart mysql

第十三步:登錄mysql

mysql -u root -pBo39?ed2h*W1 //初始化密碼

會出現以下錯誤(說是要改一下/tmp/mysql.sock目錄):

vim /etc/my.cnf
socket = /tmp/mysql.sock
systemctl restart mysql //再重啟服務

再次登錄:(mysql -u root -pBo39?ed2h*W1)

修改密碼

alter user 'root'@'localhost' identified with mysql_native_password by '123456';
或 set password for root@localhost= password(『123456』);
flush privileges;