1. 程式人生 > >Centos7二進位制安裝Mysql5.7及以上版本

Centos7二進位制安裝Mysql5.7及以上版本

1.建立mysql使用者組和使用者

groupadd mysql
useradd -r -g mysql -s /sbin/nolog mysql

2.下載mysql相關版本二進位制包,並解壓移動至/usr/local目錄

wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz 
tar xf mysql-5.7.24-linux-glibc2.12-x86_64.tar.gz #解壓到當前目錄
mv /usr/local/src/mysql-5.7.24-linux-glibc2.12
-x86_64/ /usr/local/mysql #移動並重命名

3.在Mysql目錄建立data目錄,並授予相關許可權

mkdir -p /usr/local/mysql/data
chown -R mysql.mysql .
chmod -R 755 /usr/local/mysql

4.解除安裝自帶mysql、mariadb資料

rpm -qa | grep mysql
rpm -qa | grep mariadb
rpm -e --nodeps 包名

5.初始化mysql資料庫,會生成一個隨機密碼,記得儲存好。警告提示關係不大,可以忽略

[[email protected]
mysql]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql/ --datadir=/usr/local/mysql/data/ 2018-11-14T09:05:32.752289Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2018-11
-14T09:05:34.248738Z 0 [Warning] InnoDB: New log files created, LSN=45790 2018-11-14T09:05:34.787689Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2018-11-14T09:05:34.941610Z 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: 7238b05b-e7ec-11e8-b680-3417ebcfddea. 2018-11-14T09:05:34.980838Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2018-11-14T09:05:34.981293Z 1 [Note] A temporary password is generated for [email protected]: Lk5jgsJ2nG#7

6.配置/etc/my.cnf檔案,至關重要。資料庫能不能正常啟動就看這個配置檔案了。如果配置不對,會給你搞一堆的錯誤出來....很是腦闊痛

[[email protected] mysql]# vim /etc/my.cnf
[[email protected] mysql]# cat /etc/my.cnf
[mysqld]
socket=/tmp/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/var/log/mysql/mysql.log
pid-file=/var/run/mysql/mysql.pid

#
# include all files from the config directory
#
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
default_storage_engine = InnoDB
user=mysql
tmpdir=/tmp
[[email protected] mysql]# 

7.啟動mysql資料庫

ln -vs /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld #建立軟連線
/etc/init.d/mysqld start

8.登陸資料庫

ln -vs /usr/local/mysql/bin/mysql /usr/bin/ #建立軟連結,也就是windows說的快捷方式
mysql -uroot -p密碼 #用初始化生產的隨機密碼登陸資料庫做相應的授權操作

附加

總結一下二進位制安裝Mysql5.7資料庫所遇到的問題

1.Cetos7作業系統不能解壓帶gz字尾的包,換了多少個引數都沒用。不知道是哪裡的問題,暫時未找到解決辦法,有遇到的網友可以留言告知下。錯誤如下所示

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now

2.前面也說到配置檔案的問題,這個真的是至關重要,就因為配置檔案裡面有個兩個datadir導致一直出現如下錯誤,

[[email protected] data]# /etc/init.d/mysqld start
Starting MySQL..... ERROR! The server quit without updating PID file (/var/lib/mysql/oracle.pid).

3.登陸資料庫之後出現的問題,如下所示

mysql> use mysql
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

這個問題這個主要是由一個引數控制的 default_password_lifetime,有興趣的朋友可以去看看官方的解釋

我是從一個部落格中看到這個的。資料來源

解決方法如下

alter user user() identified by "密碼";

4.mysql -uroot -p不能使用,這個是由於沒有建立軟連結導致;上文有說。

大概就這麼多,如有遺漏和不足請大家留言,我會及時更正文件!