您的位置:首页 > 数据库 > MySQL

编译安装mysql与mysql error 解决之道

2015-07-11 23:11 561 查看
摘要: mysql without updating pid files error & mysql.sock error

今天在济南出差,通过尝试编译安装mysql,使得一个诡异的问题得到解决。下面以流水的方式记述一下问题具体是怎样得到解决的。

昨天转发了一篇博文——error : The server quit without updating PID file

后来遇到了问题,error : cannot connect to local mysql server through socket (/tmp/mysql.sock)

后来辗转周折,先是删除mysql,然后用yum install mysql的方式重新安装MySQL,结果并没有得到改善。

Linux下编译安装MySQL

下面是在 CentOS 6.5 下编译安装 MySQL 5.6.20 (作者安装的是5.6.20,然而我在按照这个地址下载mysql源码时遇到障碍,所以换了另外一个版本)

1. 首先卸载系统自带的 mysql

# yum remove mysql

2.安装cmake

下载cmake源码

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

( 如果提示没安装C++编译器就先安装C++编译器:# yum -y install gcc-c++ )

# 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[/code] 这个地方可以百度一下其他的版本也行,只要网址能用来下载就好。

# 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文件)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息