Ubuntu 20.04 MariaDBインストールと外部接続設定

MariaDB外部から接続する方法

AWS EC2Ubuntu 20.04MariaDBをインストールする

aptを最新版にUpgradeする

sudo apt upgrade

aptのパッケージ一覧を更新する

sudo apt update

MariaDBをインストールする

sudo apt install mariadb-server

MariaDBの初期設定スクリプトを開始する

sudo mysql_secure_installation

MariaDB最初の起動

sudo mariadb

上記が実行で↓下記Errorが出る場合

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)

MariaDBが起動できていないので下記コマンドで起動する

sudo systemctl start mariadb
sudo mariadb
「Server version: 10.3.34-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04」と出た
「Server version: 10.3.34-MariaDB-0ubuntu0.20.04.1 Ubuntu 20.04」と出た

exitを入力しmariadbコマンドツールから抜ける

MariaDBサーバーに外部から接続できる設定を先にしておく

viエディタで50-server.cnf(mysqld.conf)を編集

sudo vi /etc/mysql/mariadb.conf.d/50-server.cnf 

/etc/mysql/mariadb.conf.dここに50-server.cnfが無い場合mysqld.confを編集する

28行目あたりにbind-address = 127.0.0.1があるのでコメントアウトする

外部から接続できるユーザーを作成

「sudo mariadb」ここに入ってからユーザー作成する下記コマンドを入力する

CREATE USER "user1"@"%" IDENTIFIED BY 'password1';
SET PASSWORD FOR "user1"@"%" = PASSWORD('password1');
GRANT ALL ON *.* TO "user1"@"localhost";
grant all privileges on *.* to "user1"@"%";

上記の2行目はいらないかも

user1というユーザーを作成し、パスワードはpassword1

MariaDB 起動|終了|再起動

sudo systemctl start mariadb #起動
sudo systemctl stop mariadb  #終了
sudo systemctl restart mariadb #再起動

ユーザー作成と設定が終わったので、MariaDBをrestart再起動しておく

MariaDBサーバーへ外部から接続する

mysql -h127.0.0.1 -uuser1 -ppassword1 データベース名

AWS CLI DynamoDB Query コマンド

AWS CLIを使った DynamoDB Queryコマンドを使った テーブルデータ取得方法です。

テーブル名 = table1として Partition Key = ID ソートキーなしの場合

aws dynamodb query \
  --table-name table1 \
  --key-condition-expression "ID = :U" \
  --expression-attribute-values  '{":U":{"S":"検索したい文字"}}' \
  --profile あれば --region ap-northeast-1 > out.json

–profileや–region指定なければ入れなくてOKです。例では最後にout.jsonファイルに出力されます。ソートキーが無い場合Queryはあまり意味が無いかもです

上記 table1テーブルにPartition KeyとSort Keyがある場合(Sort Key = SORT1)

aws dynamodb query \
  --table-name table1 \
  --key-condition-expression "ID = :U and begins_with(SORT1, :S)" \
  --expression-attribute-values  '{":U":{"S":"検索したい文字"}, ":S":{"S":"検索したいソート名"}}' \
  --profile あれば --region ap-northeast-1 > out.json

–key-condition-expressionにbegins_with()が入っていますが この場合前方一致の文字列検索が可能です。

2022 MJELD TECHNOLOGIES. ALL RIGHTS RESERVED