MySQL 5.7 セットアップ
アーカイブファイルの取得
高速ダウンロードのためにaria2を活用します。このツールは複数プロトコルとマルチスレッド Downloads をサポートしています。
sudo apt install aria2
aria2c https://downloads.mysql.com/archives/get/p/23/file/mysql-server_5.7.29-1ubuntu18.04_amd64.deb-bundle.tar
ダウンロード完了後、アーカイブを展開します。
sudo tar -xvf mysql-server_5.7.29-1ubuntu18.04_amd64.deb-bundle.tar
既存のMySQL環境の移除
以下のコマンド群により、完全アンインストールを実行します。設定ファイル 含めて全て削除されます。
apt-get install sudo -y
sudo apt-get purge msql-*
sudo apt-get purge mysql-server mysql-client mysql-common mysql-server-core-* mysql-client-core-*
sudo rm -rf /etc/mysql/ /var/lib/mysql
sudo apt autoremove
sudo apt autoclean
ライブラリ依存関係の解決
sudo apt install ./libmysql*
sudo apt install libtinfo5
本体コンポーネントの導入
クライアントとサーバーを個別にインストールします。community editionのインストール prompter が表示される場合があります。
sudo apt-get install ./mysql-community-client_5.7.29-1ubuntu18.04_amd64.deb
sudo apt-get install ./mysql-client_5.7.29-1ubuntu18.04_amd64.deb
sudo apt-get install ./mysql-community-server_5.7.29-1ubuntu18.04_amd64.deb
sudo apt-get install ./mysql-server_5.7.29-1ubuntu18.04_amd64.deb
インストール中にrootアカウントのパスワード設定を求められます。任意のパスワードを入力してください。
サービス起動の確認
systemctl status mysql.service
绿色的「active (running)」が表示されれば正常に起動しています。確認後はCtrl+Cで終了します。
データベースへのアクセス
rootユーザーでログインし、パスワード認証方式へ変更します。
$ sudo mysql -u root
> ALTER USER 'root'@'localhost' IDENTIFIED BY 'your_password';
FLUSH PRIVILEGES;
パスワード指定でのログイン:
sudo mysql -uroot -pyour_password
サンプルデータベースの構築
テスト用のデータベースとテーブルを作成します。
create database GomokuDB;
use GomokuDB;
CREATE TABLE users (
id INT(11) NOT NULL AUTO_INCREMENT,
username VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);
リモート接続の許可
rootユーザーのホストアクセス範囲を拡大し、リモート接続を有効化します。
UPDATE mysql.user SET host = '%' WHERE user = 'root' AND host = 'localhost';
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'your_password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
ネットワーク設定ファイルも修正する必要があります。
sudo sed -i "s/^bind-address\s*=.*/bind-address = 0.0.0.0/" /etc/mysql/mysql.conf.d/mysqld.cnf
sudo systemctl restart mysql
sudo ufw allow 3306
C/C++ 開発用ライブラリ
sudo apt install libmysqlcppconn-dev
MySQL 8.0 導入
sudo apt-get install mysql-server
sudo apt-get install mysql-client
sudo apt-get install libmysqlclient-dev
C++によるMySQL接続実装
ビルド設定
CMakeを使用する場合:
add_executable(DemoApp main.cpp)
target_link_libraries(DemoApp mysqlclient)
直接g++でコンパイルする場合:
g++ -o test test.c `mysql_config --cflags --libs`
接続示例コード
#include <iostream>
#include <mysql/mysql.h>
int main() {
MYSQL* conn = mysql_init(nullptr);
if (!conn) {
std::cerr << "MySQL 初期化エラー!" << std::endl;
return 1;
}
const char* server = "localhost";
const char* user = "root";
const char* passwd = "your_password";
const char* database = "GomokuDB";
unsigned int port = 3306;
if (!mysql_real_connect(conn, server, user, passwd,
database, port, nullptr, 0)) {
std::cerr << "接続失敗: " << mysql_error(conn) << std::endl;
mysql_close(conn);
return 1;
}
std::cout << "MySQLサーバーへの接続に成功しました!" << std::endl;
mysql_close(conn);
return 0;
}
クエリ実行例
int execute_query(MYSQL* connection, const char* sql) {
if (mysql_query(connection, sql)) {
std::cerr << "クエリエラー: " << mysql_error(connection) << std::endl;
return -1;
}
return 0;
}