CentOs7 急速安装 MySQL,献给还在为装机浪费生命的同学。
准备工作:
Centos7 最小化安装(或者更高)
Oracle 官网下载 MySQL 安装包 (https://dev.mysql.com/downloads/mysql/)
这里演示安装 5.6 版本, 下载的包为 MySQL-5.6.37-1.el7.x86_64.rpm-bundle.tar
开始:
把文件上传到服务器,我这是上传到 /root/ 路径,mysql 版本为 5.6.37
[root@localhost ~]# lltotal 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 ~]#
登陆到服务器新建一个脚本文件
vi mysql_secure_installation.exp
将以下内容复制到脚本文件, 输入 :wq 退出
#!/usr/bin/expectset pwd [lindex $argv 0]spawn /usr/bin/mysql_secure_installationexpect { "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 --versionmysql 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=4163mtu 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 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 -pEnter password: Welcome to the MySQL monitor. Commands end with ; or \g.Your MySQL connection id is 2Server 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 itsaffiliates. Other names may be trademarks of their respectiveowners.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
授权远程连接