本次编译安装版本是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会报错。
配置如下
[root@localhost 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。
[root@localhost mysql]# service mysqld start
Starting MySQL.. [确定]
运行安全初始化!!!
[root@localhost 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!
配置环境变量
[root@localhost mysql]# echo "export PATH=/usr/local/mysql/bin" >>/etc/profile
不重启服务器,是环境变量生效的两种方法。
一、使用"."
命令如下
. /etc/profile
二、使用"source"
source /etc/profile
登录mysql
[root@localhost 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
[root@localhost mysql]# mysql -uroot -p
-bash: mysql: command not found
环境变量问题,未把mysql命令添加进环境变量。
解决方法:
echo "export PATH=/usr/local/mysql/bin" >>/etc/profile
source /etc/profile
2、mysql启动报错
方法一
[root@localhost scripts]# service mysqld start
Starting MySQL.The server quit without updating PID file (/[失败]b/mysql/localhost.localdomain.pid).
报错原因:为对配置文件进行修改。
解决方法:在配置文件的mysqld下面添加
skip_name_resolve = ON
然后就可以正常运行。
方法二
有可能是配置文件冲突引起的,一般我们编译安装mysql时,会有配置文件产生。即/etc/my.cnf。但是我们一般不会选择这个,而是在etc目录下创建mysql目录,然后把mysql安装文件中的my-*.cnf复制到/etc/mysql/中取名my.cnf,但是在运行/usr/local/mysql/scripts/mysql_install_db 时,虽然指定了data目录,和mysql安装目录,但是mysql却自动选择了/etc/my.cnf,/etc/my.cnf却没有编辑,所以,会造成报错。
解决方法:
编辑或删除/etc/my.cnf,如果编辑,请写入正确的data目录和安装目录
datadir = /usr/local/mysql/data
basedir = /usr/local/mysql
然后再启动就OK了
serveice mysqld start
3、mysql安全初始化报错。
[root@localhost 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]。