[关闭]
@yanglt7 2018-10-21T15:55:41.000000Z 字数 5095 阅读 551

【Web 集群实战】12_LNMP 之 MySQL 的安装与配置

Web集群实战


安装 MySQL 数据库

安装方式

安装步骤

1)创建 mysql 用户的账号

  1. [root@ylt001 ~]# groupadd mysql
  2. [root@ylt001 ~]# useradd -s /sbin/nologin -g mysql -M mysql
  1. [root@ylt001 ~]# tail -1 /etc/passwd
  2. mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
  3. [root@ylt001 ~]# id mysql
  4. uid=27(mysql) gid=27(mysql) groups=27(mysql)
  1. [root@ylt001 ~]# mkdir -p /home/ylt/tools
  2. # 如若已创建则无需
  3. [root@ylt001 ~]# cd /home/ylt/tools

2)获取 MySQL 软件包

注:

3) 采用二进制方式安装 MySQL

  1. [root@ylt001 tools]# tar xf mysql-5.5.61-linux-glibc2.12-x86_64.tar.gz
  2. [root@ylt001 tools]# mkdir -p /application
  3. # 如若已创建则无需
  4. [root@ylt001 tools]# mv mysql-5.5.61-linux-glibc2.12-x86_64 /application/mysql-5.5.61
  5. # 移动并更改目录名
  1. [root@ylt001 tools]# ln -s /application/mysql-5.5.61 /application/mysql
  2. [root@ylt001 tools]# ll /application
  3. total 12
  4. lrwxrwxrwx 1 mysql mysql 26 Sep 21 20:23 mysql -> /application/mysql-5.5.61/
  5. drwxr-xr-x 13 root root 4096 Sep 21 20:10 mysql-5.5.61
  6. lrwxrwxrwx 1 root root 25 Sep 17 15:15 nginx -> /application/nginx-1.14.0
  7. drwxr-xr-x 11 root root 4096 Sep 17 15:16 nginx-1.14.0

4) 初始化 MySQL 配置文件 my.cnf

  1. [root@ylt001 tools]# cd /application/mysql/
  2. [root@ylt001 mysql]# ll support-files/*.cnf
  3. -rw-r--r-- 1 7161 31415 4691 Jun 16 02:20 support-files/my-huge.cnf
  4. -rw-r--r-- 1 7161 31415 19759 Jun 16 02:20 support-files/my-innodb-heavy-4G.cnf
  5. -rw-r--r-- 1 7161 31415 4665 Jun 16 02:20 support-files/my-large.cnf
  6. -rw-r--r-- 1 7161 31415 4676 Jun 16 02:20 support-files/my-medium.cnf
  7. -rw-r--r-- 1 7161 31415 2840 Jun 16 02:20 support-files/my-small.cnf
  8. [root@ylt001 mysql]# /bin/cp support-files/my-small.cnf /etc/my.cnf

5) 初始化 MySQL 数据库文件

  1. [root@ylt001 mysql]# mkdir -p /application/mysql/data
  2. # 建立 MySQL 数据文件目录
  3. [root@ylt001 mysql]# chown -R mysql.mysql /application/mysql/
  4. [root@ylt001 mysql]# /application/mysql/scripts/mysql_install_db --basedir=/application/mysql --datadir=/application/mysql/data --user=mysql
  5. # 初始化 MySQL 数据库文件,会有很多提示信息,如果没有 ERROR 级别的错误,会有两个 OK 的字样,表示初始化成功,否则就要解决初始化的问题
  6. ...
  7. ...
  8. ...
  9. OK
  10. Filling help tables...
  11. OK
  12. To start mysqld at boot time you have to copy
  13. ...
  14. ...
  15. ...

配置并启动 MySQL 数据库

1) 设置 MySQL 启动脚本

  1. [root@ylt001 mysql]# cp support-files/mysql.server /etc/init.d/mysqld
  2. # 拷贝 MySQL 启动脚本到 MySQL 的命令路径
  3. [root@ylt001 mysql]# chmod +x /etc/init.d/mysqld
  4. # 使脚本可执行

2) MySQL 二进制默认安装路径是 /usr/local/mysqld,启动脚本里是 /usr/local/mysql 的路径都需要替换

  1. [root@ylt001 mysql]# sed -i 's#/usr/local/mysql#/application/mysql#g' /application/mysql/bin/mysqld_safe /etc/init.d/mysqld

3) 启动 MySQL 数据库

  1. [root@ylt001 mysql]# /etc/init.c/mysqld start

4) 检查 MySQL 数据库是否启动

  1. [root@ylt001 mysql]# netstat -lntup|grep mysql
  2. tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 1169/mysqld

5) 查看 MySQL 数据库启动结果日志

  1. [root@ylt001 mysql]# tail -10 /application/mysql/data/ylt001.err
  2. 180922 20:53:03 InnoDB: Completed initialization of buffer pool
  3. 180922 20:53:03 InnoDB: highest supported file format is Barracuda.
  4. 180922 20:53:03 InnoDB: Waiting for the background threads to start
  5. 180922 20:53:04 InnoDB: 5.5.61 started; log sequence number 1595685
  6. 180922 20:53:04 [Note] Server hostname (bind-address): '0.0.0.0'; port: 3306
  7. 180922 20:53:04 [Note] - '0.0.0.0' resolves to '0.0.0.0';
  8. 180922 20:53:04 [Note] Server socket created on IP: '0.0.0.0'.
  9. 180922 20:53:04 [Note] Event Scheduler: Loaded 0 events
  10. 180922 20:53:04 [Note] /application/mysql/bin/mysqld: ready for connections.
  11. Version: '5.5.61' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server (GPL)

6)设置 MySQL 开机自启动

  1. [root@ylt001 mysql]# chkconfig --add mysqld
  2. [root@ylt001 mysql]# chkconfig mysqld on
  3. [root@ylt001 mysql]# chkconfig --list mysqld
  4. mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off

7) 配置 mysql 命令的全局使用路径

  1. [root@ylt001 mysql]# echo 'export PATH=/application/mysql/bin:$PATH' >>/etc/profile
  2. [root@ylt001 mysql]# tail -1 /etc/profile
  3. export PATH=/application/mysql/bin:$PATH
  4. [root@ylt001 mysql]# source /etc/profile
  5. [root@ylt001 mysql]# echo $PATH
  6. /application/mysql/bin:/application/mysql/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

8) 登录 MySQL 测试

  1. [root@ylt001 mysql]# mysql
  2. mysql> show databases;
  3. # 查看当前所有的数据库
  4. mysql> select user();
  5. 查看当前的登录用户
  6. mysql> quit

MySQL 安全配置

1) 为 MySQL 的 root 用户设置密码

  1. [root@ylt001 mysql]# mysqladmin -u root password 'ylt123'
  2. # 更改默认密码
  3. [root@ylt001 mysql]# mysql
  4. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
  5. # 无法直接登录了
  6. [root@ylt001 mysql]# mysql -u root -p
  7. Enter password:
  8. Welcome to the MySQL monitor. Commands end with ; or \g.
  9. Your MySQL connection id is 2
  10. Server version: 5.5.61 MySQL Community Server (GPL)
  11. Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.
  12. Oracle is a registered trademark of Oracle Corporation and/or its
  13. affiliates. Other names may be trademarks of their respective
  14. owners.
  15. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  16. mysql>

2) 清理无用的 MySQL 用户及库

  1. mysql> select user,host from mysql.user;
  2. mysql> drop user "root"@"::1";
  3. mysql> drop user ""@"localhost";
  4. mysql> drop user ""@"ylt001";
  5. mysql> drop user "root"@"ylt001";
  6. mysql> select user,host from mysql.user;
  7. +------+-----------+
  8. | user | host |
  9. +------+-----------+
  10. | root | 127.0.0.1 |
  11. | root | localhost |
  12. +------+-----------+
  13. 2 rows in set (0.00 sec)
  14. mysql> flush privileges;
  15. mysql> drop database test;
  16. mysql> show databases;
  17. mysql> quit
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注