CentOS上源码安装MySQL问题解决及安全优化
CentOS上源码安装MySQL问题解决及安全优化
0.说明
当然,MySQL的安装方法多种多样,在CentOS上,你可以采用YUM的方式安装,这样的好处是:快速方便。基本上,它会帮你解决所有的函数库依赖问题,正常情况下,只要YUM执行完成,那么MySQL也就可以使用了。
但我更倾向于使用源码的方式来安装MySQL,原因也很简单:除了有详细的官方文档外,你还可以非常清楚地知道你自己在做什么,这点在以后MySQL运行出现问题时将会有很大的帮助!
但即便是按照官方文档来安装,你也会遇到各种各样的问题,这里,我将呈现一个完整的过程给大家,直到完成下面的4个任务:
-
下载MySQL 5.6
-
安装准备:安装MySQL依赖函数库
-
安装与运行MySQL
-
优化MySQL
(1)账户安全优化
(2)数据库安全优化
我是安装完CentOS 6.5后就进行MySQL 5.6的安装,因此非常有参考价值!
CentOS 7下源码安装MySQL 5.6
MySQL5.7.3.0安装配置图解教程
Ubuntu 14.04下安装MySQL
《MySQL权威指南(原书第2版)》清晰中文扫描版 PDF
Ubuntu 14.04 LTS 安装 LNMP Nginx\PHP5 (PHP-FPM)\MySQL
Ubuntu 14.04下搭建MySQL主从服务器
Ubuntu 12.04 LTS 构建高可用分布式 MySQL 集群
Ubuntu 12.04下源代码安装MySQL5.6以及Python-MySQLdb
MySQL-5.5.38通用二进制安装
1.下载MySQL 5.6
下载地址:http://dev.mysql.com/downloads/mysql/5.6.html
进入该下载地址后,选择:
Linux - Generic (glibc 2.5) (x86, 64-bit), Compressed TAR Archive
或
Linux - Generic (glibc 2.5) (x86, 32-bit), Compressed TAR Archive
这取决于你用的是32位的还是64位的,这里,我下载的是64位的,下载完成后的包如下:
1 2 |
[root@leaf ~] # ls
mysql-5.6.29-linux-glibc2.5-x86_64. tar .gz
|
2.安装准备:安装MySQL依赖函数库
MySQL依赖一个名为libaio的函数库,需要先安装它,否则后面安装MySQL会出现问题。
如下:
1 2 3 4 5 6 |
[root@leaf ~] # yum search libaio #查找libaio的相关信息
[root@leaf ~] # yum install libaio
Loaded plugins: security
Setting up Install Process
Package libaio-0.3.107-10.el6.x86_64 already installed and latest version
Nothing to do
|
当然,有可能在你安装完CentOS后,你就已经安装了libaio,这取决你你当时安装CentOS时所选择的安装包,如上面我的安装提示,就说明我的系统上已经安装了libaio了。
3.安装与运行MySQL
(1)分别创建一个名为mysql的用户和用户组
如下:
1 2 |
[root@leaf ~] # groupadd mysql
[root@leaf ~] # useradd -r -g mysql -s /bin/false mysql
|
-r和-s参数的可以使得mysql这个用户没有登陆你系统的权限,这可以保证系统的安全性。
(2)解包与建立软链接
如下:
1 2 3 |
[root@leaf ~] # cd /usr/local
[root@leaf local ] # tar zxvf /root/mysql-5.6.29-linux-glibc2.5-x86_64.tar.gz
[root@leaf local ] # ln -s /usr/local/mysql-5.6.29-linux-glibc2.5-x86_64/ mysql
|
需要知道的是,正常情况下,我们习惯将编译安装的软件放在/usr/local目录下,当然你也可以自行选择,不过还是建议放在这里。
建立软链接的好处是,如果你需要使用mysql的安装目录,就不用去输入一长串的目录名称了,因为我们解压缩后的mysql包的目录,名字很长。
(3)初始化Data目录
解包完MySQL后,MySQL目录中会有一个data目录:
1 2 3 |
[root@leaf local ] # cd mysql
[root@leaf mysql] # ls -d data/
data/
|
里面包含的是MySQL运行所必需的系统信息,因此我们需要将这些数据初始化,如下:
1 2 3 4 5 |
[root@leaf mysql] # chown -R mysql . #修改mysql目录下的所有文件的属主为mysql
[root@leaf mysql] # chgrp -R mysql . #修改mysql目录下的所有文件的属组为mysql
[root@leaf mysql] # scripts/mysql_install_db --user=mysql #以mysql用户的身份初始化数据
[root@leaf mysql] # chown -R root . #修改mysql目录下的所有文件的属主为root
[root@leaf mysql] # chown -R mysql data #修改mysql目录下的data目录的属主为mysql
|
请务必按照上面的操作进行,至于详细的是什么意思,为什么要这样做,可以参考官方文档,有非常详细的解释:http://dev.mysql.com/doc/refman/5.6/en/data-directory-initialization.html
(4)启动MySQL
如下:
1 2 3 4 5 |
[root@leaf mysql] # bin/mysqld_safe --user=mysql &
[1] 30877
[root@leaf mysql] # 160306 11:58:50 mysqld_safe Logging to '/var/log/mysqld.log'.
160306 11:58:50 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
160306 11:58:51 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld .pid ended
|
理论上应该是可以的,但是看我上面的操作,出现了ended的提示,也就是mysql启动不了,这时我们去看一下日志信息:
1 2 3 4 5 |
[root@leaf ~] # tail -f /var/log/mysqld.log
......
2016-03-06 12:00:36 31231 [ERROR] /usr/local/mysql/bin/mysqld : Can 't create/write to file ' /var/run/mysqld/mysqld .pid' (Errcode: 2 - No such file or directory)
2016-03-06 12:00:36 31231 [ERROR] Can 't start server: can' t create PID file : No such file or directory
160306 12:00:36 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld .pid ended
|
问题:can't create PID file: No such file or directory,即找不到mysql启动的pid文件
解决方案:mysqld目录不存在,我们创建它就可以了
1 2 3 4 5 |
[root@leaf mysql] # mkdir /var/run/mysqld
[root@leaf mysql] # cd /var/run/mysqld/
[root@leaf mysqld] # touch mysqld.pid #创建mysqld.pid文件
[root@leaf mysqld] # cd ..
[root@leaf run] # chown -R mysql mysqld #将mysqld目录的属主设置为mysql
|
回到mysql目录,我们再启动一次mysql,如下:
1 2 3 4 |
[root@leaf run] # cd /usr/local/mysql
[root@leaf mysql] # bin/mysqld_safe --user=mysql
160306 12:12:45 mysqld_safe Logging to '/var/log/mysqld.log' .
160306 12:12:45 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
|
可以看到并没有终止运行的提示信息,我们再确认一下mysql服务是不是已经启动了:
1 2 3 4 |
[root@leaf ~] # netstat -antup
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID /Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 31484 /mysqld
|
mysql服务确实已经成功启动了!
(5)测试mysql服务
为了使mysql可以更好地在你的系统上运行,建议进行一定的mysql服务测试,如下:
1 2 3 4 |
[root@leaf mysql] # bin/mysqladmin version
bin /mysqladmin : connect to server at 'localhost' failed
error: 'Can' t connect to local MySQL server through socket '/tmp/mysql.sock' (2)'
Check that mysqld is running and that the socket: '/tmp/mysql.sock' exists!
|
按照官方文档的操作去测试,但却出现了上面的问题(需要注意的是,我的mysql服务已经开启了!)。
问题:/tmp/mysql.sock不存在
解决方案:其实mysql.sock是存在的,只是它不在/tmp目录下而已,默认情况下,mysql.sock在/var/lib/mysql/目录下,我们只需要创建一个软链接到/tmp目录下即可
1 |
[root@leaf mysql] # ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
|
这时再重复上面的操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[root@leaf mysql] # bin/mysqladmin version
bin /mysqladmin Ver 8.42 Distrib 5.6.29, for linux-glibc2.5 on x86_64
Copyright (c) 2000, 2016, 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.
Server version 5.6.29
Protocol version 10
Connection Localhost via UNIX socket
UNIX socket /tmp/mysql .sock
Uptime: 6 min 36 sec
Threads: 1 Questions: 2 Slow queries: 0 Opens: 67 Flush tables: 1 Open tables: 60 Queries per second avg: 0.005
|
成功了!然后我们再进行下面的操作热热身吧:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
[root@leaf mysql] # bin/mysqladmin -u root shutdown #通过mysqladmin关闭mysql服务
[root@leaf mysql] # bin/mysqld_safe --user=mysql & #启动mysql服务
#查看mysql数据库中默认存在的数据库
[root@leaf mysql] # bin/mysqlshow
+--------------------+
| Databases |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
#查看mysql数据库(注意此mysql数据库是一个实体,与上面的统称不同)中的数据表
[root@leaf mysql] # bin/mysqlshow mysql
Database: mysql
+---------------------------+
| Tables |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| innodb_index_stats |
| innodb_table_stats |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slave_master_info |
| slave_relay_log_info |
| slave_worker_info |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
查看mysql数据库中的所有user表
[root@leaf mysql] # bin/mysql -e "SELECT User, Host, plugin FROM mysql.user" mysql
+------+-----------+-----------------------+
| User | Host | plugin |
+------+-----------+-----------------------+
| root | localhost | mysql_native_password |
| root | leaf | mysql_native_password |
| root | 127.0.0.1 | mysql_native_password |
| root | ::1 | mysql_native_password |
| | localhost | mysql_native_password |
| | leaf | mysql_native_password |
+------+-----------+-----------------------+
|
需要注意的是,上面的这些测试必须要在你已经启动了mysql服务的情况下才去进行操作。同时,如果想知道每一步的详细解释,可以参考官方文档:http://dev.mysql.com/doc/refman/5.6/en/testing-server.html
准确来讲,MySQL是已经成功安装完成了!下面我们再做一些基础的优化,主要是从安全的角度去考虑。
更多详情见请继续阅读下一页的精彩内容:
|
评论暂时关闭