12.2 MySQL安装

时间:2022-04-27
本文章向大家介绍12.2 MySQL安装,主要内容包括MySQL安装目录概要、搭建LNMP环境、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

MySQL安装目录概要

  • cd /usr/local/src
  • wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
  • tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
  • mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
  • cd /usr/local/mysql
  • useradd mysql
  • mkdir /data/
  • ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
  • cp support-files/my-default.cnf /etc/my.cnf
  • cp support-files/mysql.server /etc/init.d/mysqld
  • vi /etc/init.d/mysqld
    • 定义basedir和datadir
  • /etc/init.d/mysqld start

搭建LNMP环境

LAMP架构下,然后搭建LNMP架构
1.首先查看mysql是否启动
ps aux |grep mysql
2.删除目录
rm -rf /usr/local/mysql/
3.删除启动的脚本
rm -rf /etc/init.d/mysqld
4.然后其他步骤相同
  1. 这里新建一个虚拟环境(这里不是在lamp架构上搭建的,而是新建的一个环境)
  2. 然后进入到/usr/local/src目录下
[root@hanfeng ~]# cd /usr/local/src
[root@hanfeng src]#
  1. 下载mysql安装包
[root@hanfeng src]# wget http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
  1. 然后解压安装包
[root@hanfeng src]# tar zxvf mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz
  1. 移动目录并修改名称——>这里移动目录到/usr/local/下时,一定不能有mysql目录,(若已经有mysql目录时,再去移动则会放到mysql目录下面去,而不是去移动并修改名称了)
[root@hanfeng src]# mv mysql-5.6.35-linux-glibc2.5-x86_64 /usr/local/mysql
[root@hanfeng src]# 
  1. 移动完后,检查目录
[root@hanfeng src]# ls /usr/local/mysql
bin      data  include  man         README   share      support-files
COPYING  docs  lib      mysql-test  scripts  sql-bench
[root@hanfeng src]# 
  1. 进入到/usr/local/mysql目录下
[root@hanfeng src]# cd /usr/local/mysql
[root@hanfeng mysql]# 
  1. 新建mysql用户和/data/目录——>这里若是在lamp之前的基础上做的话,需要rm -rf /data/mysql/*清空内容,(直接删除mysql目录也可以。它会自动创建)
[root@hanfeng mysql]# useradd mysql
[root@hanfeng mysql]# mkdir /data/
[root@hanfeng mysql]# 
  1. 初始化./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
  • 初始化的过程目的,就是把mysql启动所需要的目录生成
[root@hanfeng mysql]# ./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
  • 错误
FATAL ERROR: please install the following Perl modules before executing ./scripts/mysql_install_db:
Data::Dumper
  • 解决方法
yum install -y perl-Data-Dumper
  • 错误
Installing MySQL system tables..../bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
  • 解决方法
yum install -y libaio
  1. 然后可以echo $?检查,或者看初始化的过程中是否有两个OK
[root@hanfeng mysql]# echo $?
0
[root@hanfeng mysql]# 
  1. 拷贝配置文件cp support-files/my-default.cnf /etc/my.cnf
  1. 拷贝文件cp support-files/mysql.server /etc/init.d/mysqld
[root@hanfeng mysql]# cp support-files/mysql.server /etc/init.d/mysqld
  1. 编辑文件 /etc/init.d/mysqld,并配置
[root@hanfeng mysql]# vim /etc/init.d/mysqld

在文件中配置
basedir=/usr/local/mysql
datadir=/data/mysql

然后保存退出
  1. 启动mysql服务
[root@hanfeng mysql]# /etc/init.d/mysqld start
Starting MySQL.Logging to '/data/mysql/hanfeng.err'.
. SUCCESS! 
[root@hanfeng mysql]# 
  1. 查看服务是否启动成功
[root@hanfeng mysql]# ps aux |grep mysql
root      2295  0.0  0.1 113252  1608 pts/0    S    22:41   0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/hanfeng.pid
mysql     2403  1.9 44.6 973512 451180 pts/0   Sl   22:41   0:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/hanfeng.err --pid-file=/data/mysql/hanfeng.pid
root      2429  0.0  0.0 112656   992 pts/0    R+   22:41   0:00 grep --color=auto mysql
[root@hanfeng mysql]# 
  1. 将mysql服务加入到服务列表中去,并设置开机启动
[root@hanfeng mysql]# chkconfig --add mysqld
[root@hanfeng mysql]# chkconfig mysqld on
[root@hanfeng mysql]# 
  1. 下次就可以直接使用service关闭或启动服务
[root@hanfeng mysql]# service mysqld stop
Shutting down MySQL.. SUCCESS! 
[root@hanfeng mysql]# service mysqld start
Starting MySQL. SUCCESS!