mysql5.7安装

整理一下mysql5.7的安装,安装基本与之前的mysql5.6保持一致,但是会有一些不同,来介绍一下!

整理一下安装mysql的步骤,大致如下:

  • 解压压缩包(二进制免编译压缩包)
  • 修改压缩包名字
  • 创建mysql用户和用户组(组也可以不创建)
  • 创建datadir目录
  • 初始化数据库
  • 修改启动脚本文件,5.7.22版本好像没有默认配置文件。
  • 启动mysql

安装过程如下:

#解压文件包
tar zxvf mysql-5.7.22-linux-glibc2.12-x86_64.tar.gz
#修改文件包名字并移动位置
mv mysql-5.7.22-linux-glibc2.12-x86_64 /usr/local/mysql
#创建mysql用户
useradd -M mysql
#创建数据库目录
mkdir /data/mysql
#修改mysql目录的用户主和用户组
chown -R mysql:mysql /data/mysql
#安装mysql
[root@test3 mysql]# ./bin/mysql_install_db --user=mysql --datadir=/data/mysql
2018-07-18 19:21:28 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
#报警告提示,使用如下方式初始化
[root@test3 mysql]# ./bin/mysqld --initialize --user=mysql --datadir=/data/mysql
./bin/mysqld: error while loading shared libraries: libnuma.so.1: cannot open shared object file: No such file or directory
#安装时报错,可以使用安装如下文件包
yum -y install numactl.x86_64
#初始化如下
[root@test3 mysql]# ./bin/mysqld --initialize --user=mysql --datadir=/data/mysql
2018-07-18T11:24:17.639719Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2018-07-18T11:24:18.757337Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-07-18T11:24:18.939043Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2018-07-18T11:24:19.007577Z 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: 1c981b1a-8a7d-11e8-98d1-fa336351fc00.
2018-07-18T11:24:19.009188Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2018-07-18T11:24:19.010028Z 1 [Note] A temporary password is generated for root@localhost: ASB=D_(8zN?d
#注意最后一行,有一个临时密码设置
#拷贝启动脚本
cp -p support-files/mysql.server /etc/init.d/mysqld
#修改mysqld文件
sed -i "s/^datadir=/datadir=\/data\/mysql/" /etc/init.d/mysqld
#启动mysql
[root@test3 mysql]# service mysqld start
Starting MySQL.Logging to '/data/mysql/test3.err'.
 SUCCESS! 
#然后进入shell交互界面
[root@test3 mysql]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.22

Copyright (c) 2000, 2018, 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>show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
#mysql5.7要求必须更改密码,然后才可以正常使用。
mysql> alter user "root"@"localhost" identified by "123qwE#!";
Query OK, 0 rows affected (0.00 sec)
#mysql已经可以正常使用
mysql> show databases; 
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

mysql>

新给定的临时密码是有一定的期限的,若过长时间不修改密码,再登录,则会出现如下错误!

[root@test1 mysql]# mysql -uroot -p
Enter password: 
ERROR 1862 (HY000): Your password has expired. To log in you must change it using a client that supports expired passwords.
[root@test1 mysql]#

这时候可以使用mysqladmin修改密码,如下:

[root@test1 mysql]# ./bin/mysqladmin -uroot -p password
Enter password: 
New password: 
Confirm new password: 
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
[root@test1 mysql]#

在初始化的时候,若是不想mysql自己设置密码,可以使用参数--initialize-inscure来初始化。

mysqld --initialize-insecure --datadir=/data/mysql --user=mysql

需要注意一点的是,mysql5.7在安装时会自动在/etc/my.cnf下创建配置文件,因此不需要再拷贝,但是需要修改数据库文件中目录位置,配置文件如下:

[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/data/mysql/test.log
pid-file=/var/run/mysqld/mysqld.pid
mysql5.7默认配置文件

mysql5.7的安装脚本如下:

#!/bin/bash
#mysql5.7安装脚本
mysql_v=mysql-5.7.23-linux-glibc2.12-x86_64
print_str() {
    echo -e "\033[31;33;1m $1 \033[0m"
}

check_ok() {
    if [ $? != 0 ]; then
        echo "Some error happend, please check it"
    fi       
}

print_str "判断mysql安装包是否存在"
sleep 1
cd /usr/local/src
mysql_file=${mysql_v}.tar.gz
if [ -f ${mysql_file} ]; then
    print_str "解压安装包"
    tar -zxvf ${mysql_file}
    check_ok
    print_str "移动备份文件"
    mv ${mysql_v} /usr/local/mysql
    check_ok
else
   print_str "请下载数据库包文件" 
fi  
sleep 2

print_str "关闭selinux和iptables"
#关闭防火墙
iptables -F
service iptables save
#关闭selinux
selinux1=`getenforce`
if [ $selinux1 != 0 ];then
    setenforce 0
    sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
    check_ok
fi
sleep 2

print_str "创建mysql用户"
if ! grep -q "mysql" /etc/passwd; then
    useradd -M mysql
    check_ok
else
   print_str "mysql用户已经存在"
fi
sleep 2

print_str "创建数据库目录"
if [ -d /data/mysql ]; then
    #删除原来的目录,或者备份原来的目录,这里选择备份
    mv /data/mysql /data/mysql.bak
    mkdir -p /data/mysql
else
    mkdir -p /data/mysql 
fi
sleep 2

print_str "更改目录权限"
chown -R mysql:mysql /data/mysql
check_ok

print_str "检查是否安装gcc编译器和依赖包"
for pak in gcc numactl libaio libaio-devel pcre-devel zlib-devel
do
    if ! rpm -qa ${pak} >> /dev/null; then
        print_str ${pak} has installed!!
    else
       yum install -y ${pak} >& /dev/null
       sleep 1
       check_ok
    fi
done

print_str "初始化数据库"
#mysql5.7的初始化命令之前版本的初始化命令不一样
cd /usr/local/mysql
./bin/mysqld --initialize --user=mysql --datadir=/data/mysql >& /tmp/mysql.txt
check_ok
sleep 2
pwd=`grep "temporary password" /tmp/mysql.txt  |awk '{print $NF}'`
check_ok
#mysql5.7初始化的时候会自动设置一个密码,这里我们提取出这个密码
print_str "数据库的初始化密码为 ${pwd}"

sleep 2
print_str "设置启动脚本"
cp -p support-files/mysql.server /etc/init.d/mysqld
check_ok
sed -i "s/^datadir=/datadir=\/data\/mysql/" /etc/init.d/mysqld
check_ok
sleep 2

print_str "修改数据库配置文件"
#mysql5.7会在/etc/下面创建my.cnf配置文件,因此不需要再拷贝,但是需要修改其文件目录
#若是没有配置文件可以自己拷贝一个,
if [ -f /etc/my.cnf ]; then
    mv /etc/my.cnf  /etc/my.cnf.bak
fi

#默认配置文件有很少的配置,我们直接写入
cat > /etc/my.cnf <<EOF
[mysqld]
datadir=/data/mysql
socket=/data/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/data/mysql/test.log
pid-file=/var/run/mysqld/mysqld.pid
EOF

#启动数据库,并且添加环境变量
service mysqld start
check_ok
echo PATH=$PATH:/usr/local/mysql/bin >> /etc/profile.d/mysql.sh
source /etc/profile
check_ok
#删除中间文件
rm -f /tmp/mysql.txt

echo -e "\033[31;35;1m mysql5.7需要强制修改密码,请尽快修改密码\033[0m"
mysql5.7安装脚本

 

posted @ 2018-07-18 20:42  夜间独行的浪子  阅读(390)  评论(0编辑  收藏  举报