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
受權遠程鏈接