codecamp

20.2.1 配置Mysql服务

本书在第18章讲解过MySQL和MariaDB数据库管理系统之间的因缘和特性,也狠狠地夸奖了MariaDB数据库,但是MySQL数据库当前依然是生产环境中最常使用的关系型数据库管理系统之一,坐拥极大的市场份额,并且已经通过十几年不断的发展向业界证明了自身的稳定性和安全性。另外,虽然第18章已经讲解了基本的数据库管理知识,但是为了进一步帮助大家夯实基础,本章依然在这里整合了MySQL数据库内容,使大家在温故的同时可以知新。

在使用Yum软件仓库安装服务程序时,系统会自动根据RPM软件包中的指令集完整软件配置等工作。但是一旦选择使用源码包的方式来安装,这一切就需要自己来完成了。针对MySQL数据库来讲,我们需要在系统中创建一个名为mysql的用户,专门用于负责运行MySQL数据库。请记得要把这类账户的Bash终端设置成nologin解释器,避免黑客通过该用户登录到服务器中,从而提高系统安全性。

    [root@linuxprobe cmake-2.8.11.2]# cd ..
    [root@linuxprobe src]# useradd mysql -s /sbin/nologin

创建一个用于保存MySQL数据库程序和数据库文件的目录,并把该目录的所有者和所属组身份修改为mysql。其中,/usr/local/mysql是用于保存MySQL数据库服务程序的目录,/usr/local/mysql/var则是用于保存真实数据库文件的目录。

    [root@linuxprobe src]# mkdir -p /usr/local/mysql/var
    [root@linuxprobe src]# chown -Rf mysql:mysql /usr/local/mysql

接下来解压、编译、安装MySQL数据库服务程序。在编译数据库时使用的是cmake命令,其中,-DCMAKE_INSTALL_PREFIX参数用于定义数据库服务程序的保存目录,-DMYSQL_DATADIR参数用于定义真实数据库文件的目录,-DSYSCONFDIR则是定义MySQL数据库配置文件的保存目录。由于MySQL数据库服务程序比较大,因此编译的过程比较漫长,在此期间可以稍微休息一下。

    [root@linuxprobe src]# tar xzvf mysql-5.6.19.tar.gz
    [root@linuxprobe src]# cd mysql-5.6.19/
    [root@linuxprobe mysql-5.6.19]# cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/usr/local/mysql/var -DSYSCONFDIR=/etc
    [root@linuxprobe mysql-5.6.19]# make
    [root@linuxprobe mysql-5.6.19]# make install

为了让MySQL数据库程序正常运转起来,需要先删除/etc目录中的默认配置文件,然后在MySQL数据库程序的保存目录scripts内找到一个名为mysql_install_db的脚本程序,执行这个脚本程序并使用--user参数指定MySQL服务的对应账号名称(在前面步骤已经创建),使用--basedir参数指定MySQL服务程序的保存目录,使用--datadir参数指定MySQL真实数据库的文件保存目录,这样即可生成系统数据库文件,也会生成出新的MySQL服务配置文件。

    [root@linuxprobe mysql-5.6.19]# rm -rf /etc/my.cnf
    [root@linuxprobe mysql-5.6.19]# cd /usr/local/mysql
    [root@linuxprobe mysql]# ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/var

把系统新生成的MySQL数据库配置文件链接到/etc目录中,然后把程序目录中的开机程序文件复制到/etc/rc.d/init.d目录中,以便通过service命令来管理MySQL数据库服务程序。记得把数据库脚本文件的权限修改成755以便于让用户有执行该脚本的权限:

    [root@linuxprobe mysql]# ln -s my.cnf /etc/my.cnf 
    [root@linuxprobe mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
    [root@linuxprobe mysql]# chmod 755 /etc/rc.d/init.d/mysqld

编辑刚复制的MySQL数据库脚本文件,把第46、47行的basedir与datadir参数分别修改为MySQL数据库程序的保存目录和真实数据库的文件内容。

    [root@linuxprobe mysql]# vim /etc/rc.d/init.d/mysqld 
    ………………省略部分输出信息………………
     39 #
     40 # If you want to affect other MySQL variables, you should make your changes
     41 # in the /etc/my.cnf, ~/.my.cnf or other MySQL configuration files.
     42 
     43 # If you change base dir, you must also change datadir. These may get
     44 # overwritten by settings in the MySQL configuration files.
     45 
     46 basedir=/usr/local/mysql
     47 datadir=/usr/local/mysql/var
     48 
    ………………省略部分输出信息………………

配置好脚本文件后便可以用service命令启动mysqld数据库服务了。mysqld是MySQL数据库程序的服务名称,注意不要写错。顺带再使用chkconfig命令把mysqld服务程序加入到开机启动项中。

    [root@Linuxprobe mysql]# service mysqld start
    Starting MySQL. SUCCESS! 
    [root@linuxprobe mysql]# chkconfig mysqld on

MySQL数据库程序自带了许多命令,但是Bash终端的PATH变量并不会包含这些命令所存放的目录,因此我们也无法顺利地对MySQL数据库进行初始化,也就不能使用MySQL数据库自带的命令了。想要把命令所保存的目录永久性地定义到PATH变量中,需要编辑/etc/profile文件并写入追加的命令目录,这样当物理设备在下一次重启时就会永久生效了。如果不想通过重启设备的方式来生效,也可以使用source命令加载一下/ect/profile文件,此时新的PATH变量也可以立即生效了。

    [root@linuxprobe mysql]# vim /etc/profile
    ………………省略部分输出信息………………
     64 
     65 for i in /etc/profile.d/*.sh ; do
     66 if [ -r "$i" ]; then
     67 if [ "${-#*i}" != "$-" ]; then
     68 . "$i"
     69 else
     70 . "$i" >/dev/null
     71 fi
     72 fi
     73 done
     74 export PATH=$PATH:/usr/local/mysql/bin
     75 unset i
     76 unset -f pathmunge
    [root@linuxprobe mysql]# source /etc/profile

MySQL数据库服务程序还会调用到一些程序文件和函数库文件。由于当前是通过源码包方式安装MySQL数据库,因此现在也必须以手动方式把这些文件链接过来。

    [root@linuxprobe mysql]# mkdir /var/lib/mysql
    [root@linuxprobe mysql]# ln -s /usr/local/mysql/lib/mysql /usr/lib/mysql
    [root@linuxprobe mysql]# ln -s /tmp/mysql.sock /var/lib/mysql/mysql.sock
    [root@linuxprobe mysql]# ln -s /usr/local/mysql/include/mysql /usr/include/mysql

现在,MySQL数据库服务程序已经启动,调用的各个函数文件已经就位,PATH环境变量中也加入了MySQL数据库命令的所在目录。接下来准备对MySQL数据库进行初始化,这个初始化的配置过程与MariaDB数据库是一样的,只是最后变成了Thanks for using MySQL!

    [root@linuxprobe mysql]# 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): 此处只需按下回车键
    OK, successfully used password, moving on...
    Setting the root password ensures that nobody can log into the MySQL
    root user without the proper authorisation.
    Set root password? [Y/n] y (要为root管理员设置数据库的密码)
    New password: 输入要为root管理员设置的数据库密码
    Re-enter new password: 再输入一次密码
    Password updated successfully!
    Reloading privilege tables..
     ... Success!
    By default, a MySQL installation has an anonymous user, allowing anyone
    to log into MySQL without having to have a user account created for
    them.  This is intended only for testing, and to make the installation
    go a bit smoother.  You should remove them before moving into a
    production environment.
    Remove anonymous users? [Y/n] y (删除匿名账户)
     ... Success!
    Normally, root should only be allowed to connect from 'localhost'.  This
    ensures that someone cannot guess at the root password from the network.
    Disallow root login remotely? [Y/n] y (禁止root管理员从远程登录)
     ... Success!
    By default, MySQL comes with a database named 'test' that anyone can
    access.  This is also intended only for testing, and should be removed
    before moving into a production environment.
    Remove test database and access to it? [Y/n] y (删除test数据库并取消对其的访问权限)
     - Dropping test database...
     ... Success!
     - Removing privileges on test database...
     ... Success!
    Reloading the privilege tables will ensure that all changes made so far
    will take effect immediately.
    Reload privilege tables now? [Y/n] y (刷新授权表,让初始化后的设定立即生效)
     ... Success!
    All done!  If you've completed all of the above steps, your MySQL
    installation should now be secure.
    Thanks for using MySQL!
    Cleaning up...
20.2 LNMP动态网站架构
20.2.2 配置Nginx服务
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }