編譯安裝mysql與mysql error 解決之道

今天在濟南出差,經過嘗試編譯安裝mysql,使得一個詭異的問題獲得解決。下面以流水的方式記述一下問題具體是怎樣獲得解決的。mysql

昨天轉發了一篇博文——error : The server quit without updating PID filec++

後來遇到了問題,error : cannot connect to local mysql server through socket (/tmp/mysql.sock)sql

後來展轉周折,先是刪除mysql,而後用yum install mysql的方式從新安裝MySQL,結果並無獲得改善。 shell

Linux下編譯安裝MySQL數據庫

下面是在 CentOS 6.5 下編譯安裝 MySQL 5.6.20 (做者安裝的是5.6.20,然而我在按照這個地址下載mysql源碼時遇到障礙,因此換了另一個版本)app

1. 首先卸載系統自帶的 mysqlsocket

# yum remove mysql

2.安裝cmake ide

下載cmake源碼ui

# wget http://www.cmake.org/files/v2.8/cmake-2.8.5.tar.gz
# tar xzfv cmake-2.8.5.tar.gz
# cd cmake-2.8.5
# ./configure

( 若是提示沒安裝C++編譯器就先安裝C++編譯器:# yum -y install gcc-c++ )this

# make 
# make install

查看 cmake 版本

# cmake -version

3.安裝 ncurses 和 bison

# yum -y install ncurses-devel
# yum -y install bison

4. 建立mysql組和用戶

# groupadd mysql
# useradd -g mysql mysql

5. 建立數據庫目錄及分配訪問權限

# mkdir -p /home/app/mysql
# chown -R root:mysql /home/app/mysql
# chown -R mysql:mysql /home/app/mysql/data

6. 編譯安裝mysql

下載mysql源碼

# wget http://cdn.mysql.com/Downloads/MySQL-5.6/mysql-5.6.20.tar.gz

這個地方能夠百度一下其餘的版本也行,只要網址能用來下載就好。

# tar xzfv mysql-5.6.20.tar.gz
# cd mysql-5.6.20
# cmake -DCMAKE_INSTALL_PREFIX=/home/app/mysql -DMYSQL_DATADIR=/home/app/mysql/data -DMYSQL_BASEDIR=/home/app/mysql

最後一行命令反覆折騰了我好幾遍,必定要注意一些細節,好比空格的問題。

參數說明:

-DCMAKE_INSTALL_PREFIX    //安裝根目錄

-DINSTALL_DATADIR         //數據存儲目錄

-DSYSCONFDIR              //配置文件(my.cnf)目錄

# make 
# make install


7. 配置 mysql

其中my-default.cnf須要結合實際狀況判斷,具體的文件名須要與實際相符。

# cd /home/app/mysql
# cp ./support-files/my-default.cnf /etc/my.cnf
# vi /etc/my.cnf

這一步十分關鍵,在 [mysqld] 段修改

basedir = /home/app/mysql
datadir = /home/app/mysql/data


保存退出編輯。


8. 初始化數據庫

# cd /home/app/mysql
# ./scripts/mysql_install_db --basedir=/home/app/mysql --datadir=/home/app/mysql/data --defaults-file=/etc/my.cnf --user=mysql
# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
# chmod 755 /etc/rc.d/init.d/mysqld
# chkconfig --add mysqld

mysql_install_db這個地方須要特別注意。

9. 設置啓動腳本變量 

# vi /etc/rc.d/init.d/mysqld
basedir=/home/app/mysql
datadir=/home/app/mysql/data

保存退出。


10. 設置變量環境

# echo "PATH=$PATH:/u01/app/mysql/bin" >> /etc/profile //(永久生效)
# export PATH=$PATH:/u01/app/mysql/bin //(當前生效)


11. 啓動服務

# service mysqld start
/etc/init.d/mysqld: line 46: /home/app/mysql: is a directory
/etc/init.d/mysqld: line 47: /home/app/mysql/data: is a directory
Starting MySQL.                                            [  OK  ]

12. 設置數據庫管理員root密碼

原本理論上我能夠直接進入設置密碼的環節。然而事與願違,坑老是不少,歷經千辛作了前面的那些工做,結果系統報錯:MySQL: Starting MySQL….. ERROR! The server quit without updating PID file

# mysqladmin -u root password 'wushuu'

(注:在當前會話窗口操做,不然會出現 「mysqladmin: command not found」 錯誤,除非從新啓動系統。)

終於找到了一篇能夠解決個人問題的文檔

This step-by-step guide is mainly for FreeBSD, however the idea is the same for Linux. Every once a while, when I update my FreeBSD box, the system likes to shutdown my MySQL server. Therefore, I need to start it again after the update is done. Unfortunately, the upgrade process is not smooth every time. Sometimes it will throw me some error.

/usr/local/etc/rc.d/mysql.server start

Oh well, I got the following error messages:

Starting MySQL..... ERROR! The server quit without updating PID file.

Sometimes, the message will tell you the exact location of which PID file:

Starting MySQL..... ERROR! The server quit without updating PID file (/var/db/mysql/www.icesquare.com.pid).

There are several solutions to troubleshoot these problems. I will go over each one by one.

Solution 1: Reboot The Computer

Although it sounds simple, but it really works. During the system upgrade, the OS may disable some of your daemons. Instead of troubleshooting each one by one, the easiest way is to start everything over. For example, I experienced this problem today after upgrading the Apache and Ruby (Yes, MySQL is not part of the update), and I got this error message afterward. After rebooting the computer, the error message is gone.

Solution 2: Remove Your MySQL Config File

If you have modified your MySQL configuration file, MySQL may not like it few versions after (MySQL is not backward compatibility friendly). It can be the problem of using an unsupported variable, or something similar. The easiest way is to remove your configuration file, and try to start the MySQL server again:

Backup your MySQL configuration first.

mv /etc/my.cnf /etc/my.cnf.backup

And restart the MySQL server again:

/usr/local/share/mysql/mysql.server start

Hopefully you will see the following message:

Starting MySQL. SUCCESS!

Solution 3: Upgrade Your Database File

Sometimes, the newer MySQL doesn’t like the database created in earlier version. I discovered this when I upgrade to MySQL 5.5.7:

Starting MySQL..... ERROR! The server quit without updating PID file (/var/db/mysql/www.icesquare.com.pid).

Since MySQL tells me which PID file causes the problem, I open the file and take a look what’s going on:

sudo tail /var/db/mysql/www.icesquare.com.err

And I saw something interesting: tables: Table ‘mysql.proxies_priv’ doesn’t exist:

101112 10:49:16 InnoDB: Initializing buffer pool, size = 128.0M 101112 10:49:16 InnoDB: Completed initialization of buffer pool 101112 10:49:16 InnoDB: highest supported file format is Barracuda. 101112 10:49:17 InnoDB: 1.1.3 started; log sequence number 1589404 101112 10:49:17 [ERROR] Fatal error: Can't open and lock privilege tables: Table 'mysql.proxies_priv' doesn't exist 101112 10:49:17 mysqld_safe mysqld from pid file /var/db/mysql/www.icesquare.com.pid ended

 

The reason is very simple. MySQL could not open a table created in the earlier version (< 5.7.7) because it is not compatible with the current version. So, we can try to start the MySQL in safe mode through rc.d. First, you can edit the /etc/rc.conf and put the following into the file:

mysql_enable="YES" mysql_args="--skip-grant-tables --skip-networking"

Restart MySQL through rc.d:

/usr/local/etc/rc.d/mysql-server start

If you did it right, you should see something like the following:

Starting MySQL.. SUCCESS!

Now, MySQL is already running the safe-mode. We want to perform a MySQL upgrade on all tables:

sudo mysql_upgrade

You should see something like this:

Looking for 'mysql' as: mysql Looking for 'mysqlcheck' as: mysqlcheck Running 'mysqlcheck' with connection arguments: '--port=3306' '--socket=/tmp/mysql.sock' Running 'mysqlcheck' with connection arguments: '--port=3306' '--socket=/tmp/mysql.sock' mysql.columns_priv OK mysql.db OK mysql.event OK mysql.func OK mysql.general_log OK mysql.help_category OK mysql.help_keyword OK mysql.help_relation OK mysql.help_topic OK mysql.host OK mysql.ndb_binlog_index OK mysql.plugin OK mysql.proc OK mysql.procs_priv OK mysql.servers OK mysql.slow_log OK mysql.tables_priv OK mysql.time_zone OK mysql.time_zone_leap_second OK mysql.time_zone_name OK mysql.time_zone_transition OK mysql.time_zone_transition_type OK mysql.user OK Running 'mysql_fix_privilege_tables'... OK

Now, we want to switch the MySQL back to normal mode by commenting the extra options in /etc/rc.conf:

mysql_enable="YES" #mysql_args="--skip-grant-tables --skip-networking"

And restart MySQL through /etc/rc.d:

/usr/local/etc/rc.d/mysql-server restart

Now the MySQL is up and running again!

Happy MySQLing.

–Derrick

從新啓動服務

# service mysqld restart 
/etc/init.d/mysqld: line 46: /home/app/mysql: is a directory
/etc/init.d/mysqld: line 47: /home/app/mysql/data: is a directory
/etc/init.d/mysqld: line 46: /home/app/mysql: is a directory
/etc/init.d/mysqld: line 47: /home/app/mysql/data: is a directory
Shutting down MySQL.                                       [  OK  ]
/etc/init.d/mysqld: line 46: /home/app/mysql: is a directory
/etc/init.d/mysqld: line 47: /home/app/mysql/data: is a directory
Starting MySQL.                                            [  OK  ]

                            


13. 進入 MySQL

# mysql -u root -p

(注:在當前會話窗口操做,不然會出現 「mysql: command not found」 錯誤,除非從新啓動系統。)

Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.20 Source distribution
Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>


登陸成功了,趕快感覺一下 :)

總之,通常而言,若是是mysql.sock 的error,基本上是因爲mysql service 沒有啓動,從而不能用socket通訊的方式從終端terminal與mysql server通訊;然而當你敲service mysql start以後就有可能會遇到另一個錯誤,without updating pid files ,那麼此時的解決之道是查看對應的和pid文件在同一個目錄下的後綴爲err的錯誤日誌,而後按照本教程一步步操做便可。(/var/db/mysql/www.icesquare.com.err文件)

相關文章
相關標籤/搜索