CentOS下安装MySQL5.6.10和安全配置教程详解
注:以下所有操作都在centos 6.5 x86_64位系统下完成。
#准备工作#
在安装mysql之前,请确保已经使用yum安装了以下各类基础组件(如果系统已自带,还可以考虑yum update下基础组件):
gcc cmake openssl+openssl-devel pcre+pcre-devel bzip2+bzip2-devel libcurl+curl+curl-devel libjpeg+libjpeg-devel libpng+libpng-devel freetype+freetype-devel php-mcrypt+libmcrypt+libmcrypt-devel libxslt+libxslt-devel gmp+gmp-devel libxml2+libxml2-devel mhash ncurses+ncurses-devel xml2
然后创建mysql的用户组和用户,并且不允许登录权限:
# id mysql id: mysql:无此用户 # groupadd mysql # useradd -g mysql -s /sbin/nologin mysql # id mysql uid=500(mysql) gid=500(mysql) 组=500(mysql)
#mysql的安装#
给mysql的安装准备目录:
# mkdir -p /data/mysql/data # chown -r mysql:mysql /data/mysql
开始源码安装mysql:
# cd /usr/local/src # wget http://dev.mysql.com/get/downloads/mysql-5.6/mysql-5.6.10.tar.gz # tar zxf mysql-5.6.10.tar.gz # cd mysql-5.6.10 # cmake -dcmake_install_prefix=/usr/local/mysql-5.6.10 -dsysconfdir=/usr/local/mysql-5.6.10/etc -dmysql_unix_addr=/usr/local/mysql-5.6.10/tmp/mysql.sock -dmysql_tcp_port=3306 -dmysql_user=mysql -dmysql_datadir=/data/mysql/data -ddefault_charset=utf8 -ddefault_collation=utf8_general_ci -dwith_myisam_storage_engine=1 -dwith_innobase_storage_engine=1 -dwith_archive_storage_engine=1 -dwith_blackhole_storage_engine=1 -denabled_local_infile=1 ... cmake warning: manually-specified variables were not used by the project: mysql_user -- build files have been written to: /usr/local/src/mysql-5.6.10 # make && make install # mkdir -p /usr/local/mysql-5.6.10/etc # mkdir -p /usr/local/mysql-5.6.10/tmp # ln -s /usr/local/mysql-5.6.10/ /usr/local/mysql # chown -r mysql:mysql /usr/local/mysql-5.6.10 # chown -r mysql:mysql /usr/local/mysql
给当前环境添加mysql的bin目录:
# vim /etc/profile export mysql_home=/usr/local/mysql export path=$path:$mysql_home/bin $ source /etc/profile
执行初初始化配置脚本并创建系统自带的数据库和表:
# cd /usr/local/mysql # scripts/mysql_install_db --user=mysql --datadir=/data/mysql/data ... ok to start mysqld at boot time you have to copy support-files/mysql.server to the right place for your system please remember to set a password for the mysql root user ! to do so, start the server, then issue the following commands: ./bin/mysqladmin -u root password 'new-password' ./bin/mysqladmin -u root -h iz94mobdenkz password 'new-password' alternatively you can run: ./bin/mysql_secure_installation which will also give you the option of removing the test databases and anonymous user created by default. this is strongly recommended for production servers. see the manual for more instructions. you can start the mysql daemon with: cd . ; ./bin/mysqld_safe & you can test the mysql daemon with mysql-test-run.pl cd mysql-test ; perl mysql-test-run.pl please report any problems with the ./bin/mysqlbug script! the latest information about mysql is available on the web at http://www.mysql.com support mysql by buying support/licenses at http://shop.mysql.com warning: found existing config file ./my.cnf on the system. because this file might be in use, it was not replaced, but was used in bootstrap (unless you used --defaults-file) and when you later start the server. the new default config file was created as ./my-new.cnf, please compare it with your file and take the changes you need. warning: default config file /etc/my.cnf exists on the system this file will be read by default by the mysql server if you do not want to use this, either remove it, or use the --defaults-file argument to mysqld_safe when starting the server
注:由于mysql在启动的时候,会先去/etc/my.cnf找配置文件,如果没有找到则搜索$basedir/my.cnf,也即/usr/local/mysql-5.6.10/my.cnf,所以必须确保/etc/my.cnf没有存在,否则可能导致无法启动。
实际操作上发现系统上存在该文件,所以这里可能需要将该文件先备份改名,然后再根据上面的配置写配置文件:
# mv /etc/my.cnf /etc/my.cnf.bak # vim /usr/local/mysql-5.6.10/my.cnf [mysqld] basedir=/usr/local/mysql-5.6.10 datadir=/data/mysql/data socket=/usr/local/mysql-5.6.10/tmp/mysql.sock user=mysql sql_mode=no_engine_substitution,strict_trans_tables
修改mysql用户root的密码,这里使用mysqld_safe安全模式启动:
# mysqld_safe --user=mysql --skip-grant-tables --skip-networking & [1] 3970 [root@iz94mobdenkz ~]# 141230 19:02:31 mysqld_safe logging to '/data/mysql/data/centos.err'. 141230 19:02:32 mysqld_safe starting mysqld daemon with databases from /data/mysql/data
这个时候已经启动了mysqd_safe安全模式,另开一个窗口作为客户端连入mysql服务器:
# mysql welcome to the mysql monitor. commands end with ; or \g. your mysql connection id is 1 server version: 5.6.10 source distribution copyright (c) 2000, 2013, oracle and/or its affiliates. all rights reserved. oracle is a registered trademark of oracle corporation and/or its affiliates. other names may be trademarks of their respective owners. type 'help;' or '\h' for help. type '\c' to clear the current input statement. mysql> use mysql; mysql> update user set password=password('yourpassword') where user='root'; mysql> flush privileges; mysql> exit;
修改完毕之后使用kill把mysqld_safe进程杀死:
# ps aux | grep mysql root 3970 0.0 0.2 106308 1492 pts/1 s 19:02 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --user=mysql --skip-grant-tables --skip-networking mysql 4143 0.1 18.0 558280 90316 pts/1 sl 19:02 0:00 /usr/local/mysql-5.6.10/bin/mysqld --basedir=/usr/local/mysql-5.6.10 --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql-5.6.10/lib/plugin --user=mysql --skip-grant-tables --skip-networking --log-error=/data/mysql/data/centos.err --pid-file=/data/mysql/data/centos.pid --socket=/usr/local/mysql-5.6.10/tmp/mysql.sock root 4313 0.0 0.1 103252 836 pts/0 s+ 19:05 0:00 grep mysql # kill -9 3970 # kill -9 4143
或者回到刚才启动mysqld_safe的窗口ctrl+c将进程杀死也行。
复制服务启动脚本:
# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld # chmod +x /etc/init.d/mysqld
设置开机启动mysql服务并正常开启mysql服务(非必要项):
# chkconfig mysqld on # service mysqld usage: mysqld {start|stop|restart|reload|force-reload|status} [ mysql server options ] # service mysqld start starting mysql.
以后就可以直接通过service mysqld命令来开启/关闭mysql数据库了。
最后,建议生产环境下运行安全设置脚本,禁止root用户远程连接,移除test数据库和匿名用户等:
# /usr/local/mysql-5.6.10/bin/mysql_secure_installation note: running all parts of this script is recommended for all mysql servers in production use! please read each step carefully! in order to log into mysql to secure it, we'll need the current password for the root user. if you've just installed mysql, and you haven't set the root password yet, the password will be blank, so you should just press enter here. enter current password for root (enter for none):
注:上面输入的root密码指的是前面设置的mysql的root账户的密码。
至此,mysql数据库已经安装完毕。
#mysql的安全配置#
1、确保启动mysql不能使用系统的root账号,必须是新建的mysql账号,比如:
# mysqld_safe --user=mysql
2、mysql安装好运行初始化数据库后,默认的root账户密码为空,必须给其设置一个密码,同时保证该密码具有较高的安全性。比如:
mysql> user mysql; mysql> update user set password=password('yourpassword') where user='root'; mysql> flush privileges;
3、删除默认数据库及用户:
mysql> show databases; +--------------------+ | database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ mysql> drop daabase test; mysql> use mysql; mysql> select host,user from user; +--------------+------+ | host | user | +--------------+------+ | 127.0.0.1 | root | | ::1 | root | | centos | | | centos | root | | localhost | | | localhost | root | +--------------+------+ mysql> delete from user where not(host='localhost' and user='root'); mysql> flush privileges;
注:上面的user表中的数据可能会有所不同。
4、当开发网站连接数据库的时候,建议建立一个用户只针对某个库有update/select/delete/insert/drop table/create table等权限,减小某个项目的数据库的用户名和密码被窃取后造成其他项目受影响,比如:
mysql>create database yourdbname default charset utf8 collate utf8_general_ci; mysql>create user 'yourusername'@'localhost' identified by 'yourpassword'; mysql> grant select,insert,update,delete,create,drop privileges on yourdbname.* to 'yourusername'@localhost identified by 'yourpassword';
5、数据库文件所在的目录不允许未经授权的用户访问,需要控制对该目录的访问,比如:
# chown -r mysql:mysql /data/mysql/data # chmod -r go-rwx /data/mysql/data
以上所述是小编给大家介绍的centos下安装mysql5.6.10和安全配置教程详解,希望对大家有所帮助
推荐阅读
-
Winserver2012下mysql 5.7解压版(zip)配置安装教程详解
-
CentOS7下MySQL5.7安装配置方法图文教程(YUM)
-
CentOS7.0下安装PHP5.6.30服务的教程详解
-
CentOS安装mysql5.7 及简单配置教程详解
-
Winserver2012下mysql 5.7解压版(zip)配置安装教程详解
-
CentOS7下mysql 8.0.16 安装配置方法图文教程
-
Centos7下mysql 8.0.15 安装配置图文教程
-
CentOS7下MySQL5.7安装配置方法图文教程(YUM)
-
Win10下免安装版MySQL8.0.16的安装和配置教程图解
-
windows版本下mysql的安装启动和基础配置图文教程详解