Ubuntu 环境下 MySQL 安装与安全优化。
安装sudo apt-get update
sudo apt-get install mysql-server mysql-client # 设置root密码
# 设置数据库目录
sudo mysql_install_db
# 移除匿名帐户,禁用root远程登录
sudo mysql_secure_installation # 回答n,y,y,y,y
设置默认字符集
中文环境下,设置 utf8 为默认字符集,防止出现乱码。$ sudo vi /etc/mysql/my.cnf
[mysqld]
collation-server = utf8_unicode_ci
init-connect = 'SET NAMES utf8'
character-set-server = utf8
:wq保存配置,重启MySQL
$ sudo service mysql restart
# 查看字符集设置
$ mysql -u root -p
show variables like 'char%';
show variables like 'collation%';
加强 MySQL 安全
迁移数据库目录
MySQL 数据库默认路径 /var/lib/mysql,实际工作中,常常需要定制数据库路径,比如 /data/mysql,或者 /opt/mysql,可以是单独的数据盘或者分区,这样有利于性能调优和保护数据安全,同时也方便进行维护。
使用 mysql_install_db 重新初始化 datadir :mkdir -p /data/mysql
chown -R mysql:mysql /data/mysql
mysql_install_db --user=mysql --basedir=/usr --datadir=/data/mysql
rm -rf /var/lib/mysql
禁用远程访问等$ sudo vi /etc/mysql/my.cnf
[mysqld]
datadir = /var/lib/mysql #数据库文件目录
bind-address = 127.0.0.1 #只允许本机访问,或
skip-networking #禁用网络(但本机可以访问)
skip-show-database #禁用SHOW DATABASES
# 可增加:
local-infile=0 #禁止加载本地文件,防止类似:SELECT load_file("/etc/passwd");
$ mysql -u root -p
use mysql
UPDATE user SET Host='localhost' WHERE Host="%";
用户名优化DROP USER ""; # 或
DELETE FROM user WHERE User="";
RENAME USER root TO new_user; # 或
update user set user="new_user" where user="root"; # 或
rename user 'root'@'localhost' to 'newAdminUser'@'localhost';
密码优化UPDATE user SET Password=PASSWORD('newPassWord') WHERE User="user"; # or
SET PASSWORD FOR 'username'@'%hostname' = PASSWORD('newpass');
select user,host,password from user;
FLUSH PRIVILEGES;
# 或
$ mysqladmin -u username -p password newpass
清空命令历史
客户端工具 mysql 会将执行的命令记录在当前用户目录下的 .mysql_history 文件中,其中可能包含密码等敏感信息。cat /dev/null > ~/.mysql_history
使用日志
MySQL 日志包括错误日志、慢查询日志、一般日志和二进制日志,默认生成错误日志。在产品环境下,要合理使用日志,避免给系统增加不必要的压力。
配置文件$ sudo vi /etc/mysql/my.cnf
log_error = /var/log/mysql/error.log
#general_log_file = /var/log/mysql/mysql.log
#log_slow_queries = /var/log/mysql/mysql-slow.log
#log_bin = /var/log/mysql/mysql-bin.log
#general_log = 1
#long_query_time = 2
#log-queries-not-using-indexes
/etc/mysql/conf.d/mysqld_safe_syslog.cnf
/etc/logrotate.d/mysql-server
查看配置sudo service mysql restart
mysql> SHOW VARIABLES LIKE '%log%';
在代码中控制SET GLOBAL general_log = 'ON';
SET GLOBAL general_log = 'OFF';
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL slow_query_log = 'OFF';
日志文件位置/var/lib/mysql/{host_name}.log
/var/lib/mysql/{host_name}.err
/var/lib/mysql/{host_name}-slow.log
/var/log/mysql.err - MySQL Error log file
/var/log/mysql.log - MySQL log file
sudo ls -l /var/log/mysql*
日志监控查看grep 'something' /var/log/mysql.err
tail -f /var/log/mysql/mysql.log
tail -f /var/log/mysql.err
tail -f /var/log/syslog
less /var/log/mysql.errmysql> SHOW VARIABLES LIKE '%ssl%';
mysql> \s
$ cat /etc/apparmor.d/usr.sbin.mysqld
...
/etc/mysql/*.pem r,
制作SSL证书sudo su -
cd /etc/mysql
openssl genrsa 2048 > ca-key.pem
openssl req -new -x509 -nodes -days 3600 \
-key ca-key.pem -out ca-cert.pem
openssl req -newkey rsa:2048 -days 3600 \
-nodes -keyout server-key.pem -out server-req.pem
openssl rsa -in server-key.pem -out server-key.pem
openssl x509 -req -in server-req.pem -days 3600 \
-CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out server-cert.pem
openssl req -newkey rsa:2048 -days 3600 \
-nodes -keyout client-key.pem -out client-req.pem
openssl rsa -in client-key.pem -out client-key.pem
openssl x509 -req -in client-req.pem -days 3600 \
-CA ca-cert.pem -CAkey ca-key.pem -set_serial 01 -out client-cert.pem
openssl verify -CAfile ca-cert.pem server-cert.pem client-cert.pem
配置服务端$ sudo vi /etc/mysql/my.cnf
[mysqld]
ssl-ca=/etc/mysql/ca-cert.pem
ssl-cert=/etc/mysql/server-cert.pem
ssl-key=/etc/mysql/server-key.pem
# 重启 MySQL
$ sudo service mysql restart
# 创建使用SSL帐号
GRANT ALL PRIVILEGES ON *.* TO 'ssluser'@'%' IDENTIFIED BY 'pass' REQUIRE SSL;
配置客户端$ sudo vi /etc/mysql/my.cnf
[client]
ssl-ca=/etc/mysql/ca-cert.pem
ssl-cert=/etc/mysql/client-cert.pem
ssl-key=/etc/mysql/client-key.pem
mysql -u ssluser -p -sss -e '\s' | grep SSL
MySQL Workbench 是 MySQL 官方提供的数据库管理工具,免费跨平台,支持数据库建模,支持 MySQL 和 MariaDB,支持通过 SSH 访问远程 MySQL,即使将 MySQL 完全配置为本地访问也没有问题。当然,可以继续使用 phpMyAdmin 管理数据库。