Home Database Mysql Tutorial MySQL数据库安全配置指南_MySQL

MySQL数据库安全配置指南_MySQL

Jun 01, 2016 pm 02:11 PM
mysql root Safety guide data database Table of contents Configuration


  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,那么我们的操作就不会被记录到这两个文件里了。
  
  3、外部网络安全
  
  MySQL数据库安装好以后,Unix平台的user表是这样的:
  
  mysql> use mysql;
  
  Database changed
  
  mysql> select Host,User,Password,Select_priv,Grant_priv from user;
   MySQL数据库安全配置指南_MySQL
  图1
  
  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;
   MySQL数据库安全配置指南_MySQL
  图2
  
  4 rows in set (0.00 sec)
  
  我们先来看Unix平台的user表。其中redhat只是我试验机的机器名,所以实际上Unix平台的MySQL默认只允许本机才能连接数据库。但是缺省root用户口令是空,所以当务之急是给root用户加上口令。给数据库用户加口令有三种方法:
  
  1)在shell提示符下用mysqladmin命令来改root用户口令:
  
  shell>mysqladmin -uroot password test
  
  这样,MySQL数据库root用户的口令就被改成test了。(test只是举例,我们实际使用的口令一定不能使用这种易猜的弱口令)
  
  2)用set password修改口令:
  
  mysql> set password for root@localhost=password('test');
  
  这时root用户的口令就被改成test了。
  
  3)直接修改user表的root用户口令:
  
  mysql> use mysql;
  
  mysql> update user set password=password('test') where user='root';
  
  mysql> flush privileges;
  
  这样,MySQL数据库root用户的口令也被改成test了。其中最后一句命令flush privileges的意思是强制刷新内存授权表,否则用的还是缓冲中的口令,这时非法用户还可以用root用户及空口令登陆,直到重启MySQL服务器。
  
  我们还看到user为空的匿名用户,虽然它在Unix平台下没什么权限,但为了安全起见我们应该删除它:
  
  mysql> delete from user where user='';
  
  Windows版本MySQL的user表有很大不同,我们看到Host字段除了localhost还有是%。这里%的意思是允许任意的主机连接MySQL服务器,这是非常不安全的,给攻击者造成可乘之机,我们必须删除Host字段为%的记录:
  
  mysql>de
Statement of this Website
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

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to fix mysql_native_password not loaded errors on MySQL 8.4 How to fix mysql_native_password not loaded errors on MySQL 8.4 Dec 09, 2024 am 11:42 AM

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! AI startups collectively switched jobs to OpenAI, and the security team regrouped after Ilya left! Jun 08, 2024 pm 01:00 PM

Last week, amid the internal wave of resignations and external criticism, OpenAI was plagued by internal and external troubles: - The infringement of the widow sister sparked global heated discussions - Employees signing "overlord clauses" were exposed one after another - Netizens listed Ultraman's "seven deadly sins" Rumors refuting: According to leaked information and documents obtained by Vox, OpenAI’s senior leadership, including Altman, was well aware of these equity recovery provisions and signed off on them. In addition, there is a serious and urgent issue facing OpenAI - AI safety. The recent departures of five security-related employees, including two of its most prominent employees, and the dissolution of the "Super Alignment" team have once again put OpenAI's security issues in the spotlight. Fortune magazine reported that OpenA

70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI 70B model generates 1,000 tokens in seconds, code rewriting surpasses GPT-4o, from the Cursor team, a code artifact invested by OpenAI Jun 13, 2024 pm 03:47 PM

70B model, 1000 tokens can be generated in seconds, which translates into nearly 4000 characters! The researchers fine-tuned Llama3 and introduced an acceleration algorithm. Compared with the native version, the speed is 13 times faster! Not only is it fast, its performance on code rewriting tasks even surpasses GPT-4o. This achievement comes from anysphere, the team behind the popular AI programming artifact Cursor, and OpenAI also participated in the investment. You must know that on Groq, a well-known fast inference acceleration framework, the inference speed of 70BLlama3 is only more than 300 tokens per second. With the speed of Cursor, it can be said that it achieves near-instant complete code file editing. Some people call it a good guy, if you put Curs

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

China Mobile: Humanity is entering the fourth industrial revolution and officially announced 'three plans” China Mobile: Humanity is entering the fourth industrial revolution and officially announced 'three plans” Jun 27, 2024 am 10:29 AM

According to news on June 26, at the opening ceremony of the 2024 World Mobile Communications Conference Shanghai (MWC Shanghai), China Mobile Chairman Yang Jie delivered a speech. He said that currently, human society is entering the fourth industrial revolution, which is dominated by information and deeply integrated with information and energy, that is, the "digital intelligence revolution", and the formation of new productive forces is accelerating. Yang Jie believes that from the "mechanization revolution" driven by steam engines, to the "electrification revolution" driven by electricity, internal combustion engines, etc., to the "information revolution" driven by computers and the Internet, each round of industrial revolution is based on "information and "Energy" is the main line, bringing productivity development

How to delete data from MySQL table using PHP? How to delete data from MySQL table using PHP? Jun 05, 2024 pm 12:40 PM

PHP provides the following methods to delete data in MySQL tables: DELETE statement: used to delete rows matching conditions from the table. TRUNCATETABLE statement: used to clear all data in the table, including auto-incremented IDs. Practical case: You can delete users from the database using HTML forms and PHP code. The form submits the user ID, and the PHP code uses the DELETE statement to delete the record matching the ID from the users table.

How does Go WebSocket integrate with databases? How does Go WebSocket integrate with databases? Jun 05, 2024 pm 03:18 PM

How to integrate GoWebSocket with a database: Set up a database connection: Use the database/sql package to connect to the database. Store WebSocket messages to the database: Use the INSERT statement to insert the message into the database. Retrieve WebSocket messages from the database: Use the SELECT statement to retrieve messages from the database.

See all articles