両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン |
linux:mysql [2019/01/03 11:59] – [SQLファイルの実行] ともやん | linux:mysql [2024/05/22 00:34] (現在) – [UNIX ドメインソケット経由で接続エラーになる場合] ともやん |
---|
| |
==== パッケージインストール ==== | ==== パッケージインストール ==== |
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> |
==== 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">='</font><font color="#E6DB74">SET NAMES utf8mb4</font><font color="#F8F8F2">'</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デーモンを起動するには以下のコマンドを実行する。 |
| |
==== ログイン確認 ==== | ==== ログイン確認 ==== |
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><- 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 'help;' or '\h' for help. Type '\c' to clear the current input statement.</b> |
</code> | |
| MariaDB [(none)]> \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't connect to local server through socket '/var/lib/mysql/mysql.sock' (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 'help;' or '\h' for help. Type '\c' to clear the current input statement.</b> |
| |
| MariaDB [(none)]> \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> |
| |
===== データベース作成 ===== | ===== データベース作成 ===== |
Database changed | Database changed |
MariaDB [tomoyan_db]> SOURCE filename; | 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> | </code> |
| |
以下のようにスキーマコピーとデータコピーで対応する。 | 以下のようにスキーマコピーとデータコピーで対応する。 |
<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> |
===== ダンプファイルの作成方法(バックアップ) ===== | ===== ダンプファイルの作成方法(バックアップ) ===== |
以降の説明では、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) 環境構築]]\\ |
| |
==== パッケージのインストール ==== | ==== パッケージのインストール ==== |