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

#准备工作#

在安装MySQL之前,请确保已经使用yum安装了各类基础组件,具体见《CentOS安装LNMP环境的基础组件》

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

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

#MySQL的安装#

给MySQL的安装准备目录:

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

开始源码安装MySQL,make的时候大概要10分钟左右:

# 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..tar.gz
# cd mysql-5.6. # cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.6. -DSYSCONFDIR=/usr/local/mysql-5.6./etc -DMYSQL_UNIX_ADDR=/usr/local/mysql-5.6./tmp/mysql.sock -DMYSQL_TCP_PORT= -DMYSQL_USER=mysql -DMYSQL_DATADIR=/data/mysql/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DWITH_MYISAM_STORAGE_ENGINE= -DWITH_INNOBASE_STORAGE_ENGINE= -DWITH_ARCHIVE_STORAGE_ENGINE= -DWITH_BLACKHOLE_STORAGE_ENGINE= -DENABLED_LOCAL_INFILE= ...
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. # make && make install
# mkdir -p /usr/local/mysql-5.6./etc
# mkdir -p /usr/local/mysql-5.6./tmp
# ln -s /usr/local/mysql-5.6./ /usr/local/mysql
# chown -R mysql:mysql /usr/local/mysql-5.6.
# 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 machinename 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./my.cnf
[mysqld] basedir=/usr/local/mysql-5.6.
datadir=/data/mysql/data
socket=/usr/local/mysql-5.6./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 &

[]
[root@machinename ~]# :: mysqld_safe Logging to '/data/mysql/data/centos.err'.
:: 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
Server version: 5.6. Source distribution Copyright (c) , , 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 0.0 0.2 pts/ S : : /bin/sh /usr/local/mysql/bin/mysqld_safe --user=mysql --skip-grant-tables --skip-networking
mysql 0.1 18.0 pts/ Sl : : /usr/local/mysql-5.6./bin/mysqld --basedir=/usr/local/mysql-5.6. --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql-5.6./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./tmp/mysql.sock
root 0.0 0.1 pts/ S+ : : grep mysql # kill -
# kill -

或者回到刚才启动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./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 |
| :: | root |
| machinename | |
| machinename | 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安装MySQL-5.6.10+安全配置的更多相关文章

  1. centOS安装Mysql指南

    centOS安装Mysql指南 说明:使用操作系统centOS6.4 32位系统:mysql:mysql-5.7.10-linux-glibc2.5-i686.tar.gz; 一.准备 下载mysql ...

  2. centos安装 mysql

    centos安装 mysql 1. 下载mysqlmysql被oracle收购后现在退出了企业版和社区版本,社区版本是开源的,企业版是收费的.社区版可以下载源码也可以下载二进制文件包.源码安装比较麻烦 ...

  3. Docker安装mysql镜像并进行主从配置

    Docker安装mysql镜像并进行主从配置 1.下载需要的mysql版本镜像 docker pull mysql:5.6 2.启动mysql服务实例(基本启动) #启动主mysql docker r ...

  4. CentOs安装Mysql和配置初始密码

    mysql官网yum安装教程,地址:https://dev.mysql.com/doc/mysql-yum-repo-quick-guide/en/#repo-qg-yum-fresh-install ...

  5. 1.centOS安装Mysql

    上个星期研究了一个星期的Mysql,从今天起把学到的东西整理一下. ---------------------------------------------- mysql安装本人亲试过两种安装方式, ...

  6. 在 Centos 安装 MySQL

    MySQL是开源的数据库管理系统,通常作为LEMP(Linux, Nginx, MySQL/MariaDB, PHP/Python/Perl)技术栈的一部分,而被安装.RedHat 会害怕 Oracl ...

  7. 阿里云服务器 CentOS 安装Mysql 5.6

    下载:https://dev.mysql.com/downloads/file/?id=471181   第一步: 安装mysql5姿势是要先安装带有可用的mysql5系列社区版资源的rpm包 [ro ...

  8. centos 安装 mysql 5.6和workbench

    windows下安装mysql很简单,去官网找到.msi文件,一键安装就OK了. Centos下面安装Mysql5.6其实也是蛮简单的. 注意:centos6.5默认mysql版本是5.1的 1.添加 ...

  9. Linux CentOS 安装MySql以及搭建MySql主从复制

    前言 在之前的博客中,有过几篇都写了关于mysql在linux下的搭建教程,可能以后还会再写,但是又不想重复在写, 于是便想单独将此抽出来,单独写成一篇博客,并详细记录一些安装过程以及遇到的问题解决办 ...

  10. centos 安装mysql数据库

    在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB. 1 下载并安装MySQL官方的 Yum Re ...

随机推荐

  1. 微信小程序开发调试工具

    为了帮助开发者简单和高效地开发微信小程序,我们推出了全新的 开发者工具 ,集成了开发调试.代码编辑及程序发布等功能. 扫码登录 启动工具时,开发者需要使用已在后台绑定成功的微信号扫描二维码登录,后续所 ...

  2. Uncaught RangeError: Maximum call stack size exceeded 调试日记

    异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...

  3. PowerDesigner(数据建模)使用大全

    什么是PowerDesigner 引入百度百科的说法是: power designer是能进行数据库设计的强大的软件,是一款开发人员常用的数据库建模工具.使用它可以分别从概念数据模型(Conceptu ...

  4. Bootstrap WPF Style,Bootstrap风格的WPF样式

    简介 GitHub地址:https://github.com/ptddqr/bootstrap-wpf-style 此样式基于bootstrap-3.3.0,样式文件里的源码行数都是指的这个版本.CS ...

  5. 爬虫技术 -- 进阶学习(十)网易新闻页面信息抓取(htmlagilitypack搭配scrapysharp)

    最近在弄网页爬虫这方面的,上网看到关于htmlagilitypack搭配scrapysharp的文章,于是决定试一试~ 于是到https://www.nuget.org/packages/Scrapy ...

  6. PHP的学习--使用phar打包

    前段时间写了几个PHP的脚本,但是因为脚本的项目是基于composer安装的,给别人使用的时候不太方便,就希望能够打包成一个能直接使用的文件. 搜索了一下,发现可以使用phar打包. 假设我们有如下一 ...

  7. 关于css清除浮动,解决内容溢出的问题

    以前在布局的时候总会遇到这样的问题,比如我想让整体的内容居中,所以会这样写, .main-content{ width:960px:height:300px;margin:0px auto; } 然后 ...

  8. [转]在 .NET 中远程请求 https 内容时,发生错误:根据验证过程,远程证书无效

    该文原网址:http://www.cnblogs.com/xwgli/p/5487930.html 在 .NET 中远程请求 https 内容时,发生错误:根据验证过程,远程证书无效.   当访问 h ...

  9. c#面向对象基础技能——学习笔记(五)委托技术在开发中的应用

    委托 delegate 1.是一种全新的面向对象语言的特性: 2.开发事件驱动程序变得非常简单: 3.简化多线程难度. 理解委托:可以理解成一个方法的指针.(接收的变量是方法) 步骤: 1.声明委托, ...

  10. 关于i++引出的线程不安全性的分析以及解决措施

    Q:i++是线程安全的吗? A:如果是局部变量,那么i++是线程安全. 如果是全局变量,那么i++不是线程安全的. 理由:如果是局部变量,那么i++是线程安全:局部变量其他线程访问不到,所以根本不存在 ...