CentOS下安装MySQL5.6.10和安全配置教程详解
作者:brishenzhou 发布时间:2024-01-24 08:28:26
注:以下所有操作都在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和安全配置教程详解网站的支持!
来源:http://www.cnblogs.com/brishenzhou/archive/2016/12/07/6140261.html


猜你喜欢
- 本文实例讲述了python实现将元祖转换成数组的方法。分享给大家供大家参考。具体分析如下:python的元祖使用一对小括号表示的,元素是固定
- Python可是真强大。但他具体是怎么强大的,让我们一点一点来了解吧(小编每天晚上下班回家会抽时间看看教程,多充实下自己也是好的)。废话不多
- 1. 介绍前面我们尝试通过python实现了代码雨以及字母随机闪烁的效果,这次,我们再来实现一个代码的线性扫面。同样的,此次我们仍然是使用3
- python使用pyecharts库画地图数据可视化导库中国地图代码结果世界地图代码结果省级地图代码结果地级市地图代码结果导库from py
- 本次,我们来看看索引、提交频率对InnoDB表写入速度的影响,了解有哪些需要注意的。先直接说几个结论吧:1、关于索引对写入速度的影响:a、如
- 制作NBA数据爬虫捋顺思路我们在这里选择的是百度体育带来的数据,我们在百度当中直接搜索NBA跳转到网页,我们可以看到,百度已经为我们提供了相
- 在任何有监督机器学习项目的模型构建阶段,我们训练模型的目的是从标记的示例中学习所有权重和偏差的最佳值。如果我们使用相同的标记示例来测试我们的
- 看过一篇关于下载网页中图片的文章,它只能下载以http头的图片,我做了些改进,可以下载网页中的所有连接资源,并按照网页中的目录结构建立本地目
- 首先需要安装itchat库,可以pip install itchat安装,也可以在pycharm里安装# -*- coding:utf-8
- 如果我们在标识列中插入值,例如: insert member(id,username) values(10,'a
- 本文主要针对Golang的内置库 net/http 做了简单的扩展,通过添加中间件的形式实现了管道(Pipeline)模式,这样的好处是各模
- 随着网络的发展,网速和机器速度的提高,越来越多的网站用到了丰富客户端技术。而现在Ajax则是最为流行的一种方式。JavaScript是一种解
- 代码如下:Create trigger tri_wk_CSVHead_History on wk_CSVHead_History --声明
- 模块的的作用主要是用于字符串和文本处理,查找,搜索,替换等复习一下基本的正则表达式吧 .:匹配除了换行符以为的任意单个字符&nbs
- JavaScript经常会验证中文,这里提供两个例子: Javascript代码: /** *A simple example */ fun
- 使用Tensorflow进行深度学习训练的时候,需要对训练好的网络模型和各种参数进行保存,以便在此基础上继续训练或者使用。介绍这方面的博客有
- 实现一个树形表格的时候有多种方法:比如把 ztree 的树形直接拼接成表格,或者用强大的 jqgrid 实现,今天介绍一个比较轻量级的实现:
- 在正式的生产环境中,我们常常会需要监控服务器的状态,以保证公司整个业务的正常运转,常常我们会用到像nagios、zabbix这类工具进行实时
- 总的来讲,JavaSever PagesTM(JSP)和 微软的Active Sever Pages(ASP)在技术方面有许多相似之处。两者
- rpc有多种调用方式,http、json-rpc、tcp一、服务端在代码中,启动了三个服务package mainimport ("