MySQLデータベースをインストールする
1. MySQLのダウンロード
ダウンロードリンク:http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.31-1.el7.x86_64.rpm-bundle.tar
選択:mysql-5.7.31-1.el7.x86_64.rpm-bundle.tar(他の適切なバージョンも選択できます)
2. Linuxプラットフォームでのインストール
- 必要な作業:mariadbをアンインストールする
[root@mysql ~]# yum remove mysql-libs -y
- mysql-5.7.31-1.el7.x86_64.rpm-bundle.tarを解凍します。
[root@mysql ~]# tar -xvf mysql-5.7.31-1.el7.x86_64.rpm-bundle.tar
mysql-community-embedded-devel-5.7.31-1.el7.x86_64.rpm
mysql-community-libs-5.7.31-1.el7.x86_64.rpm
mysql-community-client-5.7.31-1.el7.x86_64.rpm
mysql-community-server-5.7.31-1.el7.x86_64.rpm
mysql-community-embedded-5.7.31-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.31-1.el7.x86_64.rpm
mysql-community-common-5.7.31-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.31-1.el7.x86_64.rpm
mysql-community-devel-5.7.31-1.el7.x86_64.rpm
mysql-community-test-5.7.31-1.el7.x86_64.rpm
- RPMパッケージをインストールします。
パッケージ間にはいくつかの依存関係があるため、それらは特定の順序でインストールする必要があります。
-
mysql-community-common-5.7.31-1.el7.x86_64.rpm
[root@mysql ~]# rpm -ivh mysql-community-common-5.7.31-1.el7.x86_64.rpm 警告:mysql-community-common-5.7.31-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql-community-common-5.7.31-1.e################################# [100%]
-
mysql-community-libs-5.7.31-1.el7.x86_64.rpm
[root@mysql ~]# rpm -ivh mysql-community-libs-5.7.31-1.el7.x86_64.rpm 警告:mysql-community-libs-5.7.31-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql-community-libs-5.7.31-1.el7################################# [100%]
-
mysql-community-libs-compat-5.7.31-1.el7.x86_64.rpm
[root@mysql ~]# rpm -ivh mysql-community-libs-compat-5.7.31-1.el7.x86_64.rpm 警告:mysql-community-libs-compat-5.7.31-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql-community-libs-compat-5.7.3################################# [100%]
-
mysql-community-client-5.7.31-1.el7.x86_64.rpm
[root@mysql ~]# rpm -ivh mysql-community-client-5.7.31-1.el7.x86_64.rpm 警告:mysql-community-client-5.7.31-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql-community-client-5.7.31-1.e################################# [100%]
-
mysql-community-server-5.7.31-1.el7.x86_64.rpm
[root@mysql ~]# rpm -ivh mysql-community-server-5.7.31-1.el7.x86_64.rpm 警告:mysql-community-server-5.7.31-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 错误:依赖检测失败: /usr/bin/perl 被 mysql-community-server-5.7.31-1.el7.x86_64 需要 net-tools 被 mysql-community-server-5.7.31-1.el7.x86_64 需要 perl(Getopt::Long) 被 mysql-community-server-5.7.31-1.el7.x86_64 需要 perl(strict) 被 mysql-community-server-5.7.31-1.el7.x86_64 需要
Perlとnet-toolsが必要です。インストールするだけです。
[root@mysql ~]# yum install perl net-tools -y
依存関係がインストールされたら、インストールできます。
[root@mysql ~]# rpm -ivh mysql-community-server-5.7.31-1.el7.x86_64.rpm 警告:mysql-community-server-5.7.31-1.el7.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:mysql-community-server-5.7.31-1.e################################# [100%]
- MySQLを起動します。
[root@mysql ~]# systemctl start mysqld #启动服务
[root@mysql ~]# systemctl enable mysqld #开机自启
[root@mysql ~]# systemctl status mysqld #查看状态
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
Active: active (running) since 一 2020-09-21 20:12:15 CST; 15s ago
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Main PID: 3278 (mysqld)
CGroup: /system.slice/mysqld.service
└─3278 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mys...
9月 21 20:12:13 mysql systemd[1]: Starting MySQL Server...
9月 21 20:12:15 mysql systemd[1]: Started MySQL Server.
- MySQLにログインします。
MySQL 5.7以降のバージョンでは、デフォルトのインストール中にrootユーザーの空のパスワードが削除されます。初めてログインするときに、システムは一時的なrootパスワードを生成します。これは/var/log/mysqld.logで確認できます。
[root@mysql ~]# cat /var/log/mysqld.log | grep "password"
2020-09-21T12:12:13.721817Z 1 [Note] A temporary password is generated for root@localhost: Qqfpoxet1+p(
# 初始密码为:Qqfpoxet1+p(
[root@mysql ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.31
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>
このうち、mysqlはクライアントコマンドを表し、「-u」の後にデータベースユーザーが続き、「-p」は入力する必要のあるパスワードを表します。同時に、ウェルカムインターフェースは次の部分についても説明します。
- コマンドは「;」または「\ g」で終わるコマンドは「;」または「\ g」で終わる
- MySQL接続IDは2 クライアントの接続IDです。この数は、これまでにMySQLサービスによって行われた接続の数を記録します。新しい接続ごとに自動的に1が追加されます
- サーバーのバージョン:5.7.31 MySQLサーバーのバージョンとタイプ
- ヘルプを表示するには「help;」または「\ h」と入力します。現在の入力ステートメントをクリアするには「\ c」を入力します。ヘルプの内容を表示するには「help;」または「\ h」を使用し、コマンドラインキャッシュをクリアするには「\ c」を使用します
ただし、一時的なパスワードでは、次のようなほとんどの操作を実行できません。
mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
#提示要使用 ALTER USER 来修改初始密码
- パスワードを変更する:
mysql> alter user 'root'@'localhost' identified by 'Opfordream@0518';
Query OK, 0 rows affected (0.00 sec)
- リモートでログインするようにrootユーザーを設定します。
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'Opfordream@0518' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)
- MySQLを構成します。
[root@mysql ~]# vim /etc/my.cnf
#增加如下内容
character-set-server=utf8
collation-server=utf8_general_ci
[client]
default-character-set=utf8
[root@mysql ~]# systemctl restart mysqld #重启 MySQL
- リモート接続をテストします。
最初にサーバー側のファイアウォールをオフにすることをお勧めします。
[root@mysql ~]# systemctl stop firewalld
[root@mysql ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
リモート接続をテストします。