search
HomeDatabaseMysql TutorialMySQL安全大讲堂:MySQL数据库安全配置_MySQL

1、前言

MySQL 是完全网络化的跨平台关系型数据库系统,同时是具有客户机/服务器体系结构的分布式数据库管理系统。它具有功能强、使用简便、管理方便、运行速度快、安全可靠性强等优点,用户可利用许多语言编写访问MySQL 数据库的程序,特别是与PHP更是黄金组合,运用十分广泛。

由于MySQL是多平台的数据库,它的默认配置要考虑各种情况下都能适用,所以在我们自己的使用环境下应该进行进一步的安全加固。作为一个MySQL的系统管理员,我们有责任维护MySQL数据库系统的数据安全性和完整性。

MySQL数据库的安全配置必须从两个方面入手,系统内部安全和外部网络安全,另外我们还将简单介绍编程时要注意的一些问题以及一些小窍门。

2、系统内部安全

首先简单介绍一下MySQL数据库目录结构。MySQL安装好,运行了mysql_db_install脚本以后就会建立数据目录和初始化数据库。如果我们用MySQL源码包安装,而且安装目录是/usr/local/mysql,那么数据目录一般会是/usr/local/mysql/var。数据库系统由一系列数据库组成,每个数据库包含一系列数据库表。MySQL是用数据库名在数据目录建立建立一个数据库目录,各数据库表分别以数据库表名作为文件名,扩展名分别为MYD、MYI、frm的三个文件放到数据库目录中。

MySQL的授权表给数据库的访问提供了灵活的权限控制,但是如果本地用户拥有对库文件的读权限的话,攻击者只需把数据库目录打包拷走,然后拷到自己本机的数据目录下就能访问窃取的数据库。所以MySQL所在的主机的安全性是最首要的问题,如果主机不安全,被攻击者控制,那么MySQL的安全性也无从谈起。其次就是数据目录和数据文件的安全性,也就是权限设置问题。

从MySQL主站一些老的binary发行版来看,3.21.xx版本中数据目录的属性是775,这样非常危险,任何本地用户都可以读数据目录,所以数据库文件很不安全。3.22.xx版本中数据目录的属性是770,这种属性也有些危险,本地的同组用户既能读也能写,所以数据文件也不安全。3.23.xx版本数据目录的属性是700,这样就比较好,只有启动数据库的用户可以读写数据库文件,保证了本地数据文件的安全。

如果启动MySQL数据库的用户是mysql,那么象如下的目录和文件的是安全的,请注意数据目录及下面的属性:

shell>ls -l /usr/local/mysql

total 40

drwxrwxr-x 2 root root 4096 Feb 27 20:07 bin

drwxrwxr-x 3 root root 4096 Feb 27 20:07 include

drwxrwxr-x 2 root root 4096 Feb 27 20:07 info

drwxrwxr-x 3 root root 4096 Feb 27 20:07 lib

drwxrwxr-x 2 root root 4096 Feb 27 20:07 libexec

drwxrwxr-x 3 root root 4096 Feb 27 20:07 man

drwxrwxr-x 6 root root 4096 Feb 27 20:07 mysql-test

drwxrwxr-x 3 root root 4096 Feb 27 20:07 share

drwxrwxr-x 7 root root 4096 Feb 27 20:07 sql-bench

drwx------ 4 mysql mysql 4096 Feb 27 20:07 var

shell>ls -l /usr/local/mysql/var

total 8

drwx------ 2 mysql mysql 4096 Feb 27 20:08 mysql

drwx------ 2 mysql mysql 4096 Feb 27 20:08 test

shell>ls -l /usr/local/mysql/var/mysql

total 104

-rw------- 1 mysql mysql 0 Feb 27 20:08 columns_priv.MYD

-rw------- 1 mysql mysql 1024 Feb 27 20:08 columns_priv.MYI

-rw------- 1 mysql mysql 8778 Feb 27 20:08 columns_priv.frm

-rw------- 1 mysql mysql 302 Feb 27 20:08 db.MYD

-rw------- 1 mysql mysql 3072 Feb 27 20:08 db.MYI

-rw------- 1 mysql mysql 8982 Feb 27 20:08 db.frm

-rw------- 1 mysql mysql 0 Feb 27 20:08 func.MYD

-rw------- 1 mysql mysql 1024 Feb 27 20:08 func.MYI

-rw------- 1 mysql mysql 8641 Feb 27 20:08 func.frm

-rw------- 1 mysql mysql 0 Feb 27 20:08 host.MYD

-rw------- 1 mysql mysql 1024 Feb 27 20:08 host.MYI

-rw------- 1 mysql mysql 8958 Feb 27 20:08 host.frm

-rw------- 1 mysql mysql 0 Feb 27 20:08 tables_priv.MYD

-rw------- 1 mysql mysql 1024 Feb 27 20:08 tables_priv.MYI

-rw------- 1 mysql mysql 8877 Feb 27 20:08 tables_priv.frm

-rw------- 1 mysql mysql 428 Feb 27 20:08 user.MYD

-rw------- 1 mysql mysql 2048 Feb 27 20:08 user.MYI

-rw------- 1 mysql mysql 9148 Feb 27 20:08 user.frm

如果这些文件的属主及属性不是这样,请用以下两个命令修正之:

shell>chown -R mysql.mysql /usr/local/mysql/var

shell>chmod -R go-rwx /usr/local/mysql/var

用root用户启动远程服务一直是安全大忌,因为如果服务程序出现问题,远程攻击者极有可能获得主机的完全控制权。MySQL从3.23.15版本开始时作了小小的改动,默认安装后服务要用mysql用户来启动,不允许root用户启动。如果非要用root用户来启动,必须加上-user=root的参数(./safe_mysqld -user=root &)。因为MySQL中有LOAD DATA INFILE和SELECT ... INTO OUTFILE的SQL语句,如果是root用户启动了MySQL服务器,那么,数据库用户就拥有了root用户的写权限。不过MySQL还是做了一些限制的,比如LOAD DATA INFILE只能读全局可读的文件,SELECT ... INTO OUTFILE不能覆盖已经存在的文件。

本地的日志文件也不能忽视,包括shell的日志和MySQL自己的日志。有些用户在本地登陆或备份数据库的时候为了图方便,有时会在命令行参数里直接带了数据库的密码,如:

shell>/usr/local/mysql/bin/mysqldump -uroot -ptest test>test.sql

shell>/usr/local/mysql/bin/mysql -uroot -ptest

这些命令会被shell记录在历史文件里,比如bash会写入用户目录的.bash_history文件,如果这些文件不慎被读,那么数据库的密码就会泄漏。用户登陆数据库后执行的SQL命令也会被MySQL记录在用户目录的.mysql_history文件里。如果数据库用户用SQL语句修改了数据库密码,也会因.mysql_history文件而泄漏。所以我们在shell登陆及备份的时候不要在-p后直接加密码,而是在提示后再输入数据库密码。

另外这两个文件我们也应该不让它记录我们的操作,以防万一。

shell>rm .bash_history .mysql_history

shell>ln -s /dev/null .bash_history

shell>ln -s /dev/null .mysql_history

上门这两条命令把这两个文件链接到/dev/null,那么我们的操作就不会被记录到这两个文件里了。

外部网络安全

MySQL数据库安装好以后,Unix平台的user表是这样的:

mysql> use mysql;

Database changed

mysql> select Host,User,Password,Select_priv,Grant_priv from user;

+-----------+------+----------+-------------+------------+

| Host | User | Password | Select_priv | Grant_priv |

+-----------+------+----------+-------------+------------+

| localhost | root | | Y | Y |

| redhat | root | | Y | Y |

| localhost | | | N | N |

| redhat | | | N | N |

+-----------+------+----------+-------------+------------+

4 rows in set (0.00 sec)


Windows平台的user表是这样的:


mysql> use mysql;

Database changed

mysql> select Host,User,Password,Select_priv,Grant_priv from user;

+-----------+------+----------+-------------+------------+

| Host | User | Password | Select_priv | Grant_priv |

+-----------+------+----------+-------------+------------+

| localhost | root | | Y | Y |

| % | root | | Y | Y |

| localhost | | | Y | Y |

| % | | | N | N |

+-----------+------+----------+-------------+------------+

4 rows in set (0.00 sec)

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL Commands in MySQL: Practical ExamplesSQL Commands in MySQL: Practical ExamplesApr 14, 2025 am 12:09 AM

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

How does InnoDB handle ACID compliance?How does InnoDB handle ACID compliance?Apr 14, 2025 am 12:03 AM

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft