0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

MySQL・MSSQL・PostgreSQLで接続するまでの流れまとめ

Posted at

環境

Almalinux9.2でアクセスする事を想定する。

mySql

Linuxから、IPアドレスを指定してmysqlでアクセスするには。

まずは、mysql-client をインストールする必要がある。今回は、mysqlサーバとして使用しないので以下コマンドのみ。

sudo yum install mysql

その後、以下のコマンドを実行する。

mysql -h(ホスト名) -u(ユーザー名) -p -D(データベース名)

ポートを指定するなら、-Pを指定する。デフォルトは3306である。

実行例
mysql -h 192.168.219.212 -u ngpf -p passwd -D dbname
mysql -h 192.168.219.212 -u ngpf -p passwd -P 3306 -D dbname

MSSQL接続

LinuxからMSSQLに接続するには以下のツールをインストールする。

まずはリポジトリを入れる。

curl https://packages.microsoft.com/config/rhel/9/prod.repo | sudo tee /etc/yum.repos.d/mssql-release.repo

旧版がまだある場合はremoveを行う。

sudo yum remove mssql-tools unixODBC-utf16 unixODBC-utf16-devel

その後、インストールする。

sudo yum install -y mssql-tools18 unixODBC-devel

whichでコマンドパスを見ると、sqlcmdのパスはここになっているらしい。

[ito@hostname ~]$ which sqlcmd
/opt/mssql-tools18/bin/sqlcmd
[ito@hostname ~]$

なので、パスを通す必要がある。

vi ~/.bashrc

で、こんな感じに編集しなおす。

編集箇所
export PATH=$PATH:/opt/mssql-tools18/bin/sqlcmd

その後、アクセスしてみる。-Sでホスト名を指定する。

sqlcmd -S <SQL Serverのホスト名またはIPアドレス> -U <ユーザー名> -d YourDatabase
実行例
sqlcmd -S 192.168.169.100 -U username -d dbname

実行すると、データベースのパスワードを求められるので入れる。これで実行すると、以下のように言われる。これは、SSLの証明書がうまくいかなかったことであるらしいので、-Cをつけてみる。

sqlcmd -S 192.168.169.100 -U username -d dbanme -C

その後、クエリを実行するには、クエリを入れた後にgoを入力する。

2> SELECT top 1 * FROM FILEMETA WHERE cid=3672 and mid=1593
3> go

PostgreSQL

まずは、リポジトリをダウンロードする。

sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm

その後、ビルトインのポスグレを無効化する。

sudo dnf -qy module disable postgresql

以下コマンドでインストールする。

sudo dnf install -y postgresql

すると、psqlが使える。MSSQLより楽!

[username@hostname ~]$ which psql
/usr/bin/psql
[username@hostname ~]$

これを使って接続する。

psql -h ホスト名 -p ポート番号 -U ユーザ名 -d データベース名
実行例
psql -h 192.168.100.100 -p 5432 -U username -d dbname
パスワードを入力後、クエリを実行できる。

参考サイト

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?