CentOs7 急速安装 MySQL

CentOs7 急速安装 MySQL,献给还在为装机浪费生命的同窗。node

准备工做:mysql

Centos7 最小化安装(或者更高)sql

Oracle 官网下载 MySQL 安装包 (https://dev.mysql.com/downloads/mysql/)数据库

这里演示安装 5.6 版本, 下载的包为 MySQL-5.6.37-1.el7.x86_64.rpm-bundle.tarbash

开始:服务器

把文件上传到服务器,我这是上传到 /root/ 路径,mysql 版本为 5.6.37app

[root@localhost ~]# ll
total 237944
-rw-------. 1 root root      1320 Aug 14 13:48 anaconda-ks.cfg
-rwxr-xr-x. 1 root root 243650560 Aug 14 14:28 MySQL-5.6.37-1.el7.x86_64.rpm-bundle.tar
[root@localhost ~]#

登录到服务器新建一个脚本文件ide

vi mysql_secure_installation.exp

将如下内容复制到脚本文件, 输入 :wq 退出spa

#!/usr/bin/expect

set pwd [lindex $argv 0]

spawn /usr/bin/mysql_secure_installation

expect {
  "none\):" { send "$pwd\r" }
}
expect {
  "*Y/n*" { send "y\r" }
}
expect {
  "password:" { send "123456\r" }
}
expect {
  "password:" { send "123456\r" }
}
expect {
  "*Y/n*" { send "y\r" }
}
expect {
  "*Y/n*" { send "n\r" }
}
expect {
  "*Y/n*" { send "y\r" }
}
expect {
  "*Y/n*" { send "y\r" }
}
expect {
  "*Y/n*" { send "y\r" }
}

这里设置 mysql 的默认密码为 123456, 也能够改成其余的。命令行

接下来执行安装

export MYSQL_VERSION=5.6.37-1.el7.x86_64 && \
yum install -y expect && \
yum install -y libaio.x86_64 && \
yum install -y perl perl-devel perl-Data-Dumper && \
rpm -qa|grep -i mysql|xargs rpm -e --nodeps|date && \
rpm -qa|grep -i mariadb|xargs rpm -e --nodeps|date && \
rm -rf /etc/my.cnf && \
rm -rf /usr/my.cnf && \
rm -rf /var/log/mysql && \
tar -xvf MySQL-$MYSQL_VERSION.rpm-bundle.tar && \
rpm -ivh MySQL-server-$MYSQL_VERSION.rpm && \
rpm -ivh MySQL-devel-$MYSQL_VERSION.rpm && \
rpm -ivh MySQL-client-$MYSQL_VERSION.rpm && \
mysql_install_db --user=mysql && \
service mysql start && \
expect mysql_secure_installation.exp `cat /root/.mysql_secret | awk -F '):' '{print $2}'|sed s/[[:space:]]//g` && \
cp /usr/share/mysql/my-default.cnf /etc/my.cnf && \
echo 'port=3306' >> /etc/my.cnf && \
echo 'max_allowed_packet=1G' >> /etc/my.cnf && \
echo 'default-storage-engine=innodb' >> /etc/my.cnf && \
echo 'character_set_server=utf8' >> /etc/my.cnf && \
echo 'innodb_strict_mode=0' >> /etc/my.cnf && \
echo 'lower_case_table_names=1' >> /etc/my.cnf && \
service mysql restart && \
mysql -uroot -p123456 -e "grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;";

第一行 MYSQL_VERSION 改成当前 mysql 的版本。

在命令行输入 mysql --version

[root@localhost ~]# mysql --version
mysql  Ver 14.14 Distrib 5.6.37, for Linux (x86_64) using  EditLine wrapper

这里为了方便演示远程链接先将防火墙关闭

systemctl stop firewalld && setenforce 0;

查看 ip

[root@localhost ~]# ifconfig 
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.0.33  netmask 255.255.254.0  broadcast 192.168.1.255
        inet6 fe80::386e:65ac:d4eb:679  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:8a:99:82  txqueuelen 1000  (Ethernet)
        RX packets 376181  bytes 494309622 (471.4 MiB)
        RX errors 0  dropped 2  overruns 0  frame 0
        TX packets 60061  bytes 5188268 (4.9 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
...

使用 mysql 客户端远程链接数据库, 默认密码为 123456

bingaos-MacBook-Pro:Downloads bingao$ mysql -h192.168.0.33 -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.37 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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>

链接成功。

 

解释如下作了什么事情:

使用yum安装了 expect libaio.x86_64 perl perl-devel perl-Data-Dumper

检查系统是否有安装过 mysql 若是有就删除

删除系统自带的 mariadb

删除 /etc/my.cnf /usr/my.cnf 配置文件

删除 默认数据 /var/log/mysql

rpm 安装 MySQL-server-5.6.37-1.el7.x86_64.rpm

rpm 安装 MySQL-devel-5.6.37-1.el7.x86_64.rpm

rpm 安装 MySQL-client-5.6.37-1.el7.x86_64.rpm

使用 mysql_install_db --user=mysql 命令初始化数据库

使用 /usr/bin/mysql_secure_installation 初始化数据库配置

新建了 /etc/my.cnf, 增长了几个参数 (这里能够根据我的须要修改)

max_allowed_packet=1G

default-storage-engine=innodb 

character_set_server=utf8 

innodb_strict_mode=0 

lower_case_table_names=1

受权远程链接

相关文章
相关标签/搜索