一、MySQL下载
- 通过官网下载:https://dev.mysql.com/downloads/mysql/ ,或者我这里是安装5.7.12,可以直接根据以下命令获得:
wegt http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar
二、MySQL安装
1. 安装检查
输入命令 rpm -aq | grep mysql ,检查是否安装:
[root@localhost ~]# rpm -aq | grep mysql
mysql-community-common-5.7.12-1.el6.x86_64
mysql-community-libs-5.7.12-1.el6.x86_64
mysql-community-server-5.7.12-1.el6.x86_64
mysql-community-client-5.7.12-1.el6.x86_64
这里表示已经安装,则需要先卸载数据库(没有安装的可以跳过步骤2)
2. 卸载数据库
停止MySQL服务:
service mysqld start
使用命令删除数据库命令:rpm -e –nodeps 包名,输入以下命令删除数据库
rpm -ev mysql-community-common-5.7.12-1.el6.x86_64 --nodeps
rpm -ev mysql-community-libs-5.7.12-1.el6.x86_64 --nodeps
rpm -ev mysql-community-server-5.7.12-1.el6.x86_64 --nodeps
rpm -ev mysql-community-client-5.7.12-1.el6.x86_64 --nodeps
查找MySQL目录
[root@localhost ~]# find / -name mysql
/etc/logrotate.d/mysql
/var/lib/mysql
/var/lib/mysql/mysql
/usr/bin/mysql
/usr/lib64/mysql
/usr/share/mysql
/usr/local/mysql
删除MySQL目录
rm -rf /var/lib/mysql
rm -rf /var/lib/mysql/mysql
rm -rf /usr/lib64/mysql
rm -rf /usr/share/mysql
rm -rf /usr/local/mysql
删除 my.cnf
rm -rf /etc/my.cnf
3. 安装MySQL
创建MySQL安装目录
mkdir -p /usr/local/mysql
复制下载的MySQL文件到安装目录下
cp ~/download/mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar /usr/local/mysql/
解压文件
cd /usr/local/mysql/
tar -xf mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar
按顺序执行如下安装
rpm -ivh mysql-community-common-5.7.12-1.el6.x86_64.rpm
rpm -ivh mysql-community-libs-5.7.12-1.el6.x86_64.rpm
rpm -ivh mysql-community-client-5.7.12-1.el6.x86_64.rpm
rpm -ivh imysql-community-server-5.7.12-1.el6.x86_64.rpm
rpm -ivh mysql-community-devel-5.7.12-1.el6.x86_64.rpm
到此安装完成,安装完成后MySQL服务不会启动,接下来我们启动服务
4. MySQL启动
启动MySQL服务:
systemctl start mysqld.service
查看服务启动状态:
[zhaobin@localhost mysql]$ systemctl status mysqld.service
● mysqld.service - SYSV: MySQL database server.
Loaded: loaded (/etc/rc.d/init.d/mysqld; bad; vendor preset: disabled)
Active: active (running) since 二 2019-06-18 10:28:34 CST; 6h ago
Docs: man:systemd-sysv-generator(8)
Process: 3497 ExecStart=/etc/rc.d/init.d/mysqld start (code=exited, status=0/SUCCESS)
Main PID: 3833 (mysqld)
CGroup: /system.slice/mysqld.service
├─3635 /bin/sh /usr/bin/mysqld_safe --datadir=/var/lib/mysql --soc...
└─3833 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --...
[zhaobin@localhost mysql]$
Active 为running表示服务启动成功
5. 修改root密码
启动之后密码就初始化好了,通过:grep 'temporary password' /var/log/mysqld.log 查看密码
.[zhaobin@localhost mysql]$ sudo grep 'temporary password' /var/log/mysqld.log
2019-06-17T08:50:55.318532Z 1 [Note] A temporary password is generated for root@localhost: IyUZ/RY3-ra2
IyUZ/RY3-ra2 即为root的默认密码,接下来登陆MySQL 修改密码
[zhaobin@localhost mysql]$ mysql -uroot -p
Enter password: //在这里输入root的默认密码
登陆成功后,设置MySQL密码:
set password for 'root'@'localhost'='P@ssw0rd123';
到此MySQL已经成功安装。
说明:如果安装成功后MySQL无法正常启动,先根据log排查问题,无法解决根据步骤2卸载后重新安装即可。