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
total40
drwxrwxr-x2rootroot4096Feb2720:07bin
drwxrwxr-x3rootroot4096Feb2720:07include
drwxrwxr-x2rootroot4096Feb2720:07info
drwxrwxr-x3rootroot4096Feb2720:07lib
drwxrwxr-x2rootroot4096Feb2720:07libexec
drwxrwxr-x3rootroot4096Feb2720:07man
drwxrwxr-x6rootroot4096Feb2720:07mysql-test
drwxrwxr-x3rootroot4096Feb2720:07share
drwxrwxr-x7rootroot4096Feb2720:07sql-bench
drwx------4mysqlmysql4096Feb2720:07var
shell>ls-l/usr/local/mysql/var
total8
drwx------2mysqlmysql4096Feb2720:08mysql
drwx------2mysqlmysql4096Feb2720:08test
shell>ls-l/usr/local/mysql/var/mysql
total104
-rw-------1mysqlmysql0Feb2720:08columns_priv.MYD
-rw-------1mysqlmysql1024Feb2720:08columns_priv.MYI
-rw-------1mysqlmysql8778Feb2720:08columns_priv.frm
-rw-------1mysqlmysql302Feb2720:08db.MYD
-rw-------1mysqlmysql3072Feb2720:08db.MYI
-rw-------1mysqlmysql8982Feb2720:08db.frm
-rw-------1mysqlmysql0Feb2720:08func.MYD
-rw-------1mysqlmysql1024Feb2720:08func.MYI
-rw-------1mysqlmysql8641Feb2720:08func.frm
-rw-------1mysqlmysql0Feb2720:08host.MYD
-rw-------1mysqlmysql1024Feb2720:08host.MYI
-rw-------1mysqlmysql8958Feb2720:08host.frm
-rw-------1mysqlmysql0Feb2720:08tables_priv.MYD
-rw-------1mysqlmysql1024Feb2720:08tables_priv.MYI
-rw-------1mysqlmysql8877Feb2720:08tables_priv.frm
-rw-------1mysqlmysql428Feb2720:08user.MYD
-rw-------1mysqlmysql2048Feb2720:08user.MYI
-rw-------1mysqlmysql9148Feb2720:08user.frm
如果这些文件的属主及属性不是这样,请用以下两个命令修正之:
shell>chown-Rmysql.mysql/usr/local/mysql/var
shell>chmod-Rgo-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不能覆盖已经存在的文件。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. Choose the appropriate client transport protocol ActiveMQ supports a variety of client transport protocols, including STOMP, AMQP and OpenWire. Choose the right protocol based on your application needs to optimize performance and reliability. 2. Configure message persistence. Persistent messages are persisted even after server restarts, while non-persistent messages are not. For critical messages, choose persistence to ensure reliable delivery. Demo code: //Set message persistence MessageProducerproducer=session.createProducer(destination);producer.setDeliveryMode(Deliv

Embedded systems refer to applications that run on specific hardware platforms and are typically used to control, monitor, and process various devices and systems. As a powerful programming language, C++ is widely used in embedded system development. This article will introduce the basic concepts and techniques of C++ embedded system development, and how to create high-reliability embedded applications. 1. Overview of Embedded System Development Embedded system development requires a certain understanding of the hardware platform, because embedded applications need to interact directly with the hardware. In addition to hardware platforms, embedded systems

Java database connection security solution: JDBC encryption: Use SSL/TLS connection to protect data transmission security. Connection pool: reuse connections, limit resource consumption, and prevent overuse. Restrict access: Grant applications only the minimum necessary permissions to prevent data leakage. Defense against SQL injection: Use parameterized queries and input validation to defend against malicious attacks.

With the popularity of the Internet and the continuous expansion of application scenarios, we use databases more and more often in our daily lives. However, database security issues are also receiving increasing attention. Among them, SQL injection attack is a common and dangerous attack method. This article will introduce the principles, harms and how to prevent SQL injection attacks. 1. Principle of SQL injection attack SQL injection attack generally refers to the behavior of hackers executing malicious SQL statements in applications by constructing specific malicious input. These behaviors sometimes lead to

Title: Things to note when deleting database files of Dreamweaver CMS. As a popular website construction tool, the deletion of database files of Dreamweaver CMS is one of the problems often encountered in website maintenance. Incorrect database file deletion operations may result in website data loss or website failure to function properly. Therefore, we must be extremely cautious when performing database file deletion operations. The following will introduce the precautions for deleting Dreamweaver CMS database files, and provide some specific code examples to help you correctly delete database files. Note: prepare

Database Security: Strategies to Protect Java Applications from SQL Injection Attacks Summary: With the development of the Internet, Java applications play an increasingly important role in our lives and work. However, at the same time, database security issues have become increasingly prominent. SQL injection attacks are one of the most common and devastating database security vulnerabilities. This article will introduce some strategies and measures to protect Java applications from the threat of SQL injection attacks. Part 1: What is a SQL injection attack? SQL injection

How to use MySQL user rights management to protect database security Introduction MySQL is a widely used open source relational database management system. In order to protect the security of the database, MySQL provides user rights management functions. By properly setting user permissions, security control of the database can be achieved to prevent malicious operations and illegal access. This article will introduce how to use MySQL's user rights management to protect the security of the database, and provide code examples for demonstration. Create users and authorization. First, log in to MyS using the root account.

MySQL application and security project experience summary in the financial field Introduction: With the development of technology and the rapid growth of the financial industry, the application of database technology in the financial field has become more and more important. As a mature open source relational database management system, MySQL is widely used in data storage and processing by financial institutions. This article will summarize the application of MySQL in the financial field and analyze the experience and lessons learned in security projects. 1. Application of MySQL in the financial field Data storage and processing are usually required by financial institutions
