Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
Help us understand the problem. What is going on with this article?

CentOSにmysqlコマンドをインストールする方法

  • 環境
    • CentOS Linux release 7.8.2003 (Core)
    • 接続先 : MySQL8.0.20

やりたいこと : mysqlコマンドだけインストールしたい

以前、RDSに接続したくてUbuntuにmysqlコマンドだけインストールしたことがある。

DockerでMySQLデータベースとアプリケーションのコンテナを作成した。
なので今回は、データベース用のコンテナに接続するためにmysqlコマンドだけほしい。

MySQLリポジトリをインストールする

  1. MySQLリポジトリをインストールする
$ sudo rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
[sudo] password for ponsuke:
Retrieving https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm
warning: /var/tmp/rpm-tmp.P6p8HL: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:mysql80-community-release-el7-1  ################################# [100%]

欲しいバージョンが有効になっていることを確認する

  1. ほしいバージョンが有効になっていることを確認する
  2. ほしいバージョンが無効になっていたらyum-config-managerコマンドで有効化する
# ほしいバージョンが有効になっていることを確認する
$ sudo yum repolist enabled | grep mysql
Repository google-chrome is listed more than once in the configuration
mysql-connectors-community/x86_64 MySQL Connectors Community                 153
mysql-tools-community/x86_64      MySQL Tools Community                      110
mysql80-community/x86_64          MySQL 8.0 Community Server                 177

インストールする

  1. mysqlコマンドはmysql-community-clientに含まれている
  2. mysql-community-clientをインストールする
# mysqlコマンドはmysql-community-clientに含まれている
$ sudo yum search mysql-community-client
Loaded plugins: fastestmirror, ovl
Repository google-chrome is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: d36uatko69830t.cloudfront.net
 * epel: d2lzkl7pfhq30w.cloudfront.net
 * extras: d36uatko69830t.cloudfront.net
 * updates: d36uatko69830t.cloudfront.net
============================================================================ N/S matched: mysql-community-client =============================================================================
mysql-community-client.i686 : MySQL database client applications and tools
mysql-community-client.x86_64 : MySQL database client applications and tools

  Name and summary matches only, use "search all" for everything.


# mysql-community-clientをインストールする
$ sudo yum install -y mysql-community-client.x86_64
Loaded plugins: fastestmirror, ovl
Repository google-chrome is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: d36uatko69830t.cloudfront.net
 * epel: d2lzkl7pfhq30w.cloudfront.net
 * extras: d36uatko69830t.cloudfront.net
 * updates: d36uatko69830t.cloudfront.net
Resolving Dependencies
--> Running transaction check
---> Package mysql-community-client.x86_64 0:8.0.20-1.el7 will be installed
--> Processing Dependency: mysql-community-libs(x86-64) >= 8.0.11 for package: mysql-community-client-8.0.20-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-libs.x86_64 0:8.0.20-1.el7 will be installed
--> Processing Dependency: mysql-community-common(x86-64) >= 8.0.11 for package: mysql-community-libs-8.0.20-1.el7.x86_64
--> Running transaction check
---> Package mysql-community-common.x86_64 0:8.0.20-1.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==============================================================================================================================================================================================
 Package                                              Arch                                 Version                                      Repository                                       Size
==============================================================================================================================================================================================
Installing:
 mysql-community-client                               x86_64                               8.0.20-1.el7                                 mysql80-community                                47 M
Installing for dependencies:
 mysql-community-common                               x86_64                               8.0.20-1.el7                                 mysql80-community                               609 k
 mysql-community-libs                                 x86_64                               8.0.20-1.el7                                 mysql80-community                               4.5 M

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

Total download size: 52 M
Installed size: 256 M
Downloading packages:
warning: /var/cache/yum/x86_64/7/mysql80-community/packages/mysql-community-common-8.0.20-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Public key for mysql-community-common-8.0.20-1.el7.x86_64.rpm is not installed
(1/3): mysql-community-common-8.0.20-1.el7.x86_64.rpm                                                                                                                  | 609 kB  00:00:00
(2/3): mysql-community-libs-8.0.20-1.el7.x86_64.rpm                                                                                                                    | 4.5 MB  00:00:00
(3/3): mysql-community-client-8.0.20-1.el7.x86_64.rpm                                                                                                                  |  47 MB  00:00:00
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                                                                         138 MB/s |  52 MB  00:00:00
Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Importing GPG key 0x5072E1F5:
 Userid     : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
 Fingerprint: a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
 Package    : mysql80-community-release-el7-1.noarch (installed)
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
  Installing : mysql-community-common-8.0.20-1.el7.x86_64                                                                                                                                 1/3
  Installing : mysql-community-libs-8.0.20-1.el7.x86_64                                                                                                                                   2/3
  Installing : mysql-community-client-8.0.20-1.el7.x86_64                                                                                                                                 3/3
  Verifying  : mysql-community-client-8.0.20-1.el7.x86_64                                                                                                                                 1/3
  Verifying  : mysql-community-libs-8.0.20-1.el7.x86_64                                                                                                                                   2/3
  Verifying  : mysql-community-common-8.0.20-1.el7.x86_64                                                                                                                                 3/3

Installed:
  mysql-community-client.x86_64 0:8.0.20-1.el7

Dependency Installed:
  mysql-community-common.x86_64 0:8.0.20-1.el7                                                   mysql-community-libs.x86_64 0:8.0.20-1.el7

Complete!

アプリケーション用コンテナから接続してみる

$ mysql -h {MySQLのコンテナ名} -u root -D {データベース名} -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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> 
ponsuke0531
びっくりするほど物覚えが悪いが、エンジニアを目指しています。
http://ponsuke-tarou.hatenablog.com/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away