差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
linux:mysql [2019/01/03 11:34] – [MySQLサービスの起動] ともやんlinux:mysql [2024/05/22 00:34] (現在) – [UNIX ドメインソケット経由で接続エラーになる場合] ともやん
行 4: 行 4:
  
 ==== パッケージインストール ==== ==== パッケージインストール ====
- MySQL をインストールするには以下のコマンドを実行する。 +MySQL をインストールするには以下のコマンドを実行する。\\ 
-<code+<WRAP color_term
-$ sudo dnf install mariadb mariadb-server +<WRAP color_command><html><pre> 
-</code>+<font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">dnf</font> install <font color="#A347BA">-y</font> mariadb mariadb-server 
 +</pre></html></WRAP> 
 +</WRAP> 
 ==== MySQLサービスの自動起動設定 ==== ==== MySQLサービスの自動起動設定 ====
 <code> <code>
行 20: 行 23:
 ==== MySQL の設定 ==== ==== MySQL の設定 ====
 UTF-8を標準とするように設定する。 UTF-8を標準とするように設定する。
-<code+<WRAP color_term
-$ sudo vi /etc/my.cnf +<WRAP color_command><html><pre> 
-</code+<font color="#0087FF"><b>$</b></font> <font color="#26A269"><u style="text-decoration-style:solid">sudo</u></font> <font color="#26A269">nano</font> <u style="text-decoration-style:solid">/etc/my.cnf</u> 
-<code+</pre></html></WRAP
-[mysqld] +<WRAP color_result><html><pre
-character-set-server=utf8+<font color="#75715E">#</font> 
 +<font color="#75715E"># This group is read both both by the client and the server</font> 
 +<font color="#75715E"># use it for options that affect everything</font> 
 +<font color="#75715E">#</font> 
 +<font color="#F8F8F2">[</font><font color="#A6E22E">client-server</font><font color="#F8F8F2">]</font>
  
-[mysql+<font color="#F8F8F2">[</font><font color="#A6E22E">client</font><font color="#F8F8F2">]</font> 
-default-character-set=utf8 +<font color="#F92672">default-character-set</font><font color="#F8F8F2">=</font><font color="#E6DB74">utf8mb4</font>
-</code>+
  
 +<font color="#F8F8F2">[</font><font color="#A6E22E">mysql</font><font color="#F8F8F2">]</font>
 +<font color="#F92672">default-character-set</font><font color="#F8F8F2">=</font><font color="#E6DB74">utf8mb4</font>
 +
 +<font color="#F8F8F2">[</font><font color="#A6E22E">mysqld</font><font color="#F8F8F2">]</font>
 +<font color="#F92672">collation-server</font><font color="#F8F8F2"> = </font><font color="#E6DB74">utf8mb4_unicode_520_ci</font>
 +<font color="#F92672">init-connect</font><font color="#F8F8F2">=&apos;</font><font color="#E6DB74">SET NAMES utf8mb4</font><font color="#F8F8F2">&apos;</font>
 +<font color="#F92672">character-set-server</font><font color="#F8F8F2">=</font><font color="#E6DB74">utf8mb4</font>
 +
 +<font color="#75715E">#</font>
 +<font color="#75715E"># include all files from the config directory</font>
 +<font color="#75715E">#</font>
 +<font color="#E6DB74">!includedir /etc/my.cnf.d</font>
 +</pre></html></WRAP>
 +</WRAP>
 ==== MySQLサービスの起動 ==== ==== MySQLサービスの起動 ====
  MySQLデーモンを起動するには以下のコマンドを実行する。  MySQLデーモンを起動するには以下のコマンドを実行する。
行 118: 行 138:
  
 ==== ログイン確認 ==== ==== ログイン確認 ====
-root にてログインできることを確認する。 +mariadb の root にてログインできることを確認する。 
-<code+<WRAP color_term
-$ mysql -u root -p +<WRAP color_command><html><pre> 
-Enter password: ******** <- root ユーザーのパスワードを入力 +<font color="#0087FF"><b>$</b></font> <font color="#26A269">mysql</font> <font color="#A347BA">-u</font> root <font color="#A347BA">-p</font> 
-Welcome to the MySQL monitor.  Commands end with ; or \g. +</pre></html></WRAP> 
-Your MySQL connection id is 4 +<WRAP color_result><html><pre> 
-Server version: 5.0.77 Source distribution+Enter password: ******** <b class=DiY>&lt;- root ユーザーのパスワードを入力</b> 
 +<b>Welcome to the MariaDB monitor.  Commands end with ; or \g.</b> 
 +<b>Your MariaDB connection id is 54</b> 
 +<b>Server version: 10.11.6-MariaDB-log Alpine Linux</b>
  
-Type 'help;' or '\h' for help. Type '\c' to clear the buffer.+<b>Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.</b>
  
-mysql> +<b>Type &apos;help;&apos; or &apos;\h&apos; for help. Type &apos;\c&apos; to clear the current input statement.</b> 
-</code>+ 
 +MariaDB [(none)]&gt; \q 
 +<b>Bye</b> 
 +</pre></html></WRAP> 
 +</WRAP> 
 + 
 +=== UNIX ドメインソケット経由の接続エラー === 
 +<WRAP round tip minfont_12 90%> 
 +UNIX ドメインソケット経由でローカルサーバーに接続できない場合は...🤪\\ 
 +<WRAP color_term> 
 +<WRAP color_command><html><pre> 
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269">mysql</font> <font color="#A347BA">-u</font> root <font color="#A347BA">-p</font
 +</pre></html></WRAP> 
 +<WRAP color_result><html><pre> 
 +Enter password:  
 +ERROR 2002 (HY000): Can&apos;t connect to local server through socket &apos;/var/lib/mysql/mysql.sock&apos; (2) 
 +</pre></html></WRAP> 
 +</WRAP> 
 + 
 +同一コンピューター内なので UNIX ドメインソケット (mysql.sock) で通信しようとするが、__Podman や Docker などのコンテナ仮想化では別マシンと通信するときと同様に TCP/IP ソケット通信でなければ接続できない。__\\ 
 + 
 +<html><code>-h ::1</code></html> オプションで IPv6/IPv4 ローカルホストアドレスを指定すると TCP/IP ソケット通信にできる😉\\ 
 +または、<html><code>-P 3306</code></html> オプションでポート指定することでも同様に通信できる😊\\ 
 +<WRAP color_term> 
 +<WRAP color_command><html><pre> 
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269">mysql</font> <font color="#A347BA">-u</font> root <font color="#A347BA">-h</font> ::1 <font color="#A347BA">-p</font> 
 +or 
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269">mysql</font> <font color="#A347BA">-u</font> root <font color="#A347BA">-h</font> 127.0.0.1 <font color="#A347BA">-p</font> 
 +or 
 +<font color="#0087FF"><b>$</b></font> <font color="#26A269">mysql</font> <font color="#A347BA">-u</font> root <font color="#A347BA">-P</font> 3306 <font color="#A347BA">-p</font> 
 +</pre></html></WRAP> 
 +<WRAP color_result><html><pre> 
 +Enter password:  
 +<b>Welcome to the MariaDB monitor.  Commands end with ; or \g.</b> 
 +<b>Your MariaDB connection id is 54</b> 
 +<b>Server version: 10.11.6-MariaDB-log Alpine Linux</b> 
 + 
 +<b>Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.</b> 
 + 
 +<b>Type &apos;help;&apos; or &apos;\h&apos; for help. Type &apos;\c&apos; to clear the current input statement.</b> 
 + 
 +MariaDB [(none)]&gt; \q 
 +<b>Bye</b> 
 +</pre></html></WRAP> 
 +</WRAP> 
 +参考: [[so>questions/4448467/cant-connect-to-local-mysql-server-through-socket-var-lib-mysql-mysql-sock|Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) - Stack Overflow]]\\ 
 +</WRAP>
  
 ===== データベース作成 ===== ===== データベース作成 =====
行 135: 行 204:
 # mysql -u root -p # mysql -u root -p
 Enter password: ******** <- root ユーザーのパスワードを入力 Enter password: ******** <- root ユーザーのパスワードを入力
-Welcome to the MySQL monitor.  Commands end with ; or \g. +Welcome to the MariaDB monitor.  Commands end with ; or \g. 
-Your MySQL connection id is 5 +Your MariaDB connection id is 15 
-Server version: 5.0.77 Source distribution+Server version: 10.3.11-MariaDB MariaDB Server 
 + 
 +Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  
-Type 'help;' or '\h' for help. Type '\c' to clear the buffer.+Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
 </code> </code>
   - 現在存在しているデータベースを確認する。<code>   - 現在存在しているデータベースを確認する。<code>
-mysql> SHOW DATABASES;+MariaDB [(none)]> SHOW DATABASES;
 +--------------------+ +--------------------+
 | Database           | | Database           |
 +--------------------+ +--------------------+
-| information_schema |  +| information_schema | 
-| mysql              |  +| mysql              | 
-test               +performance_schema |
 +--------------------+ +--------------------+
-3 rows in set (0.00 sec)+3 rows in set (0.004 sec)
 </code> </code>
   - データベースの作成を行う。<code>   - データベースの作成を行う。<code>
-mysql> CREATE DATABASE tomoyan_db; +MariaDB [(none)]> CREATE DATABASE tomoyan_db; 
-Query OK, 1 row affected (0.00 sec)+Query OK, 1 row affected (0.001 sec)
 </code>キャラクタセットを指定する場合(MySQL 4.1以降)<code>CREATE DATABASE tomoyan_db CHARACTER SET utf8;</code> </code>キャラクタセットを指定する場合(MySQL 4.1以降)<code>CREATE DATABASE tomoyan_db CHARACTER SET utf8;</code>
   - データベースが正しく作成されていることを確認する。<code>   - データベースが正しく作成されていることを確認する。<code>
-mysql> SHOW DATABASES;+MariaDB [(none)]> SHOW DATABASES;
 +--------------------+ +--------------------+
 | Database           | | Database           |
 +--------------------+ +--------------------+
-| information_schema |  +| information_schema | 
-| mysql              |  +| mysql              | 
-test               |  +performance_schema 
-| tomoyan_db         +| tomoyan_db         |
 +--------------------+ +--------------------+
-4 rows in set (0.00 sec)+4 rows in set (0.003 sec)
 </code> </code>
  
 ===== ユーザー作成 ===== ===== ユーザー作成 =====
   - ユーザーを作成するには以下のコマンドを実行する。<code>   - ユーザーを作成するには以下のコマンドを実行する。<code>
-mysql> GRANT ALL PRIVILEGES ON tomoyan_db.* TO 'tomoyan'@'localhost' \+MariaDB [(none)]> GRANT ALL PRIVILEGES ON tomoyan_db.* TO 'tomoyan'@'localhost' \
  IDENTIFIED BY 'password' WITH GRANT OPTION;  IDENTIFIED BY 'password' WITH GRANT OPTION;
-Query OK, 0 rows affected (0.00 sec)+Query OK, 0 rows affected (0.003 sec)
 </code> </code>
   - ユーザーが正しく作成されたことを確認するために以下のSQLを実行する。<code>   - ユーザーが正しく作成されたことを確認するために以下のSQLを実行する。<code>
-mysql> SELECT host, user, password FROM mysql.user; +MariaDB [(none)]> SELECT host, user, password FROM mysql.user; 
-+----------------------+---------+------------------+ ++-----------+---------+-------------------------------------------+ 
-| host                 | user    | password         +| host      | user    | password                                  
-+----------------------+---------+------------------+ ++-----------+---------+-------------------------------------------+ 
-| localhost            | root    | 7ba00ed47f10af52 |  +| localhost | root    | *A8950ACBC0ABE4A58931119F34574629F62B4CE9 
-| green.fireball.local | root    | 7ba00ed47f10af52 |  +| 127.0.0.1 | root    | *A8950ACBC0ABE4A58931119F34574629F62B4CE9 
-| 127.0.0.1            | root    | 7ba00ed47f10af52 |  +::1       root    *A8950ACBC0ABE4A58931119F34574629F62B4CE9 
-localhost                    |                  |  +| localhost | tomoyan | *2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19 
-| green.fireball.local |                          |  ++-----------+---------+-------------------------------------------+ 
-| localhost            | tomoyan | 7ba00ed47f10af52 |  +rows in set (0.003 sec)
-+----------------------+---------+------------------+ +
-rows in set (0.00 sec)+
 </code> </code>
   - mysql データベースの権限テーブルより権限の再読み込みを行う。<code>   - mysql データベースの権限テーブルより権限の再読み込みを行う。<code>
-mysql> FLUSH PRIVILEGES; +MariaDB [(none)]> FLUSH PRIVILEGES; 
-Query OK, 0 rows affected (0.00 sec)+Query OK, 0 rows affected (0.002 sec)
 </code> </code>
  
行 197: 行 266:
  ユーザーのパスワードを変更するには以下のコマンドを実行する。  ユーザーのパスワードを変更するには以下のコマンドを実行する。
 <code> <code>
-mysql> SET PASSWORD FOR tomoyan@"localhost"=PASSWORD('newpassword');+MariaDB [(none)]> SET PASSWORD FOR tomoyan@"localhost"=PASSWORD('newpassword'); 
 +Query OK, 0 rows affected (0.001 sec)
 </code> </code>
  
行 203: 行 273:
 ユーザーをテーブルから削除する。 ユーザーをテーブルから削除する。
 <code> <code>
-mysql> DROP USER 'tomoyan'@'localhost';+MariaDB [(none)]> DROP USER 'tomoyan'@'localhost'; 
 +Query OK, 0 rows affected (0.004 sec)
 </code> </code>
  
 ===== 操作対象データベースの変更 ===== ===== 操作対象データベースの変更 =====
-<code>mysql> USE database_name;</code>+<code> 
 +MariaDB [(none)]> USE tomoyan_db; 
 +Database changed 
 +</code>
  
 ===== SQLファイルの実行 ===== ===== SQLファイルの実行 =====
行 213: 行 287:
 $ mysql -u user -p[password] [database] < sqlfile.sql $ mysql -u user -p[password] [database] < sqlfile.sql
 </code> </code>
-<code>mysql> SOURCE filename;</code>+<code> 
 +MariaDB [(none)]> USE tomoyan_db; 
 +Database changed 
 +MariaDB [tomoyan_db]> SOURCE filename; 
 +</code> 
 + 
 +===== データベースの削除 ===== 
 +<code> 
 +MariaDB [(none)]> DROP DATABASE tomoyan_db; 
 +Query OK, 0 rows affected (0.012 sec) 
 +</code> 
 +<code> 
 +MariaDB [(none)]> SHOW DATABASES; 
 ++--------------------+ 
 +| Database           | 
 ++--------------------+ 
 +| information_schema | 
 +| mysql              | 
 +| performance_schema | 
 ++--------------------+ 
 +3 rows in set (0.003 sec) 
 +</code>
  
 ===== テーブルのコピー ===== ===== テーブルのコピー =====
行 219: 行 314:
 以下のようにスキーマコピーとデータコピーで対応する。 以下のようにスキーマコピーとデータコピーで対応する。
 <code> <code>
-mysql> CREATE TABLE table_name_yyyymmdd LIKE table_name; +MariaDB [(none)]> CREATE TABLE table_name_yyyymmdd LIKE table_name; 
-mysql> INSERT INTO table_name_yyyymmdd SELECT * FROM table_name;+MariaDB [(none)]> INSERT INTO table_name_yyyymmdd SELECT * FROM table_name;
 </code> </code>
 ===== ダンプファイルの作成方法(バックアップ) ===== ===== ダンプファイルの作成方法(バックアップ) =====
行 256: 行 351:
  以降の説明では、LAMP 環境と epel リポジトリの設定が済んでいることを前提としている。これらの条件を満たすためには、予め以下の二つの手順を実行しておくこと。\\  以降の説明では、LAMP 環境と epel リポジトリの設定が済んでいることを前提としている。これらの条件を満たすためには、予め以下の二つの手順を実行しておくこと。\\
 \\ \\
- [[linux:centos_rpmfusion|CentOS で RPM Fusion を利用する]]\\ + [[linux:centos:rpmfusion|CentOS で RPM Fusion を利用する]]\\ 
- [[linux:centos_install_lamp|CentOS による LAMP(Apache, MySQL, PHP) 環境構築]]\\+ [[linux:centos:centos_install_lamp|CentOS による LAMP(Apache, MySQL, PHP) 環境構築]]\\
  
 ==== パッケージのインストール ==== ==== パッケージのインストール ====
  • linux/mysql.1546482899.txt.gz
  • 最終更新: 2019/05/18 02:23
  • (外部編集)