TOP ▲ itcore TOPTIPScentos7_mariadb.php  タグ:centos7 mariadb mysql php インストール

CentOS7 MariaDB インストール | itcore 2019年

インストール

# cd
# vi mariadb_install.sh
yum -y update
yum -y install mariadb mariadb-server mariadb-devel
# sh -x mariadb_install.sh |& tee mariadb_install.log

[root@erp2 ~]# rpm -qa | grep -i mariadb
mariadb-server-5.5.60-1.el7_5.x86_64
mariadb-5.5.60-1.el7_5.x86_64
mariadb-libs-5.5.60-1.el7_5.x86_64
mariadb-devel-5.5.60-1.el7_5.x86_64

# cp -ip /etc/my.cnf /etc/my.cnf_`date "+%Y%m%d"`
# vi /etc/my.cnf
[mysqld]
character-set-server=utf8
# インポートデータサイズ 1MB → 100MB
# もし超えるとERROR 2006 (HY000) at line 11569: MySQL server has gone away
max_allowed_packet=100MB
[client]
default-character-set = utf8

# systemctl start mariadb
# systemctl enable mariadb.service

[root@erp2 ~]# mysql --version
mysql Ver 15.1 Distrib 5.5.60-MariaDB, for Linux (x86_64) using readline 5.1

初期設定

[root@erp2 htdocs]# /usr/bin/mysql_secure_installation
Enter current password for root (enter for none): [改行]
Set root password? [Y/n] Y
New password: [rootパスワード]
Re-enter new password: [rootパスワード]
Remove anonymous users? [Y/na ]Y
Disallow root login remotely? [Y/n] Y
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y
Thanks for using MariaDB!

動作確認

[ログインとログオフ]
[root@erp2 htdocs]# mysql -uroot -p
Enter password: [rootパスワード]
Server version: 5.5.60-MariaDB MariaDB Server
MariaDB [(none)]> exit
Bye

[DB作成]
MariaDB [(none)]> CREATE DATABASE db1 DEFAULT CHARACTER SET utf8;
MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| db1                |
| mysql              |
| performance_schema |
+--------------------+

[ユーザ作成]
MariaDB [(none)]> GRANT ALL PRIVILEGES ON db1.* TO db1_user@localhost IDENTIFIED BY 'db1_password';

[ログイン]
[root@erp2 htdocs]# mysql -udb1_user -pdb1_password;

[テーブル作成]
MariaDB [(none)]> use db1
MariaDB [db1]> CREATE TABLE table1 (\
    ->     id INT AUTO_INCREMENT PRIMARY KEY,\
    ->     name VARCHAR(255)\
    ->     );
MariaDB [db1]> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| table1        |
+---------------+
MariaDB [db1]> desc table1;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| name  | varchar(255) | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

[データ作成]
MariaDB [db1]> insert table1 set name='name1';
MariaDB [db1]> insert table1 set name='name2';
MariaDB [db1]> select * from table1;
+----+-------+
| id | name  |
+----+-------+
|  1 | name1 |
|  2 | name2 |
+----+-------+

PHPからの接続確認

[PHPのmysqlモジュールインストール]
# yum -y install php-mysql php php-gd php-mbstring
# systemctl restart httpd

[動作確認プログラム]
# cd [ドキュメントルート]
# vi mysql.php
<?php
$mysqli = new mysqli("localhost", "db1_user", "db1_password", "db1");
$sql = "select * from table1";
echo "sql=$sql<br>\n";
if ($result = $mysqli->query($sql)) {
    while ($row = $result->fetch_assoc()) {
        echo "id=".$row["id"]. " name=".$row["name"]."<br>";
    }
    $result->close();
}
$mysqli->close();
?>

[実行結果]
sql=select * from table1
id=1 name=name1