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()が入っていますが この場合前方一致の文字列検索が可能です。

CognitoのUserAttributesをC#から取得

Cognito C#言語から接続

Amazon Cognitoは、OpenID Connect などのエンタープライズ ID プロバイダーを通してサインインすることができるAWS内のサービスです。Cognitoのユーザープールにユーザーを追加すると、ユーザー内にUserAttributes情報を収納できます。そのCognito UserAttributesをC#言語を使ってユーザーにログインしUserAttributesを取得するコード例です。

パッケージインストール

Visual Studio C#で新規プロジェクトを作成し、下記のようにnugetからAWSSDKの必要なパッケージをインストールします

Install-Package AWSSDK.Core -Version 3.7.5.9
Install-Package AWSSDK.CognitoIdentity -Version 3.7.0.110 #←これは要らないかも
Install-Package AWSSDK.CognitoIdentityProvider -Version 3.7.1.82
Install-Package Amazon.Extensions.CognitoAuthentication -Version 2.2.2

AWSコンソール画面から[プールID]と[アプリクライアント ID]を用意

下のようにAWSコンソール画面(ブラウザ)などからユーザープールを選択し[プールID]と[アプリクライアントID]情報を取得します。

プールID取得方法
[ユーザープール]→[全般設定]→[プールID]
アプリクライアントID取得方法
[ユーザープール]→[アプリクライアント]→[アプリクライアントID]

コード例

usingで必要なのは下記の3行です

using Amazon;
using Amazon.CognitoIdentityProvider;
using Amazon.Extensions.CognitoAuthentication;
private void button1_Click(object sender, EventArgs e)
{
	string poolId = "ap-northeast-1_さきほど用意したプールID";
	string clientId = "さきほど用意したアプリクライアントID";
	string userId = textBox1.Text; //Cognitoでユーザー追加設定したユーザー名
	string passWd = textBox2.Text; //Cognitoでユーザー追加設定したパスワード
	AmazonCognitoIdentityProviderClient amazonCognitoIdentityProviderClient1 = new(new Amazon.Runtime.AnonymousAWSCredentials(), RegionEndpoint.APNortheast1);
	CognitoUserPool cognitoUserPool1 = new(poolId, clientId, amazonCognitoIdentityProviderClient1);
	CognitoUser cognitoUser1 = new(userId, clientId, cognitoUserPool1,amazonCognitoIdentityProviderClient1);

	InitiateSrpAuthRequest initiateSrpAuthRequest1 = new()
	{
		Password = passWd
	};
	var authResponse = cognitoUser1.StartWithSrpAuthAsync(initiateSrpAuthRequest1).ConfigureAwait(true).GetAwaiter();        
	var _idToken = authResponse.GetResult().AuthenticationResult;
	var _getUserResponse = cognitoUser1.GetUserDetailsAsync().ConfigureAwait(false).GetAwaiter();
	var _userResponse = _getUserResponse.GetResult();
	textBox3.Clear();
	_userResponse.UserAttributes.ForEach(_v => {
		textBox3.Text += string.Format("{0}={1}\r\n", _v.Name, _v.Value);
	});
}

async / awaitは利用していませんが、非同期処理実装も可能です。AWSSDKの場合GetAwaiter() / GetResult()を使います。

Amazon Cognito ユーザーデータ取得画面