1. 程式人生 > >mysql編譯安裝及報錯詳解

mysql編譯安裝及報錯詳解

本次編譯安裝版本是mysql-5.5.61 mysql下載連線 安裝mysql,我們需要提前建立好mysql使用者,並且禁止mysql登入伺服器,不建立家目錄。

useradd mysql -M -s /sbin/nologin 
id mysql

id mysql 判斷使用者是否新增完成。 開始編譯安裝mysql之前,我們需要先安裝一些安裝包,對mysql進行支援,如果不安裝這些,mysql肯定無法編譯安裝。這些安裝包可以直接使用yum進行安裝。

yum -y install ncurses-devel cmake gcc-c++

開始正式編譯。解壓編譯。

tar -zxf mysql-5.5.61.tar.gz
cd mysql-5.5.61
cmake .
echo $?  
make && make install

編譯安裝完成之後,需要對mysql進行配置。否則無法啟動和執行。

mkdir /etc/mysql
cp /usr/local/mysql/support-files/my-large.cnf /etc/mysql/my.cnf
cp /usr/local/mysql/support-files/mysql.server /etc/rc.d/init.d/mysqld
chown -R mysql.mysql /usr/local/mysql
chmod a+x /etc/rc.d/init.d/mysqld
/usr/local/mysql/scripts/mysql_install_db --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql/ --user=mysql

編輯mysql的主配置檔案,在[mysqld]下面新增

datadir = /usr/local/mysql/data
    skip_name_resolve = ON

否則mysql會報錯。

配置如下

[r[email protected] mysql]# vim /etc/mysql/my.cnf 
# locations which depend on the deployment platform.
# You can copy this option file to one of those
# locations. For information about these locations, see:
# http://dev.mysql.com/doc/mysql/en/option-files.html
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# with the "--help" option.

# The following options will be passed to all MySQL clients
[client]
#password       = your_password
port            = 3306
socket          = /tmp/mysql.sock

# Here follows entries for some specific programs

# The MySQL server
[mysqld]
port            = 3306
socket          = /tmp/mysql.sock
skip-external-locking
key_buffer_size = 16K
max_allowed_packet = 1M
table_open_cache = 4
sort_buffer_size = 64K
read_buffer_size = 256K
read_rnd_buffer_size = 256K
net_buffer_length = 2K
thread_stack = 128K
datadir = /usr/local/mysql/data
skip_name_resolve = ON

開啟mysql。

[[email protected] mysql]# service mysqld start
Starting MySQL..                                           [確定]

執行安全初始化!!!

[[email protected] mysql]# /usr/local/mysql/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!


By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] y
 ... Success!

By default, MySQL comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y
 ... Success!

Cleaning up...



All done!  If you've completed all of the above steps, your MySQL
installation should now be secure.

Thanks for using MySQL!

配置環境變數

   [[email protected] mysql]# echo "export PATH=/usr/local/mysql/bin" >>/etc/profile

不重啟伺服器,是環境變數生效的兩種方法。 一、使用"." 命令如下

. /etc/profile

二、使用"source"

source /etc/profile

登入mysql

[[email protected] mysql]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.5.61 Source distribution

Copyright (c) 2000, 2018, 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> exit
Bye

mysql報錯: 1、command not found

[[email protected] mysql]# mysql -uroot -p
-bash: mysql: command not found

環境變數問題,未把mysql命令新增進環境變數。 解決方法:

echo "export PATH=/usr/local/mysql/bin" >>/etc/profile
source  /etc/profile

2、mysql啟動報錯 [[email protected] scripts]# service mysqld start Starting MySQL.The server quit without updating PID file (/[失敗]b/mysql/localhost.localdomain.pid). 報錯原因:為對配置檔案進行修改。 解決方法:在配置檔案的mysqld下面新增

skip_name_resolve = ON

然後就可以正常執行。 3、mysql安全初始化報錯。

[[email protected] mysql]# ./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): 
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

原因:mysql未能成功啟動。未能啟動原因如上 解決方法:按照問題2進行修復,修復成功之後,就可以安全初始化。 4、mysql啟動成功,但是無法登入和完成安全初始化。 原因:mysql.sock檔案不存在,或者配置檔案中指向錯誤的sock位置。 解決方法:find / -name mysql.sock -type f 查詢位置。並且使用ln -s 命令進行連結。

如果文件有問題,或者需要交流,請聯絡作者,[email protected]