MySQL 下載地址:https://dev.mysql.com/downloads/mysql/
並按如下方式選擇來下載安裝包。

1. 設置配置文件/etc/my.cnf
[client]
port = 3306
socket = /usr/local/mysql/socketfile/mysql.sock
default-character-set=utf8
[mysqld]
user=mysql
port = 3306
server_id = 1
socket=/usr/local/mysql/socketfile/mysql.sock
basedir =/usr/local/mysql
datadir =/usr/local/mysql/data
pid-file=/usr/local/mysql/data/mysqld.pid
log-error=/usr/local/mysql/log/mysql-error.log
character_set_server=utf8
max_connections=1000
2.新建用戶和用戶組
groupadd mysql
useradd -g mysql mysql
3.解壓mysql二進制文件到目錄/usr/local目錄下,並重命名為mysql
cd /usr/local
tar -xf /root/mysql-5.7.18-linux-glibc2.5-x86_64.tar.gz
mv mysql-5.7.18-linux-glibc2.5-x86_64 mysql
chown -R mysql:mysql mysql
cd /usr/local/mysql
mkdir log
chown -R mysql:mysql log
chmod 755 log
cd /usr/local/mysql/log
touch mysql-error.log
chmod 755 mysql-error.log
chown mysql:mysql mysql-error.log
mkdir -p /usr/local/mysql/socketfile
chown -R mysql:mysql /usr/local/mysql/socketfile
4.初始化數據庫
cd /usr/local/mysql
bin/mysqld --initialize --user=mysql
初始化數據庫后的密碼存放在mysql-error.log文件中
[mysql@SaltStack-Minion log]$ more mysql-error.log 2017-09-25T01:02:36.182707Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentat ion for more details). 2017-09-25T01:02:42.076463Z 0 [Warning] InnoDB: New log files created, LSN=45790 2017-09-25T01:02:42.464764Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2017-09-25T01:02:42.916407Z 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 U UID: 3c1e3858-a18d-11e7-b261-000c299cd645. 2017-09-25T01:02:43.104917Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2017-09-25T01:02:43.124083Z 1 [Note] A temporary password is generated for root@localhost: O4Cfoig_gXui 2017-09-25T01:02:53.184964Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentat ion for more details).
5.設置環境變量(root用戶和mysql用戶都添加)
在~/.bash_profile文件中添加mysql的安裝目錄,添加后的結果如下:
[root@CentOS6 mysql]# vi ~/.bash_profile # .bash_profile # Get the aliases and functions if [ -f ~/.bashrc ]; then . ~/.bashrc fi # User specific environment and startup programs PATH=$PATH:$HOME/bin:/usr/local/mysql/bin export PATH
#使修改生效
source ~/.bash_profile
6.設置service服務自啟動
cd /usr/local/mysql cp support-files/mysql.server /etc/init.d/mysql.server service mysql.server start service mysql.server restart service mysql.server stop
7.修改MySQL的root密碼
set password for root@localhost = password('mysql'); flush privileges;
8.當啟動MySQL數據庫報錯時,嘗試采用如下方式解決:
報錯信息:
Starting MySQL.. ERROR! The server quit without updating PID file
解決方法:
(1)首先檢查mysql日志文件中報錯信息(一般mysql日志文件會寫在/etc/my.cnf這個文件中);
(2)找到報錯信息之后,然后逐步確診原因即可;
(3)上述問題,我是通過刪除mysql.sock文件(在/etc/my.cnf文件中會記錄該文件的所在目錄),然后再次數據庫,問題解決;
