Cent OS 7下MySQL安装教程

  • 环境:Cent OS 7
  • MYSQL版本:
    • MySQL-client-5.5.60-1.sles11.x86_64.rpm
    • MySQL-server-5.5.60-1.sles11.x86_64.rpm

1、查看 Cent OS 7 是否自带 mariadb 及卸载步骤
# 1.1 查询
[[email protected] ~]# rpm -qa | grep mariadb
mariadb-libs-5.5.56-2.el7.x86_64	# 有返回值即代表安装,具体版本号可不一样

# 1.2 卸载 mariadb [失败命令]
[[email protected] ~]# rpm -e mariadb-libs-5.5.56-2.el7.x86_64
#回显如下则命令失败:
error: Failed dependencies:
    libmysqlclient.so.18()(64bit) is needed by (installed) postfix-2:2.10.1-6.el7.x86_64
     libmysqlclient.so.18(libmysqlclient_18)(64bit) is needed by (installed) postfix-2:2.10.1-6.el7.x86_64

# 1.3 强制卸载 [成功命令]
[[email protected] ~]# rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64

# 1.4 校验 mariadb 是否卸载成功 [无返回结果即代表成功]
[[email protected] ~]# rpm -qa | grep mariadb
  • 为什么要卸载mariadb?
  • 原因:安装MySQL的时候会和mariadb文件相冲突导致安装不成功。

2、新建文件夹并复制MySQL

[[email protected] ~]# mkdir /opt/mysql

# 复制
	MySQL-client-5.5.60-1.sles11.x86_64.rpm
	MySQL-server-5.5.60-1.sles11.x86_64.rpm
到 /opt/mysql 下

3、安装MySQL服务端和客户端

# 3.1 安装 MySQL 服务端 [失败命令]
[[email protected] mysql]# rpm -ivh MySQL-server-5.5.60-1.sles11.x86_64.rpm	
#回显如下则命令失败:
warning: MySQL-server-5.5.60-1.sles11.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
	aaa_base is needed by MySQL-server-5.5.60-1.sles11.x86_64
	pwdutils is needed by MySQL-server-5.5.60-1.sles11.x86_64
	usr/bin/perl is needed by MySQL-server-5.5.60-1.sles11.x86_64

# 3.2 强制安装MySQL
 [[email protected] mysql]# rpm -ivh MySQL-server-5.5.60-1.sles11.x86_64.rpm --force --nodeps

执行结果见下图:
Cent OS 7下MySQL安装教程

 [[email protected] mysql]# rpm -ivh MySQL-client-5.5.60-1.sles11.x86_64.rpm --force --nodeps

执行结果见下图:
Cent OS 7下MySQL安装教程


4、设置/修改MySQL密码

# 4.1 设置MySQL密码
[[email protected] mysql]# /usr/bin/mysql_secure_installation
Enter current password for root (enter for none): [这里直接按 Enter]
#输入密码后回显如下则命令失败,按 Ctrl+C 退出:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
# 原因:查看MySQL启动状态,可以看到MySQL服务没有打开,所以修改密码前必须先打开MySQL服务

# 4.2 开启MySQL服务
# 4.2.1 查看MySQL服务状态
[[email protected] mysql]# service mysql status
#回显如下:ERROR! MySQL is not running

# 4.2.2 开启MySQL服务
[[email protected] mysql]# service mysql start
#回显如下:Starting MySQL.. SUCCESS!

见下图:
Cent OS 7下MySQL安装教程

# 4.3 再次修改密码
# 4.3.1 
[[email protected] mysql]# /usr/bin/mysql_secure_installation
...
Enter current password for root (enter for none):[这里直接按 Enter]
...
Set root password? [Y/n] [输入 Y]
New password: [输入root用户的登录新密码]
Re-enter new password: [重复输入新密码]
Password updated successfully!
Reloading privilege tables..
 ... Success!

见下图:
Cent OS 7下MySQL安装教程

5、测试MySQL安装是否成功

[[email protected] mysql]# mysql -uroot -p
Enter password:[输入设置的root用户密码]
#回显如下,则安装及设置密码成功:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.5.60 MySQL Community Server (GPL)

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>