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

MySQL数据库安全配置_MySQL

Jun 01, 2016 pm 02:12 PM
mysql root Password Safety 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;
  +-----------+------+----------+-------------+------------+
  | 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)
  
  我们先来看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='';
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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 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)

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

RDS MySQL integration with Redshift zero ETL RDS MySQL integration with Redshift zero ETL Apr 08, 2025 pm 07:06 PM

Data Integration Simplification: AmazonRDSMySQL and Redshift's zero ETL integration Efficient data integration is at the heart of a data-driven organization. Traditional ETL (extract, convert, load) processes are complex and time-consuming, especially when integrating databases (such as AmazonRDSMySQL) with data warehouses (such as Redshift). However, AWS provides zero ETL integration solutions that have completely changed this situation, providing a simplified, near-real-time solution for data migration from RDSMySQL to Redshift. This article will dive into RDSMySQL zero ETL integration with Redshift, explaining how it works and the advantages it brings to data engineers and developers.

Do mysql need to pay Do mysql need to pay Apr 08, 2025 pm 05:36 PM

MySQL has a free community version and a paid enterprise version. The community version can be used and modified for free, but the support is limited and is suitable for applications with low stability requirements and strong technical capabilities. The Enterprise Edition provides comprehensive commercial support for applications that require a stable, reliable, high-performance database and willing to pay for support. Factors considered when choosing a version include application criticality, budgeting, and technical skills. There is no perfect option, only the most suitable option, and you need to choose carefully according to the specific situation.

How to optimize MySQL performance for high-load applications? How to optimize MySQL performance for high-load applications? Apr 08, 2025 pm 06:03 PM

MySQL database performance optimization guide In resource-intensive applications, MySQL database plays a crucial role and is responsible for managing massive transactions. However, as the scale of application expands, database performance bottlenecks often become a constraint. This article will explore a series of effective MySQL performance optimization strategies to ensure that your application remains efficient and responsive under high loads. We will combine actual cases to explain in-depth key technologies such as indexing, query optimization, database design and caching. 1. Database architecture design and optimized database architecture is the cornerstone of MySQL performance optimization. Here are some core principles: Selecting the right data type and selecting the smallest data type that meets the needs can not only save storage space, but also improve data processing speed.

How to fill in mysql username and password How to fill in mysql username and password Apr 08, 2025 pm 07:09 PM

To fill in the MySQL username and password: 1. Determine the username and password; 2. Connect to the database; 3. Use the username and password to execute queries and commands.

Query optimization in MySQL is essential for improving database performance, especially when dealing with large data sets Query optimization in MySQL is essential for improving database performance, especially when dealing with large data sets Apr 08, 2025 pm 07:12 PM

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

Understand ACID properties: The pillars of a reliable database Understand ACID properties: The pillars of a reliable database Apr 08, 2025 pm 06:33 PM

Detailed explanation of database ACID attributes ACID attributes are a set of rules to ensure the reliability and consistency of database transactions. They define how database systems handle transactions, and ensure data integrity and accuracy even in case of system crashes, power interruptions, or multiple users concurrent access. ACID Attribute Overview Atomicity: A transaction is regarded as an indivisible unit. Any part fails, the entire transaction is rolled back, and the database does not retain any changes. For example, if a bank transfer is deducted from one account but not increased to another, the entire operation is revoked. begintransaction; updateaccountssetbalance=balance-100wh

How to copy and paste mysql How to copy and paste mysql Apr 08, 2025 pm 07:18 PM

Copy and paste in MySQL includes the following steps: select the data, copy with Ctrl C (Windows) or Cmd C (Mac); right-click at the target location, select Paste or use Ctrl V (Windows) or Cmd V (Mac); the copied data is inserted into the target location, or replace existing data (depending on whether the data already exists at the target location).

See all articles