mysql 安裝總結
mysql 安裝總結
系統:cxentos7
mysql版本:mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz
新增mysql使用者
一般啟動mysql都是使用的mysql使用者程序來啟動。所以第一步,新增mysql使用者useradd mysql
。
解壓重新命名檔案
解壓上面的檔案tar -zxvf mysql-5.6.42-linux-glibc2.12-x86_64.tar.gz
拷貝到一個你指定的資料夾下面,然後重新命名為一個短點的名字mv mysql-5.6.42-linux-glibc2.12-x86_64 /home/cube/mysql
初始化資料庫
這一步很關鍵,筆者當時卡在這裡很久,因為執行指令碼的位置很重要。當時筆者直接進入到scripts
下執行,這是非常致命的錯誤,最後初始化的資料庫資料會在scripts 資料夾下面生成,顯然這是不對的。我們實際的資料資料夾是在mysql/data
下面。
chown -R mysql:mysql mysql cd mysql ./scripts/mysql_install_db --user=mysql --basedir=/home/cube/mysql --datadir=/home/cube/mysql/data
上面一定要先改變下檔案的許可權,否則在初始化資料的時候就會因為許可權不足導致失敗。
當出現一下資訊的時候說明資料庫初始化成功。
OK 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: /home/cube/mysql/bin/mysqladmin -u root password 'new-password' /home/cube/mysql/bin/mysqladmin -u root -h 78afdad83326 password 'new-password' Alternatively you can run: /home/cube/mysql/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 . ; /home/cube/mysql/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 New default config file was created as /home/cube/mysql/my.cnf and will be used by default by the server when you start it. You may edit this file to change server settings
配置my.cnf檔案
[mysqld] basedir = /home/cube/mysql datadir = /home/cube/mysql/data port = 3306 socket = /home/cube/mysql/mysqld.sock user=mysql sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
我們來啟東試試下./support-files/mysql.server start
發現失敗,錯誤資訊如下:
bash-4.2# ./support-files/mysql.server start ./support-files/mysql.server: line 244: my_print_defaults: command not found ./support-files/mysql.server: line 264: cd: /usr/local/mysql: No such file or directory Starting MySQL ERROR! Couldn't find MySQL server (/usr/local/mysql/bin/mysqld_safe)
發現,在啟動服務的時候,去/usr/local/mysql
來找安裝包,當我們使用yum來安裝mysql的時候,會發現mysql的安裝位置就是/usr/local/mysql
,所以這裡我們需要再次配置下路徑。
配置 mysql.server
開啟vim ./support-files/mysql.server
配置下basedir
和datadir
兩個屬性
basedir=/home/cube/mysql datadir=/home/cube/mysql/data
好了,我們再來重啟下試試。
啟動服務
./support-files/mysql.server start
出現如下資訊,說明啟動成功
bash-4.2# ./support-files/mysql.server start Starting MySQL.Logging to '/home/cube/mysql/data/78afdad83326.err'. SUCCESS!
使用mysql連線
bin/mysql -uroot -P3306 -p
發現會出現如下的錯誤:
bash-4.2# bin/mysql -uroot -P3306 -p Enter password: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
錯誤顯示的是找不到/tmp/mysql.sock
這個檔案,記得在上面的my.cnf檔案中,我們配置了socket的檔案位置。
在 my.cnf 中配置client
屬性,保證 mysql能夠拿到正確的配置去連線。
[client] port=3306 socket=/home/cube/mysql/mysqld.sock
同時,在連線的指定配置檔案--defaults-file=my.cnf
完整的如下:
./bin/mysql --defaults-file=my.cnf -uroot -p
進入會提示要輸入密碼,這裡不用輸入密碼,直接回車即可進入。
最後,到此所有已經結束。剩下的還有一些需要完善的地方,比如 設定編碼 ,設定日誌檔案等等。