linux:mysql

文書の過去の版を表示しています。


MySQL

MySQL をインストールするには以下のコマンドを実行する。

$ sudo yum install mariadb mariadb-server
$ sudo chkconfig mysqld on
$ sudo chkconfig --list mysqld
mysqld          0:off   1:off   2:on    3:on    4:on    5:on    6:off

CentOS7

$ sudo systemctl enable mariadb.service

UTF-8を標準とするように設定する。

$ sudo vi /etc/my.cnf
[mysqld]
character-set-server=utf8

[mysql]
default-character-set=utf8

MySQLデーモンを起動するには以下のコマンドを実行する。

$ sudo service mysqld start
MySQL データベースを初期化中:  Installing MySQL system tables...
OK
Filling help tables...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/usr/bin/mysqladmin -u root password 'new-password'
/usr/bin/mysqladmin -u root -h green.fireball.local password 'new-password'

Alternatively you can run:
/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default.  This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /usr ; /usr/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl

Please report any problems with the /usr/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com
                                                           [  OK  ]
MySQL を起動中:                                            [  OK  ]

CentOS7

$ sudo systemctl start mariadb.service
$ mysql -u root
mysql> show variables like "char%";
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

以下のコマンドを実行して MySQL の root パスワードを変更する。

$ mysql_secure_installation
~省略~
Enter current password for root (enter for none): Enter <- 入力
~省略~
Set root password? [Y/n] Enter <- 入力
New password: ******** <- root のパスワードを入力
Re-enter new password: ******** <- root の確認パスワードを入力
~省略~
Remove anonymous users? [Y/n] Enter <- 入力
~省略~
Disallow root login remotely? [Y/n] Enter <- 入力
~省略~
Remove test database and access to it? [Y/n] n <- 入力
~省略~
Reload privilege tables now? [Y/n] Enter <- 入力

root にてログインできることを確認する。

$ mysql -u root -p
Enter password: ******** <- root ユーザーのパスワードを入力
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.0.77 Source distribution

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql>
  1. 以下のコマンドを実行して MySQL コマンドラインツールを実行する。
    # mysql -u root -p
    Enter password: ******** <- root ユーザーのパスワードを入力
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 5
    Server version: 5.0.77 Source distribution
    
    Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
  2. 現在存在しているデータベースを確認する。
    mysql> SHOW DATABASES;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema | 
    | mysql              | 
    | test               | 
    +--------------------+
    3 rows in set (0.00 sec)
  3. データベースの作成を行う。
    mysql> CREATE DATABASE tomoyan_db;
    Query OK, 1 row affected (0.00 sec)

    キャラクタセットを指定する場合(MySQL 4.1以降)

    CREATE DATABASE tomoyan_db CHARACTER SET utf8;
  4. データベースが正しく作成されていることを確認する。
    mysql> SHOW DATABASES;
    +--------------------+
    | Database           |
    +--------------------+
    | information_schema | 
    | mysql              | 
    | test               | 
    | tomoyan_db         | 
    +--------------------+
    4 rows in set (0.00 sec)
  1. ユーザーを作成するには以下のコマンドを実行する。
    mysql> GRANT ALL PRIVILEGES ON tomoyan_db.* TO 'tomoyan'@'localhost' \
     IDENTIFIED BY 'password' WITH GRANT OPTION;
    Query OK, 0 rows affected (0.00 sec)
  2. ユーザーが正しく作成されたことを確認するために以下のSQLを実行する。
    mysql> SELECT host, user, password FROM mysql.user;
    +----------------------+---------+------------------+
    | host                 | user    | password         |
    +----------------------+---------+------------------+
    | localhost            | root    | 7ba00ed47f10af52 | 
    | green.fireball.local | root    | 7ba00ed47f10af52 | 
    | 127.0.0.1            | root    | 7ba00ed47f10af52 | 
    | localhost            |         |                  | 
    | green.fireball.local |         |                  | 
    | localhost            | tomoyan | 7ba00ed47f10af52 | 
    +----------------------+---------+------------------+
    6 rows in set (0.00 sec)
  3. mysql データベースの権限テーブルより権限の再読み込みを行う。
    mysql> FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.00 sec)

 ユーザーのパスワードを変更するには以下のコマンドを実行する。

mysql> SET PASSWORD FOR tomoyan@"localhost"=PASSWORD('newpassword');

ユーザーをテーブルから削除する。

mysql> DROP USER 'tomoyan'@'localhost';
mysql> USE database_name;
$ mysql -u user -p[password] [database] < sqlfile.sql
mysql> SOURCE filename;

MySQL サーバ 5.1 では、Sybase SQL 拡張機能である SELECT INTO TABLE はサポートされていない。
以下のようにスキーマコピーとデータコピーで対応する。

mysql> CREATE TABLE table_name_yyyymmdd LIKE table_name;
mysql> INSERT INTO table_name_yyyymmdd SELECT * FROM table_name;

全てのデータベースをダンプする場合

$ mysqldump -u root -p[password] -x --all-databases > alldatabase_datas.dump

データベース名を指定してダンプする場合

$ mysqldump -u root -p[password] [database] > database_datas.dump

テーブル名を指定してデータのみダンプする場合(DROP & CREATE & INSERT)

$ mysqldump -u root -p[password] [database] [table1] [table2...] > table_datas.sql

テーブル名を指定してデータのみダンプする場合(INSERT)

$ mysqldump -u root -p[password] -t [database] [table1] [table2...] > table_datas.sql

テーブル名を指定してXMLをエクスポートする場合

$ mysqldump -u root -p[password] -X [database] [table1] [table2...] > table_datas.sql

全てのデータベースを復元する場合

$ mysql -u root -p[password] < alldatabases.dump

データベース名を指定して復元する場合

$ mysql -u root -p[password] [データベース名] < データベース名.dump

 以降の説明では、LAMP 環境と epel リポジトリの設定が済んでいることを前提としている。これらの条件を満たすためには、予め以下の二つの手順を実行しておくこと。

 CentOS で RPM Fusion を利用する
 CentOS による LAMP(Apache, MySQL, PHP) 環境構築

  1. 以下のコマンドを実行すると epel リポジトリより phpMyAdmin パッケージがインストールされる。
    # yum install phpMyAdmin
  2. インストールが完了すると、以下のパスに導入されている。
    /usr/share/phpMyAdmin
  3. Apache に対しての設定も、以下のファイルが httpd.conf に Include されることによって行われる。
    /etc/httpd/conf.d/phpmyadmin.conf
  4. phpMyAdmin.conf 初期設定は以下の内容となっており、ローカルからのアクセスのみ許可している。
    Alias /phpMyAdmin /usr/share/phpMyAdmin
    Alias /phpmyadmin /usr/share/phpMyAdmin
    <Directory /usr/share/phpMyAdmin/>
       order deny,allow
       deny from all
       allow from 127.0.0.1
       allow from ::1
    </Directory>
    <Directory /usr/share/phpMyAdmin/libraries>
        Order Deny,Allow
        Deny from All
        Allow from None
    </Directory>
  5. このままの設定で外部から利用するのが良いので、putty などを利用して SSH のトンネル設定で以下のようにポートフォワードの設定を行う。
    L8081 localhost:80
    ※ローカル 8081 ポートへの通信を リモートの localhost:80 へ転送する。
  6. putty で接続して以下の URL にアクセスすることで利用できる。
    http://localhost:8081/phpmyadmin
  • linux/mysql.1546482742.txt.gz
  • 最終更新: 2019/05/18 02:23
  • (外部編集)