Home

MySQL Setup Memo

■MySQLインストール
kokaki@skynew:~$ sudo apt install -y mysql-server mysql-client php-mysqli

■MySQLバージョン確認
kokaki@skynew:~$ mysql --version
    mysql  Ver 8.0.40-0ubuntu0.24.04.1 for Linux on x86_64 ((Ubuntu))

■MySQL状態確認
kokaki@skynew:~$ sudo service mysql status
    ● mysql.service - MySQL Community Server
        Loaded: loaded (/usr/lib/systemd/system/mysql.service; enabled; preset: enabled)
        Active: active (running) since Thu 2025-01-02 10:12:41 JST; 22min ago
    Main PID: 13966 (mysqld)
        Status: "Server is operational"
        Tasks: 38 (limit: 9455)
        Memory: 398.9M ()
        CGroup: /system.slice/mysql.service
                └─13966 /usr/sbin/mysqld

    Jan 02 10:12:41 skynew systemd[1]: Starting mysql.service - MySQL Community Server...
    Jan 02 10:12:41 skynew systemd[1]: Started mysql.service - MySQL Community Server.

■MySQLユーザ作成
kokaki@skynew:~$ sudo mysql -uroot -p
    Enter password: (Raspberrypi5)
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 19
    Server version: 8.0.40-0ubuntu0.24.04.1 (Ubuntu)

    Copyright (c) 2000, 2024, Oracle and/or its affiliates.

    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> CREATE USER 'dev'@'localhost' IDENTIFIED BY 'dev';
    Query OK, 0 rows affected (0.02 sec)

    mysql> GRANT all on *.* to 'dev'@'localhost';
    Query OK, 0 rows affected (0.02 sec)

    mysql> CREATE DATABASE dev;
    Query OK, 1 row affected (0.01 sec)

    mysql> FLUSH PRIVILEGES;
    Query OK, 0 rows affected (0.01 sec)

    mysql> quit
    Bye

■phpMyAdminインストール
kokaki@skynew:~$ sudo apt install -y phpmyadmin
    ┌────────────────────────────────┤ Configuring phpmyadmin ├────────────────────────────────┐
    │ Please choose the web server that should be automatically configured to run phpMyAdmin.  │
    │                                                                                          │
    │ Web server to reconfigure automatically:                                                 │
    │                                                                                          │
    │  [*] apache2                                                                             │
    │  [ ] lighttpd                                                                            │
    │                                                                                          │
    │                                                                                          │
    │                                          <Ok>                                            │
    │                                                                                          │
    └──────────────────────────────────────────────────────────────────────────────────────────┘

    ┌──────────────────────────────────────────────┤ Configuring phpmyadmin ├──────────────────────────────────────────────┐
    │                                                                                                                      │
    │ The phpmyadmin package must have a database installed and configured before it can be used. This can be optionally   │
    │ handled with dbconfig-common.                                                                                        │
    │                                                                                                                      │
    │ If you are an advanced database administrator and know that you want to perform this configuration manually, or if   │
    │ your database has already been installed and configured, you should refuse this option. Details on what needs to be  │
    │ done should most likely be provided in /usr/share/doc/phpmyadmin.                                                    │
    │                                                                                                                      │
    │ Otherwise, you should probably choose this option.                                                                   │
    │                                                                                                                      │
    │ Configure database for phpmyadmin with dbconfig-common?                                                              │
    │                                                                                                                      │
    │                                   <Yes>                                      <No>                                    │
    │                                                                                                                      │
    └──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

    ┌────────────────────────────────────────────┤ Configuring phpmyadmin ├─────────────────────────────────────────────┐
    │ Please provide a password for phpmyadmin to register with the database server. If left blank, a random password   │
    │ will be generated.                                                                                                │
    │                                                                                                                   │
    │ MySQL application password for phpmyadmin:                                                                        │
    │                                                                                                                   │
    │ ************_____________________________________________________________________________________________________ │
    │                                                                                                                   │
    │                                 <Ok>                                     <Cancel>                                 │
    │                                                                                                                   │
    └───────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

    ┌────┤ Configuring phpmyadmin ├─────┐
    │                                   │
    │                                   │
    │ Password confirmation:            │
    │                                   │
    │ ************_____________________ │
    │                                   │
    │      <Ok>          <Cancel>       │
    │                                   │
    └───────────────────────────────────┘
http://localhost/phpmyadmin
phpMyAdmin
■MySQLセキュリティ
kokaki@skynew:~$ sudo mysql_secure_installation
    Securing the MySQL server deployment.

    Connecting to MySQL using a blank password.

    VALIDATE PASSWORD COMPONENT can be used to test passwords
    and improve security. It checks the strength of password
    and allows the users to set only those passwords which are
    secure enough. Would you like to setup VALIDATE PASSWORD component?

    Press y|Y for Yes, any other key for No: y

    There are three levels of password validation policy:

    LOW    Length >= 8
    MEDIUM Length >= 8, numeric, mixed case, and special characters
    STRONG Length >= 8, numeric, mixed case, special characters and dictionary                  file

    Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0

    Skipping password set for root as authentication with auth_socket is used by default.
    If you would like to use password authentication instead, this can be done with the "ALTER_USER" command.
    See https://dev.mysql.com/doc/refman/8.0/en/alter-user.html#alter-user-password-management for more information.

    By default, a MySQL installation has an anonymous user,
    allowing anyone to log into MySQL without having to have
    a user account created for them. This is intended only for
    testing, and to make the installation go a bit smoother.
    You should remove them before moving into a production
    environment.

    Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
    Success.


    Normally, root should only be allowed to connect from
    'localhost'. This ensures that someone cannot guess at
    the root password from the network.

    Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y
    Success.

    By default, MySQL comes with a database named 'test' that
    anyone can access. This is also intended only for testing,
    and should be removed before moving into a production
    environment.


    Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
    - Dropping test database...
    Success.

    - Removing privileges on test database...
    Success.

    Reloading the privilege tables will ensure that all changes
    made so far will take effect immediately.

    Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
    Success.

    All done!
■MySQLテーブル(dev.students)作成
kokaki@skynew:~$ mysql -udev -p dev
    Enter password:
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 44
    Server version: 8.0.40-0ubuntu0.24.04.1 (Ubuntu)

    Copyright (c) 2000, 2024, Oracle and/or its affiliates.

    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> CREATE TABLE students(id int,name varchar(20));
    Query OK, 0 rows affected (0.06 sec)

    mysql> INSERT INTO students (id, name) values (1, 'Taro'), (2, 'Hanako');
    Query OK, 2 rows affected (0.01 sec)
    Records: 2  Duplicates: 0  Warnings: 0

    mysql> commit;
    Query OK, 0 rows affected (0.00 sec)

    mysql> show tables;
    +---------------+
    | Tables_in_dev |
    +---------------+
    | students      |
    +---------------+
    1 row in set (0.00 sec)

    mysql> quit
    Bye
■動作確認
http://localhost/php/phpmysqltest.php
phpからMySQLデータベースにアクセス

Home

2025 kokaki.jp, Office.