二进制格式安装MySQL5.7

文章目录

  • 一. 安装步骤
  • 二. 配置文件介绍
  • 三. 自动安装MySQL脚本


一. 安装步骤


1.下载二进制格式的mysql安装包

[root@node1 ~]# cd /usr/src
[root@node1 src]# wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz

2.创建用户和组

[root@node1 src]# useradd -r -M -s /sbin/nologin -u 306 mysql
[root@node1 src]# id mysql
uid=306(mysql) gid=306(mysql) groups=306(mysql)

3.将压缩包解压至/usr/local目录中

[root@node1 src]# tar xf mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz -C /usr/local
[root@node1 src]# cd /usr/local/
[root@node1 local]# ls
bin    include  libexec                              share
etc    lib      mysql-5.7.30-linux-glibc2.12-x86_64  src
games  lib64    sbin

4.创建软链接

[root@node1 local]# ln -sv mysql-5.7.30-linux-glibc2.12-x86_64/ mysql
‘mysql’ -> ‘mysql-5.7.30-linux-glibc2.12-x86_64/’

5.修改/usr/local/mysql目录的属主和属组

[root@node1 local]# chown -R mysql.mysql /usr/local/mysql
[root@node1 local]# ll -d /usr/local/mysql
lrwxrwxrwx. 1 mysql mysql 36 Jun 10 21:13 /usr/local/mysql -> mysql-5.7.30-linux-glibc2.12-x86_64/

6.添加环境变量

[root@node1 local]# vi /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin:$PATH   //添加此行内容 
[root@node1 local]# source /etc/profile.d/mysql.sh 
[root@node1 local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

7.建立数据库存放目录

[root@node1 local]# mkdir /opt/data
[root@node1 local]# chown -R mysql.mysql /opt/data
[root@node1 local]# ll -d /opt/data
drwxr-xr-x. 2 mysql mysql 6 Jun 10 20:42 /opt/data

8.初始化数据库
初始化后会在最后产生一个密码

[root@node1 local]# mysqld --initialize --user=mysql --datadir=/opt/data/
2020-06-11T01:21:58.867408Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-06-11T01:21:59.248356Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-06-11T01:21:59.303487Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-06-11T01:21:59.363452Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: f25e8a4f-ab81-11ea-b68c-000c2997c366.
2020-06-11T01:21:59.366177Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-06-11T01:22:00.388432Z 0 [Warning] CA certificate ca.pem is self signed.
2020-06-11T01:22:01.151903Z 1 [Note] A temporary password is generated for root@localhost: kR+arfck*9qa  //kR+arfck*9qa为数据库root账户登录密码

9.生成配置文件

//这里可以直接覆盖my.cnf文件
[root@node1 ~]# vim /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql   //数据库安装路径
datadir = /opt/data          //数据存放路径
socket = /tmp/mysql.sock    //套接字文件存放路径
port = 3306                      //端口号
pid-file = /opt/data/mysql.pid   //进程ID存放路径
user = mysql                 //启动用户
skip-name-resolve            //跳过名称解析,连接时使用IP

10.配置服务启动脚本

[root@node1 ~]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@node1 ~]# vim /etc/init.d/mysqld
//修改下面两行内容
basedir=/usr/local/mysql
datadir=/opt/data

11.启动mysql

[root@node1 ~]# service mysqld start
Starting MySQL.Logging to '/opt/data/node1.err'.
 SUCCESS! 
[root@node1 ~]# ss -antl |grep 3306
LISTEN     0      80          :::3306                    :::*

12.修改密码
使用第8步产生的密码登录数据库,进行密码的修改

[root@node1 ~]# mysql -uroot -p'kR+arfck*9qa'
mysql> set password = password('123');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> exit
Bye

[root@node1 ~]# mysql -uroot -p123
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> 

二. 配置文件介绍


mysql主配置文件为/etc/my.cnf

配置文件查找次序:

/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FILE --> ~/.my.cnf

若在多个配置文件中均有设定,则最后找到的最终生效

mysql配置文件常用参数:

port

服务监听端口,默认为3306

socket

指定套接字文件路径,程序使用套接字链接比使用”域名:端口“链接要快, 因为这样就不需要协议解析了

basedir

指定mysql的安装路径

datadir

指定mysql数据存放路径

pid-file

指定进程ID文件存放路径

user

指定mysql以什么用户的身份提供服务

skip-name-resolve

禁止MySQL对外部连接进行DNS解析
使用这一选项可以消除MySQL进行DNS解析的时间。
若开启该选项,则所有远程主机连接授权都要使用IP地址方
式否则MySQL将无法正常处理连接请求


三. 自动安装MySQL脚本


需要将二进制格式的mysql压缩包和脚本放入同一个目录中,然后执行脚本进行安装

[root@node1 ~]# ls mysql_script/
mysql-5.7.30-linux-glibc2.12-x86_64.tar.gz  script.sh

[root@node1 ~]# cat mysql_script/script.sh
#!/bin/bash

#以下变量可按需修改:
#定义二进制压缩包的名称
install=mysql-5.7.30-linux-glibc2.12-x86_64
#定义数据库安装路径
basedir=/usr/local
#定义数据存放目录
datadir=/opt/data
#定义数据库登录密码
pass=123

#判断是否安装libaio
rpm -qa |grep libaio-devel &>/dev/null
if [ $? -ne 0 ];then
    mv /etc/yum.repos.d/* /tmp
    curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
    sed -i 's/$releasever/7/g' /etc/yum.repos.d/CentOS-Base.repo
    sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
    yum clean all &>/dev/null
    yum -y install libaio-devel &>/dev/null
fi

#创建用户和组
id mysql &>/dev/null
if [ $? -ne 0 ];then
    useradd -r -M -s /sbin/nologin -u 306 mysql
fi

#解压
if [ ! -d $basedir/$install ];then
    tar xf ${install}.tar.gz -C $basedir
fi

#创建软连接
ln -s $basedir/$install $basedir/mysql &>/dev/null

#修改属主和属组
chown -R mysql.mysql $basedir/mysql*

#添加环境变量
echo "export PATH=$basedir/mysql/bin:\$PATH" > /etc/profile.d/mysql.sh

#建立数据库存放目录,修改属主和属组
if [ ! -d $datadir ];then
    mkdir -p $datadir
fi
chown -R mysql.mysql $datadir

#初始化数据库
line=$(ls $datadir |wc -l)
if [ $line -eq 0 ];then
    $basedir/mysql/bin/mysqld --initialize --user=mysql --datadir=$datadir &> /root/passwd
fi

#取出密码
passwd=$(grep 'password' /root/passwd |awk '{print $NF}') 

#生成配置文件
if [ $line -eq 0 ];then
cat > /etc/my.cnf <<EOF
[mysqld]
basedir = $basedir/mysql
datadir = $datadir
socket = /tmp/mysql.sock
port = 3306
pid-file = $datadir/mysql.pid
user = mysql
skip-name-resolve
EOF
fi

#配置服务启动脚本
if [ ! -f /etc/init.d/mysqld ];then
    cp -a $basedir/mysql/support-files/mysql.server /etc/init.d/mysqld
    sed -ri "s#^(basedir=).*#\1${basedir}/mysql#g" /etc/init.d/mysqld
    sed -ri "s#^(datadir=).*#\1$datadir#g" /etc/init.d/mysqld
fi

#添加执行权限
chmod +x /etc/init.d/mysqld

#启动数据库
service mysqld start &>/dev/null

#修改密码
$basedir/mysql/bin/mysql --connect-expired-password -uroot -p${passwd} -e "set password=password('$pass')" &>/dev/null

#判断数据库是否安装成功
$basedir/mysql/bin/mysql -uroot -p${pass} -e 'show databases' &>/dev/null
if [ $? -eq 0 ];then
    echo "数据库安装完成,登录密码为$pass"
else
    echo "数据库安装失败"
fi

你可能感兴趣的:(数据库)