Linux 下MySQL 安装之yum命令安装

一般来说使用yum安装软件,只需要直接yum install即可,但是我们最好在安装之前做一下安装检测,检测是否已经安装了该软件,我们在安装MySQL之前需要检测是否已经安装了MySQL,是否有MySQL在运行中。检测命令如下:

[root@192 ~]# ps -ef|grep mysql
mysql      2406      1  0 04:07 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql      2578   2406  0 04:07 ?        00:00:01 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root       2865   2361  0 04:09 pts/0    00:00:00 grep --color=auto mysql

如上所示,我们检测到系统已经安装了MySQL数据库,因此我们需要停止该数据库服务,并且卸载重新安装。停止数据库服务时,我们可以使用 service mysqld stop或者/bin/systemctl stop  mysqld.service命令停止MySQL。在卸载之前我们可以通过下面的命令查看MySQL的安装包。

[root@192 ~]# rpm -qa | grep -i mysql

mysql-community-libs-5.6.36-2.el7.x86_64
mysql-community-release-el7-5.noarch
mysql-community-client-5.6.36-2.el7.x86_64
mysql-community-server-5.6.36-2.el7.x86_64
mysql-community-common-5.6.36-2.el7.x86_64

如上所示,列举出了6个安装包,其中一个为yum源,我们只需要将yum源以外的安装包通过 rmp -ev命令卸载即可,如下为卸载MySQL的命令:

[root@192 ~]# rpm -ev mysql-community-server-5.6.36-2.el7.x86_64
Preparing packages...
mysql-community-server-5.6.36-2.el7.x86_64
[root@192 ~]# rpm -ev mysql-community-client-5.6.36-2.el7.x86_64
Preparing packages...
mysql-community-client-5.6.36-2.el7.x86_64

[root@192 ~]# rpm -ev mysql-community-libs-5.6.36-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

碰见依赖错误的问题使用下面的命令
[root@192 ~]# rpm -ev mysql-community-libs-5.6.36-2.el7.x86_64 --nodeps
Preparing packages...
mysql-community-libs-5.6.36-2.el7.x86_64

[root@192 ~]# rpm -ev mysql-community-common-5.6.36-2.el7.x86_64
Preparing packages...
mysql-community-common-5.6.36-2.el7.x86_64

[root@192 ~]# rpm -ev mysql-community-release-el7-5.noarch
Preparing packages...
mysql-community-release-el7-5.noarch

yum命令是最好用的软件安装工具,一般都会使用yum安装,我们先使用yum安装MySQL,后面介绍其他安装MySQL的不同方式
有时我们使用yum安装MySQL会显示下面的安装信息。

[root@192 ~]# yum install mysql
Loaded plugins: fastestmirror, langpacks
base                                                                                                                                                                    | 3.6 kB  00:00:00     
extras                                                                                                                                                                  | 3.4 kB  00:00:00     
updates                                                                                                                                                                 | 3.4 kB  00:00:00     
Loading mirror speeds from cached hostfile
 * base: mirrors.yun-idc.com
 * extras: mirrors.zju.edu.cn
 * updates: mirrors.zju.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package mariadb.x86_64 1:5.5.52-1.el7 will be installed
--> Processing Dependency: mariadb-libs(x86-64) = 1:5.5.52-1.el7 for package: 1:mariadb-5.5.52-1.el7.x86_64
--> Running transaction check
---> Package mariadb-libs.x86_64 1:5.5.52-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================================================================
 Package                                          Arch                                       Version                                            Repository                                Size
===============================================================================================================================================================================================
Installing:
 mariadb                                          x86_64                                     1:5.5.52-1.el7                                     base                                     8.7 M
Installing for dependencies:
 mariadb-libs                                     x86_64                                     1:5.5.52-1.el7                                     base                                     761 k

Transaction Summary
===============================================================================================================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 9.5 M
Installed size: 53 M
Is this ok [y/d/N]: 

MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,但是这并不是我们想要安装的MySQL。这时候我们需要安装自己的MySQL源。我的MySQL源mysql-community-release-el7-5.noarch.rpm上传到home目录。然后安装该yum源。

[root@192 Downloads]# rpm -ivh mysql-community-release-el7-5.noarch.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql-community-release-el7-5    ################################# [100%]

在安装yum源之后,我们就可以通过yum install 安装mysql数据库了,如下为yum 安装mySql的相关信息:

[root@192 ~]# yum install mysql
Loaded plugins: fastestmirror, langpacks
mysql-connectors-community                                                                                                                                              | 2.5 kB  00:00:00     
mysql-tools-community                                                                                                                                                   | 2.5 kB  00:00:00     
mysql56-community                                                                                                                                                       | 2.5 kB  00:00:00     
mysql-tools-community/x86_64/primary_db                                                                                                                                 |  33 kB  00:00:00     
Loading mirror speeds from cached hostfile
 * base: mirrors.yun-idc.com
 * extras: mirrors.zju.edu.cn
 * updates: mirrors.zju.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-client.x86_64 0:5.6.36-2.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 5.6.10 for package: mysql-community-client-5.6.36-2.el7.x86_64
--> Running transaction check
---> Package mysql-community-libs.x86_64 0:5.6.36-2.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) >= 5.6.10 for package: mysql-community-libs-5.6.36-2.el7.x86_64
--> Running transaction check
---> Package mysql-community-common.x86_64 0:5.6.36-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================================================================
 Package                                              Arch                                 Version                                       Repository                                       Size
===============================================================================================================================================================================================
Installing:
 mysql-community-client                               x86_64                               5.6.36-2.el7                                  mysql56-community                                19 M
Installing for dependencies:
 mysql-community-common                               x86_64                               5.6.36-2.el7                                  mysql56-community                               257 k
 mysql-community-libs                                 x86_64                               5.6.36-2.el7                                  mysql56-community                               2.0 M

Transaction Summary
===============================================================================================================================================================================================
Install  1 Package (+2 Dependent packages)

Total download size: 22 M
Installed size: 100 M
Is this ok [y/d/N]: 
我们选择y安装MySQL
Is this ok [y/d/N]: y
Downloading packages:
(1/3): mysql-community-common-5.6.36-2.el7.x86_64.rpm                                                                                                                   | 257 kB  00:00:00     
(2/3): mysql-community-libs-5.6.36-2.el7.x86_64.rpm                                                                                                                     | 2.0 MB  00:00:00     
(3/3): mysql-community-client-5.6.36-2.el7.x86_64.rpm                                                                                                                   |  19 MB  00:00:12     
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                          1.7 MB/s |  22 MB  00:00:12     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
** Found 5 pre-existing rpmdb problem(s), 'yum check' output follows:
ipa-client-4.4.0-12.el7.centos.x86_64 has installed conflicts freeipa-client: ipa-client-4.4.0-12.el7.centos.x86_64
ipa-client-common-4.4.0-12.el7.centos.noarch has installed conflicts freeipa-client-common: ipa-client-common-4.4.0-12.el7.centos.noarch
ipa-common-4.4.0-12.el7.centos.noarch has installed conflicts freeipa-common: ipa-common-4.4.0-12.el7.centos.noarch
2:postfix-2.10.1-6.el7.x86_64 has missing requires of libmysqlclient.so.18()(64bit)
2:postfix-2.10.1-6.el7.x86_64 has missing requires of libmysqlclient.so.18(libmysqlclient_18)(64bit)
  Installing : mysql-community-common-5.6.36-2.el7.x86_64                                                                                                                                  1/3 
  Installing : mysql-community-libs-5.6.36-2.el7.x86_64                                                                                                                                    2/3 
  Installing : mysql-community-client-5.6.36-2.el7.x86_64                                                                                                                                  3/3 
  Verifying  : mysql-community-common-5.6.36-2.el7.x86_64                                                                                                                                  1/3 
  Verifying  : mysql-community-libs-5.6.36-2.el7.x86_64                                                                                                                                    2/3 
  Verifying  : mysql-community-client-5.6.36-2.el7.x86_64                                                                                                                                  3/3 

Installed:
  mysql-community-client.x86_64 0:5.6.36-2.el7                                                                                                                                                 

Dependency Installed:
  mysql-community-common.x86_64 0:5.6.36-2.el7                                                    mysql-community-libs.x86_64 0:5.6.36-2.el7                                                   

Complete!

安装数据库之后,如果我们使用mysql -uroot -p命令报下面的错误,ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)。则说明MySQL并没有启动会,我们需要使用命令service mysqld start启动MySQL,命令如下:

[root@192 ~]# service mysqld start
Redirecting to /bin/systemctl start  mysqld.service
Failed to start mysqld.service: Unit not found.
[root@192 ~]# /bin/systemctl start  mysqld.service
Failed to start mysqld.service: Unit not found.

我们看到上面启动MySql报错了,说找不到mysqld服务,这种一般说明我们只安装了mySQL客户端,因此这时候需要我们安装mysql-server。

[root@192 ~]# yum install mysql-server
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.yun-idc.com
 * extras: mirrors.zju.edu.cn
 * updates: mirrors.zju.edu.cn
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-server.x86_64 0:5.6.36-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================================================================================================================================
 Package                                              Arch                                 Version                                       Repository                                       Size
===============================================================================================================================================================================================
Installing:
 mysql-community-server                               x86_64                               5.6.36-2.el7                                  mysql56-community                                59 M

Transaction Summary
===============================================================================================================================================================================================
Install  1 Package

Total download size: 59 M
Installed size: 251 M
Is this ok [y/d/N]: y
Downloading packages:
mysql-community-server-5.6.36-2.el7.x86_64.rpm                                                                                                                          |  59 MB  00:00:38     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : mysql-community-server-5.6.36-2.el7.x86_64                                                                                                                                  1/1 
  Verifying  : mysql-community-server-5.6.36-2.el7.x86_64                                                                                                                                  1/1 

Installed:
  mysql-community-server.x86_64 0:5.6.36-2.el7                                                                                                                                                 

Complete!

安装之后,我们可以启动MySQL服务了,启动命令前面已经贴出,这里再次贴出,并且查看MySQL进程。

[root@192 ~]# /bin/systemctl start  mysqld.service
[root@192 ~]# ps -ef|grep mysql
mysql      3518      1  0 04:44 ?        00:00:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql      3684   3518  0 04:44 ?        00:00:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mysqld.log --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/lib/mysql/mysql.sock
root       3720   2361  0 04:44 pts/0    00:00:00 grep --color=auto mysql

MySQL安装成功之后是匿名的,什么意思呢?就是安装成功之后只有root用户和匿名用户,并且不需要密码就可以进入MySQL。匿名用户我们随便输用户名就能进但是并不能有其他任何操作。因此我们需要使用root用户设置密码等。

[root@192 ~]# mysql -uroot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 7
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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当前用户和密码
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> select user,password,host from user;
+------+----------+-----------------+
| user | password | host            |
+------+----------+-----------------+
| root |          | localhost       |
| root |          | 192.168.226.128 |
| root |          | 127.0.0.1       |
| root |          | ::1             |
|      |          | localhost       |
|      |          | 192.168.226.128 |
+------+----------+-----------------+
6 rows in set (0.00 sec)
可以看到用户有root和匿名用户,密码都为空,就是没有密码的意思。
下面我们删除匿名用户并为root设置密码
mysql> delete from mysql.user where user='';
Query OK, 2 rows affected (0.00 sec)

mysql> update user set password=PASSWORD('123456') where user='root';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

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

mysql> exit;
Bye

下面重新进入MySQL客户端不输密码就会报错,输入密码后进入mysql。

[root@192 ~]# mysql -uroot
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@192 ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 5.6.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2017, 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个

红包金额最低5元

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

抵扣说明:

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

余额充值