Linux安裝Mysql5.6

趙帥強發表於2018-12-17

由於安裝的mysql8.0和其他伺服器的資料庫(版本5.1.30)由於版本差異過大,無法通訊,因此需要安裝一箇中間版本5.6,但是它的安裝過程和mysql8.0安裝略有不同。

解壓檔案

// 解壓檔案生成兩個xz格式的壓縮檔案
$ tar -xzvf mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz
// 為了方便查詢,改個名字
mv mysql-5.6.42-linux-glibc2.12-x86_64 mysql5
// 為了使用mysql快速初始化,連結到指定目錄
ln -s /home/work/lnmp/mysql5/ /usr/local/mysql

環境配置

我們需要專門的mysql程式啟動使用者和許可權管理:

// 建立mysql系統使用者和使用者組
useradd -r mysql
// 給予安裝目錄mysql許可權
chown mysql:mysql -R mysql5

配置自己的mysql配置檔案,因為我有多個Mysql庫,我手動指定很多引數:

[client]
socket=/home/work/lnmp/mysql5/tmp/mysql.sock
default-character-set=utf8

[mysql]
basedir=/home/work/lnmp/mysql5/
datadir=/home/work/lnmp/mysql5/data/
socket=/home/work/lnmp/mysql5/tmp/mysql.sock
port=3306
user=mysql
# 指定日誌時間為系統時間
log_timestamps=SYSTEM
log-error=/home/work/lnmp/mysql5/log/mysql.err

[mysqld]
basedir=/home/work/lnmp/mysql5/
datadir=/home/work/lnmp/mysql5/data/
socket=/home/work/lnmp/mysql5/tmp/mysql.sock
port=3306
user=mysql
log_timestamps=SYSTEM
collation-server = utf8_unicode_ci
character-set-server = utf8

[mysqld_safe]
log-error=/home/work/lnmp/mysql5/log/mysqld_safe.err
pid-file=/home/work/lnmp/mysql5/tmp/mysqld.pid
socket=/home/work/lnmp/mysql5/tmp/mysql.sock

[mysql.server]
basedir=/home/work/lnmp/mysql5
socket=/home/work/lnmp/mysql5/tmp/mysql.sock

[mysqladmin]                                                                                                                                  
socket=/home/work/lnmp/mysql5/tmp/mysql.sock 

這個裡面我指定了錯誤日誌的路徑,在接下來的操作中,如果出現錯誤,除了檢視終端顯示的錯誤,還要記得去錯誤日誌裡檢視詳細的資訊。

因為我指定了一些檔案,所以需要提前建立:

mkdir log
touch log/mysql.err
touch log/mysqld_safe.err
mkdir tmp
mkdir data
cd .. & chown mysql:mysql -R mysql5

資料庫初始化

如果我們不初始化,直接使用bin/mysqld_safe啟動會報錯,因為我們需要初始化mysql環境,具體的操作可以參考官方文件

$ scripts/mysql_install_db --user=mysql
...
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

  ./bin/mysqladmin -u root password `new-password`
  ./bin/mysqladmin -u root -h szwg-cdn-ai-predict00.szwg01.baidu.com password `new-password`

Alternatively you can run:

  ./bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:

  cd . ; ./bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl

  cd mysql-test ; perl mysql-test-run.pl

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

  http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

WARNING: Found existing config file ./my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as ./my-new.cnf,
please compare it with your file and take the changes you need.

提示中提示我們已經建立了root的使用者,需要修改臨時密碼,同時初始化成功。也告訴我們怎麼啟動一個資料庫例項。

啟動資料庫

我們使用mysqld_safe 命令來啟動:

$ bin/mysqld_safe
181217 14:55:08 mysqld_safe Logging to `/home/work/lnmp/mysql5/log/mysqld_safe.err`.
181217 14:55:08 mysqld_safe Starting mysqld daemon with databases from /home/work/lnmp/mysql5/data

連結全域性命令

此時,我們呼叫mysql只能用路徑/home/work/lnmp/mysql8/bin/mysql或相對路徑,需要連結為全域性命令:

$ ln -s /home/work/lnmp/mysql8/bin/mysql /usr/bin/
$ ln -s /home/work/lnmp/mysql8/bin/mysql_safe /usr/bin/

開啟資料庫

資料庫程式已經啟動,我們可以在新終端正常使用mysql資料庫,但是直接使用mysql命令報錯:

$ mysql -uroot
ERROR 2002 (HY000): Can`t connect to local MySQL server through socket `/tmp/mysql.sock` (2)

我檢視了官方安裝多個資料庫的文件,嘗試了很多方法,依然沒有辦法指定mysql命令的預設socket路徑(/tmp/mysql.sock)。但是根據mysql.sock的作用的說明,我們指定mysql.sock路徑即可:

bin/mysql -S /home/work/lnmp/mysql8/tmp/mysql.sock -h localhost -uroot -p
Enter password: 

或者:

ln -s /home/work/lnmp/mysql8/tmp/mysql.sock /tmp/

然後我們再呼叫mysql命令就不會報錯了。

修改初始密碼

初始化的時候,命令列文字已經提示我們需要怎樣更新root密碼,並根據他的指示操作即可,要詳細閱讀輸出的文字:

$ bin/mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MySQL to secure it, we`ll need the current
password for the root user.  If you`ve just installed MySQL, and
you haven`t set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): 
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.

Set root password? [Y/n] y
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!

連線資料庫,新密碼已經更新。

參考文章

  1. mysql8.0安裝:https://segmentfault.com/a/11…
  2. mysql8.0初始化:https://dev.mysql.com/doc/ref…

相關文章