CentOS下安装MySQL5.6.10和安全配置教程详解

注:以下所有操作都在CentOS 6.5 x86_64位系统下完成。

#准备工作#

在安装MySQL之前,请确保已经使用yum安装了以下各类基础组件(如果系统已自带,还可以考虑yum update下基础组件):

gcc
cmake
openssl+openssl-devel
pcre+pcre-devel
bzip2+bzip2-devel
libcurl+curl+curl-devel
libjpeg+libjpeg-devel
libpng+libpng-devel
freetype+freetype-devel
php-mcrypt+libmcrypt+libmcrypt-devel
libxslt+libxslt-devel
gmp+gmp-devel
libxml2+libxml2-devel
mhash
ncurses+ncurses-devel
xml2

然后创建mysql的用户组和用户,并且不允许登录权限:

# id mysql
id: mysql:无此用户
# groupadd mysql
# useradd -g mysql -s /sbin/nologin mysql
# id mysql
uid=500(mysql) gid=500(mysql) 组=500(mysql)

#MySQL的安装#

给MySQL的安装准备目录:

# mkdir -p /data/mysql/data
# chown -R mysql:mysql /data/mysql

开始源码安装MySQL:

# cd /usr/local/src
# wget http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.10.tar.gz
# tar zxf mysql-5.6.10.tar.gz
# cd mysql-5.6.10
# cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.6.10 -DSYSCONFDIR=/usr/local/mysql-5.6.10/etc -DMYSQL_UNIX_ADDR=/usr/local/mysql-5.6.10/tmp/mysql.sock -DMYSQL_TCP_PORT=3306 -DMYSQL_USER=mysql -DMYSQL_DATADIR=/data/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_MYISAM_STORAGE_ENGINE=1 -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1
...
CMake Warning:
Manually-specified variables were not used by the project:
MYSQL_USER
-- Build files have been written to: /usr/local/src/mysql-5.6.10
# make && make install
# mkdir -p /usr/local/mysql-5.6.10/etc
# mkdir -p /usr/local/mysql-5.6.10/tmp
# ln -s /usr/local/mysql-5.6.10/ /usr/local/mysql
# chown -R mysql:mysql /usr/local/mysql-5.6.10
# chown -R mysql:mysql /usr/local/mysql

给当前环境添加MySQL的bin目录:

# vim /etc/profile
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
$ source /etc/profile

执行初初始化配置脚本并创建系统自带的数据库和表:

# cd /usr/local/mysql
# scripts/mysql_install_db --user=mysql --datadir=/data/mysql/data
...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h iZ94mobdenkZ password 'new-password'
Alternatively you can run:
./bin/mysql_secure_installation
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the manual for more instructions.
You can start the MySQL daemon with:
cd . ; ./bin/mysqld_safe &
You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl
Please report any problems with the ./bin/mysqlbug script!
The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
WARNING: Found existing config file ./my.cnf on the system.
Because this file might be in use, it was not replaced,
but was used in bootstrap (unless you used --defaults-file)
and when you later start the server.
The new default config file was created as ./my-new.cnf,
please compare it with your file and take the changes you need.
WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
--defaults-file argument to mysqld_safe when starting the server

注:由于MySQL在启动的时候,会先去/etc/my.cnf找配置文件,如果没有找到则搜索$basedir/my.cnf,也即/usr/local/mysql-5.6.10/my.cnf,所以必须确保/etc/my.cnf没有存在,否则可能导致无法启动。

实际操作上发现系统上存在该文件,所以这里可能需要将该文件先备份改名,然后再根据上面的配置写配置文件:

# mv /etc/my.cnf /etc/my.cnf.bak
# vim /usr/local/mysql-5.6.10/my.cnf
[mysqld]
basedir=/usr/local/mysql-5.6.10
datadir=/data/mysql/data
socket=/usr/local/mysql-5.6.10/tmp/mysql.sock
user=mysql
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

修改MySQL用户root的密码,这里使用mysqld_safe安全模式启动:

# mysqld_safe --user=mysql --skip-grant-tables --skip-networking &
[1] 3970
[root@iZ94mobdenkZ ~]# 141230 19:02:31 mysqld_safe Logging to '/data/mysql/data/centos.err'.
141230 19:02:32 mysqld_safe Starting mysqld daemon with databases from /data/mysql/data

这个时候已经启动了mysqd_safe安全模式,另开一个窗口作为客户端连入MySQL服务器:

# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.10 Source distribution
Copyright (c) 2000, 2013, 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> use mysql;
mysql> update user set password=password('yourpassword') where user='root';
mysql> flush privileges;
mysql> exit;

修改完毕之后使用kill把mysqld_safe进程杀死:

# ps aux | grep mysql
root 3970 0.0 0.2 106308 1492 pts/1 S 19:02 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --user=mysql --skip-grant-tables --skip-networking
mysql 4143 0.1 18.0 558280 90316 pts/1 Sl 19:02 0:00 /usr/local/mysql-5.6.10/bin/mysqld --basedir=/usr/local/mysql-5.6.10 --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql-5.6.10/lib/plugin --user=mysql --skip-grant-tables --skip-networking --log-error=/data/mysql/data/centos.err --pid-file=/data/mysql/data/centos.pid --socket=/usr/local/mysql-5.6.10/tmp/mysql.sock
root 4313 0.0 0.1 103252 836 pts/0 S+ 19:05 0:00 grep mysql
# kill -9 3970
# kill -9 4143

或者回到刚才启动mysqld_safe的窗口ctrl+c将进程杀死也行。

复制服务启动脚本:

# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
# chmod +x /etc/init.d/mysqld

设置开机启动MySQL服务并正常开启MySQL服务(非必要项):

# chkconfig mysqld on
# service mysqld
Usage: mysqld {start|stop|restart|reload|force-reload|status} [ MySQL server options ]
# service mysqld start
Starting MySQL.

以后就可以直接通过service mysqld命令来开启/关闭MySQL数据库了。

最后,建议生产环境下运行安全设置脚本,禁止root用户远程连接,移除test数据库和匿名用户等:

# /usr/local/mysql-5.6.10/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):

注:上面输入的root密码指的是前面设置的MySQL的root账户的密码。

至此,MySQL数据库已经安装完毕。

#MySQL的安全配置#

1、确保启动MySQL不能使用系统的root账号,必须是新建的mysql账号,比如:

# mysqld_safe --user=mysql

2、MySQL安装好运行初始化数据库后,默认的root账户密码为空,必须给其设置一个密码,同时保证该密码具有较高的安全性。比如:

mysql> user mysql;
mysql> update user set password=password('yourpassword') where user='root';
mysql> flush privileges;

3、删除默认数据库及用户:

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
mysql> drop daabase test;
mysql> use mysql;
mysql> select host,user from user;
+--------------+------+
| host | user |
+--------------+------+
| 127.0.0.1 | root |
| ::1 | root |
| centos | |
| centos | root |
| localhost | |
| localhost | root |
+--------------+------+
mysql> delete from user where not(host='localhost' and user='root');
mysql> flush privileges;

注:上面的user表中的数据可能会有所不同。

4、当开发网站连接数据库的时候,建议建立一个用户只针对某个库有update/select/delete/insert/drop table/create table等权限,减小某个项目的数据库的用户名和密码被窃取后造成其他项目受影响,比如:

mysql>create database yourdbname default charset utf8 collate utf8_general_ci;
mysql>create user 'yourusername'@'localhost' identified by 'yourpassword';
mysql> grant select,insert,update,delete,create,drop privileges on yourdbname.* To 'yourusername'@localhost identified by 'yourpassword';

5、数据库文件所在的目录不允许未经授权的用户访问,需要控制对该目录的访问,比如:

# chown -R mysql:mysql /data/mysql/data
# chmod -R go-rwx /data/mysql/data

以上所述是小编给大家介绍的CentOS下安装MySQL5.6.10和安全配置教程详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

(0)

相关推荐

  • 64位CentOs7源码安装mysql-5.6.35过程分享

    首先安装依赖包,避免在安装过程中出现问题 [root@bogon liuzhen]# yum -y install gcc gcc-c++ [root@bogon liuzhen]# yum -y install cmake [root@bogon liuzhen]# yum -y install ncurses-devel [root@bogon liuzhen]# yum -y install autoconf [root@bogon liuzhen]# yum -y install per

  • CentOS 6.6服务器编译安装lnmp(Nginx1.6.2+MySQL5.6.21+PHP5.6.3)

    准备篇: CentOS 6.6系统安装配置图解教程 http://www.jb51.net/os/239738.html 一.配置防火墙,开启80端口.3306端口 vi /etc/sysconfig/iptables #编辑防火墙配置文件 # Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT

  • CentOS6.5下RPM方式安装mysql5.6.33的详细教程

    1.mysql下载 下载地址:https://dev.mysql.com/downloads/mysql/5.6.html<br>下载以下安装包: MySQL-client-5.6.33-1.el6.x86_64.rpm MySQL-devel-5.6.33-1.el6.x86_64.rpm MySQL-server-5.6.33-1.el6.x86_64.rpm 2.查看是否已经安装了mysql,有则移除 rpm -qa|grep -i mysql mysql-libs-5.1.66-2.e

  • CentOS 7.0编译安装lnmp教程(Nginx1.6.0+MySQL5.6.19+PHP5.5.14)

    准备篇: CentOS 7.0系统安装配置图解教程 http://www.jb51.net/os/188487.html 一.配置防火墙,开启80端口.3306端口 CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall开机启动 2.安装iptab

  • CentOS 7.2.1511 编译安装Nginx1.10.1+MySQL5.6.33+PHP5.6.26运行环境

    准备篇 一.防火墙配置 CentOS 7.x默认使用的是firewall作为防火墙,这里改为iptables防火墙. 1.关闭firewall: systemctl stop firewalld.service #停止firewall systemctl disable firewalld.service #禁止firewall开机启动 2.安装iptables防火墙 yum install iptables-services #安装 vi /etc/sysconfig/iptables #编辑

  • CentOS 6.4下编译安装MySQL5.6.14教程

    概述: CentOS 6.4下通过yum安装的MySQL是5.1版的,比较老,所以就想通过源代码安装高版本的5.6.14. 正文: 一.卸载旧版本 使用下面的命令检查是否安装有MySQL Server rpm -qa | grep mysql 有的话通过下面的命令来卸载掉 rpm -e mysql //普通删除模式 rpm -e --nodeps mysql // 强力删除模式,如果使用上面命令删除时, 提示有依赖的其它文件,则用该命令可以对其进行强力删除 二.安装MySQL 安装编译代码需要的

  • CentOS下安装MySQL5.6.10和安全配置教程详解

    注:以下所有操作都在CentOS 6.5 x86_64位系统下完成. #准备工作# 在安装MySQL之前,请确保已经使用yum安装了以下各类基础组件(如果系统已自带,还可以考虑yum update下基础组件): gcc cmake openssl+openssl-devel pcre+pcre-devel bzip2+bzip2-devel libcurl+curl+curl-devel libjpeg+libjpeg-devel libpng+libpng-devel freetype+fre

  • 使用YUM在Linux(CentOS 7)下安装mysql 5.7.18的教程详解

    项目需要使用MySQL,由于以前都是在windows下傻瓜式安装,基本没有遇到什么问题,但是这次是在服务器上安装,由于到Linux上安装软件不熟悉,走了不少弯路,耽误了好多时间.总结下来,以免下次再走弯路. ****************************图片插入不成功,不知道是怎么回事********************************* 一.各种环境: linux版本:CentOS Linux release 7.2.1511 (core) mysql版本:communi

  • CentOS安装mysql5.7 及简单配置教程详解

    安装 保证你的用户有权限 安装 没有 切换 root su root (su的意思:swich user) # rpm -ivh http://dev.mysql.com/get/mysql57-community-release-el6-9.noarch.rpm 可能会遇到 warning: /var/tmp/rpm-tmp.6V5aFC: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY 可以忽略(个人意见,百度了一下没找到合适的答案)

  • windows server2016安装MySQL5.7.19解压缩版教程详解

    记录了MySQL 5.7.19 winx64解压缩版安装教程,具体内容如下 系统环境:Win7 x64 软件准备:mysql 5.7.19 winx64 下载网址:https://dev.mysql.com/downloads/mysql/ 配置安装流程 具体安装如下: 1.把 mysql-5.7.19-winx64.zip 压缩文件解压到 C:\MySQL\ 目录下: 2.在 C:\MySQL\ 目录下新建 my.ini 配置文件: 3.用文本编辑器或其他编辑器打开 my.ini 文件,把以下

  • linux mint下安装phpstorm2020包括JDK部分的教程详解

    环境:linux mint 20,一切都是最新的版本. 都知道,PHPSTORM破解和运行都是离不开JDK/JRE的. 咱们先把这东西搞定 删除已安装的JDK sudo apt-get purge openjdk* 安装新的JDK sudo apt install default-jre sudo apt install openjdk-11-jre-headless sudo apt install openjdk-8-jre-headless 接下来,放心大胆的去官网下载phpstorm.

  • Mac下安装mysql5.7 完整步骤(图文详解)

    最近使用Mac系统,准备搭建一套本地web服务器环境.因为Mac系统自带PHP和apach,但是没有自带mysql,所以要手动去安装mysql,本次安装mysql最新版5.7.17. 1.官网下载 MySQL v5.7官方正式版下载地址:http://www.jb51.net/softs/451120.html 点击上面的地址,会看到如下图的页面.你可能不知道该下载哪一个,我下载的是最后一个,就是图中标注红色的那个按钮,为什么?因为它是dmg文件,傻瓜式安装,一路确认就可以. 点进去之后,你会看

  • CentOS7.0下安装PHP5.6.30服务的教程详解

    关于php-fpm nginx本身不能处理PHP,它只是个web服务器,当接收到请求后,如果是php请求,则发给php解释器处理,并把结果返回给客户端. nginx一般是把请求发fastcgi管理进程处理,fascgi管理进程选择cgi子进程处理结果并返回被nginx. PHP-FPM是一个PHP FastCGI管理器,是只用于PHP的. PHP在 5.3.3 之后已经讲php-fpm写入php源码核心了.所以已经不需要另外下载了. 获取PHP下载地址 为什么选择5.6.30这个版本,因为学习,

  • mysql5.7.10开启慢查询详解

    如下所示: #在/etc/my.cnf中的[mysqld]中加入如下代码: slow-query-log=On slow_query_log_file=/data/mysql/log/mysql_slow_query.log long_query_time=2 log_queries_not_using_indexes = ON 第一句是开启慢查询 第二句是用来定义慢查询日志的路径 第三句是用来定义查过多少秒的查询算是慢查询,我这里定义的是2秒 第四句就是记录下没有使用索引的query 以上这篇

  • windows版本下mysql的安装启动和基础配置图文教程详解

    下载: 第一步 : 打开网址(进入官网下载) :https://www.mysql.com ,点击downloads之后跳转到https://www.mysql.com/downloads 第二步 :跳转至网址https://dev.mysql.com/downloads/,选择Community选项 第三步 :点击MySQL Community Server进入https://dev.mysql.com/downloads/mysql/页面,再点击5.6版本的数据库 第四步:windows操作

  • centos 安装Python3 及对应的pip教程详解

    安装Python3 安装Python依赖: yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel 由于Python在linux不支持我们以可执行程序的方式安装,所以需要我们选择对应的版本源码安装 源码下载站点: https://www.python.org/ftp/python/ 以Python3.6为例: wget https://www.python.org/ftp/p

随机推荐