MYSQL两种方式安装及其区别

安装MYSQL首先要有MYSQL的安装包,MYSQL的安装包有3种:
1、RPM包 --------(掌握)
2、源码 TAR包 ------- 需要编译 (了解)
3、源码 二进制TAR包 又称二进制glibc ---------- 无需编译(掌握)

我主要说一下RPM和源码 二进制TAR包 的区别,因为源码 TAR包 在安装过程中需要编译,安装过程很繁琐,我们一般用其余两种安装包就够了。

区别如下:

1、RPM:

  • 安装简单,通过yum install 安装时间长

  • 安装目录是默认的,不用我们去改配置文件 var/lib/mysql

  • 安装好RPM包后,启动MYSQL服务,在日志文件里可以找到临时密码进行登录MYSQL后就可以修改密码。

  • 如果没有现成的安装包,可以去官网下载,进入下载页面后,

  • 操作系统选择 Red Hat Enterprise Linux / Oracle Linux ,

  • 版本选择Red Hat Enterprise Linux 6 / Oracle Linux 6(x86,64-bit)即可。
    如果想安装所有的RPM包,选RPM Bundle就好。 具体图如下:

在这里插入图片描述
在这里插入图片描述
2、源码:二进制glibc,

  • 时间主要耗费在解压上,解压后简单配置后直接使用
  • 解压目录一般放在/usr/local下的文件夹里,我放在/usr/local/mysql下
  • 需要修改配置文件:etc/my.cnf
  • 没有临时密码,可以通过用跳过授权表的方式登录MYSQL,然后修改密码
  • 如果没有现成的安装包,也可以去官网下载,
  • 操作系统选 Linux - Generic
  • 版本选 Linux - Generic (glibc 2.12) (x86,64-bit)就行

在这里插入图片描述

当然在安装之前要先搭建好本地yum,我其他博客有写。

RPM安装MYSQL:

1、先将所需安装的RPM包传到家目录下,然后解压到指定目录 (mysql)
(家目录下建目录 mysql)

[root@centos4 ~]# mkdir mysql
[root@centos4 ~]# tar -xf mysql-5.7.14-1.el6.x86_64.rpm-bundle.tar -C mysql

2、然后进入mysql目录,(查看一下解压的RPM包)用yum进行安装

[root@centos4 ~]# cd mysql
[root@centos4 mysql]# ll
total 449724
-rw-r--r--. 1 7155 31415  23564364 Jul 14  2016 mysql-community-client-5.7.14-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415    335140 Jul 14  2016 mysql-community-common-5.7.14-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415   3743156 Jul 14  2016 mysql-community-devel-5.7.14-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415  38949040 Jul 14  2016 mysql-community-embedded-5.7.14-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415 135106604 Jul 14  2016 mysql-community-embedded-devel-5.7.14-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415   2174860 Jul 14  2016 mysql-community-libs-5.7.14-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415   1723136 Jul 14  2016 mysql-community-libs-compat-5.7.14-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415 150252468 Jul 14  2016 mysql-community-server-5.7.14-1.el6.x86_64.rpm
-rw-r--r--. 1 7155 31415 104651840 Jul 14  2016 mysql-community-test-5.7.14-1.el6.x86_64.rpm
[root@centos4 mysql]# yum install mysql-community-*.rpm

这个安装有点慢,大约等3分钟左右安装完成
3、之后启动MySQL服务,(在第一次启动时会初始化数据库再启动数据库)
看到starting mysqld : [ok] -----表示MySQL服务启动成功

[root@centos4 mysql]# service mysqld start
Initializing MySQL database:                               [  OK  ]
Installing validate password plugin:                       [  OK  ]
Starting mysqld:                                           [  OK  ]

4、接下来就要登录mysql了,RPM包安装的mysql在日志文件里有临时密码,
(在配置文件/etc/my.cnf中可以看到日志文件在哪)
在/var/log/mysqld.log中有临时密码。

cat   /var/log/mysqld.log
..............
2019-03-19T20:03:33.805481Z 1 [Note] A temporary password is generated for root@localhost: eb?ugtn6/(:P

5、登录mysql
单机单实例(一种安装包且一个安装包)中可以用下面登录方式
(mysql -u用户名 -p密码)

注意:在用密码进行登录的时候最好给密码加上双引号,简单密码可以不加,如123。
但复杂密码一定一定要加上双引号。

[root@centos4 ~]# mysql -uroot -p"eb?ugtn6/(:P"
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.14

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> 

这样就登录了mysql
6、接下来改密码:
(注意密码要设置的复杂些,要不然不能保存,但自己一定要能记住)
设好后,退出mysql,重新登录即可。退出mysql用quit命令。

mysql>  set password=password('........');
Query OK, 0 rows affected, 1 warning (0.00 sec)

这就是RPM安装MYSQL,很简单。
接下来看

二进制glibc安装MYSQL:

1、将glibc传到家目录下,然后解压到指定目录下(/usr/local/mysql)

[root@centos4 ~]# mkdir /usr/local/mysql
[root@centos4 ~]# tar -xf mysql-5.7.15-linux-glibc2.5-x86_64.tar.gz -C /usr/local/mysql/

将/usr/local/mysql/mysql-5.7.15-linux-glibc2.5-x86_64里面的东西移动到/usr/local/mysq下,删除/usr/local/mysql/mysql-5.7.15-linux-glibc2.5-x86_64目录

[root@centos4 mysql]# mv mysql-5.7.15-linux-glibc2.5-x86_64/* .
[root@centos4 mysql]# rm -rf mysql-5.7.15-linux-glibc2.5-x86_64
[root@centos4 mysql]# ll
total 52
drwxr-xr-x.  2 root root   4096 Mar 20 03:58 bin
-rw-r--r--.  1 7161 wheel 17987 Aug 25  2016 COPYING
drwxr-xr-x.  2 root root   4096 Mar 20 03:58 docs
drwxr-xr-x.  3 root root   4096 Mar 20 03:57 include
drwxr-xr-x.  5 root root   4096 Mar 20 03:58 lib
drwxr-xr-x.  4 root root   4096 Mar 20 03:58 man
-rw-r--r--.  1 7161 wheel  2478 Aug 25  2016 README
drwxr-xr-x. 28 root root   4096 Mar 20 03:58 share
drwxr-xr-x.  2 root root   4096 Mar 20 03:58 support-files

2、配置环境变量(可配可不配,最好配,不配的话,在执行mysql时要加路径)
配置环境变量有两种方法:
第一种在 ~/.bash_profile文件中路径中添加/usr/local/mysql/bin

第二种就是将/usr/local/mysql/bin 复制到常用命令目录下如:/usr/bin 、 /root/bin等
我用第二种

[root@centos4 mysql]# cp /usr/local/mysql/bin/* /usr/bin

3、设置配置文件 /etc/my.cnf
基本字段如下:

[mysqld]
   basedir=/usr/local/mysql                         #  基本路径
    datadir=/usr/local/mysql/data                   #数据路径 (先在/usr/local/mysql建data目录)
    socket=/usr/local/mysql/data/mysql.sock         # socket文件
    # Disabling symbolic-links is recommended to prevent assorted security risks
    symbolic-links=0
    log-error=/usr/local/mysql/data/mysqld.log      #错误日志
    pid-file=/usr/local/mysql/data/mysqld.pid       #pid文件

一定要重启linux,使配置文件生效
4、创建用户和组

[root@centos4 mysql]# groupadd mysql
[root@centos4 mysql]# useradd -r -g mysql mysql

5、初始化数据库(放后台进行)
注意:这里的bin目录是/usr/local/mysql/bin

[root@centos4 bin]# mysqld --initialize  --datadir=/usr/local/mysql/data &

6、修改属主属组

chown -R mysql:mysql /usr/local/mysql/data

7、.启动数据库

[root@centos4 data]# cp  mysql.server  /etc/init.d/mysql.server
chkconfig --add mysql.serever
[root@centos4 data]# service mysql.server  start  
Starting MySQL.                                            [  OK  ]

8、因为日志里没有临时密码,所以要用跳过授权表的方式来登录mysql
先关闭mysql服务,再跳过授权表,
注意:一定要先关闭mysql

[root@centos4 data]# service mysql.server  stop 
Shutting down MySQL..                                      [  OK  ]

跳过授权表方式登录:(&表示放后台执行)

[root@centos4 bin]# mysqld_safe  --defaults-file=/etc/my.cnf  --skip-grant-tables & 

9、登录数据库(不需要密码)
注意:要指定socket文件的位置 用 -S 来指定

[root@centos4 bin]# mysql -uroot -p -S /usr/local/mysql/data/mysql.sock
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.15 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> 

这样就登录进来了
10、改密码
注意:改密码之前首先要刷新权限

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> alter user 'root'@'localhost' identified by '123';
Query OK, 0 rows affected (0.00 sec)

然后退出再用新密码重新登录就可以了。

+++++++++++++++++++++++++++++++++++++++++
+如果觉得写的对你有帮助的话,请留下你的小赞。+
+++++++++++++++++++++++++++++++++++++++++

  • 7
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值