Archlinux上使用MySQL(MariaDB)
Dionysen

MySQL 是 Oracle 开发的,分布广泛的多线程,多用户 SQL 数据库。

Arch Linux 支持 MariaDB,这是 MySQL 的社区开发分支,旨在实现嵌入式兼容性。Oracle 的 MySQL 降到AURmysqlAUR。另一个旨在完全兼容的分支是 Percona Server,可从 percona-server包 获得。

Percona 也有 Oracle 的 InnoDB 存储引擎的分支,称为 XtraDBMariaDB 和 Percona Server 都使用此分支。

在Arch Linux上使用MySQL,可以使用官方库中的MariaDB,也可以使用AUR中的MySQL。

安装MySQL

打开终端,并使用以下命令安装MySQL

sudo pacman -S mysql
# Actually installed mariadb
# or
yay -S mysql

如果安装的是mariadb,那么接下来所有命令中的mysql都用mariadb替换

初始化

安装完成后,运行以下命令来初始化MySQL数据库:

sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

启动MySQL服务:

启动MySQL服务

sudo systemctl start mysqld

若要设置开机自启动,运行以下命令:

sudo systemctl enable mysqld

设置root用户密码

运行以下命令来设置root用户的密码:

sudo mysql_secure_installation

按照提示,设置root密码并进行其他安全设置。

连接到MySQL服务器

运行以下命令以使用root用户连接到MySQL服务器:

mysql -u root -p

输入先前设置的root密码以进行身份验证。

创建和管理数据库

连接到MySQL服务器后,您可以使用SQL命令创建和管理数据库。以下是一些常用的命令示例:

创建数据库:

CREATE DATABASE mydatabase;

使用数据库:

USE mydatabase;

创建表:

CREATE TABLE mytable (
id INT PRIMARY KEY,
name VARCHAR(50)
);

插入数据:

INSERT INTO mytable (id, name) VALUES (1, 'John');

查询数据:

SELECT * FROM mytable;

更新数据:

UPDATE mytable SET name = 'Alice' WHERE id = 1;

删除数据:

DELETE FROM mytable WHERE id = 1;

客户端常用命令

连接到mysql

mysql -u root -p

输入\h查看帮助:

List of all client commands:
Note that all text commands must be first on line and end with ';'
? (\?) Synonym for `help'.
clear (\c) Clear the current input statement.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit (\e) Edit command with $EDITOR.
ego (\G) Send command to MariaDB server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to MariaDB server.
help (\h) Display this help.
nopager (\n) Disable pager, print to stdout.
notee (\t) Don't write into outfile.
pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
costs (\Q) Toggle showing query costs after each query
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
system (\!) Execute a system shell command.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.

For server side help, type 'help contents'

简单使用

连接:

mysql -u root -p

数据库操作:

CREATE DATABASE </database_name>; 	# 创建
DROP DATABASE </database_name>; # 删除
USE </database_name>; # 选择

数据表操作:

CREATE TABLE </table_name> (column_name column_type);	# 创建表
CREATE TABLE IF NOT EXISTS </table_name> (column_name column_type); # 不存在则创建表
DROP TABLE </table_name>; # 删除表
INSERT INTO </table_name> ( field1, field2,...fieldN )
VALUES
( value1, value2,...valueN ); # 表中插入数据
SELECT * FROM </table_name>; # 从表中读取数据

使用C++调用

linux上下载mysqlcppconnector,将头文件和库文件放到相应位置,xmake.lua中添加add_links("mysqlcppconn")

#include <iostream>
#include <jdbc/cppconn/resultset.h>
#include <jdbc/cppconn/statement.h>
#include <jdbc/mysql_connection.h>
#include <jdbc/mysql_driver.h>
#include <string>

int main(int argc, char **argv) {
sql::mysql::MySQL_Driver *driver;
sql::Connection *con;

driver = sql::mysql::get_mysql_driver_instance();
con = driver->connect("tcp://0.0.0.0:3306", "root", "140917");
sql::Statement *stmt;
sql::ResultSet *res;

stmt = con->createStatement();
stmt->execute("USE mydatabase");

// 创建表
stmt->execute(
"CREATE TABLE IF NOT EXISTS mytable (id INT, name VARCHAR(100))");

// 插入数据
stmt->execute("INSERT INTO mytable (id, name) VALUES (1, 'John')");
stmt->execute("INSERT INTO mytable (id, name) VALUES (2, 'Jane')");

// 查询并打印表内容
res = stmt->executeQuery("SELECT * FROM mytable");

std::cout << "Table Content:" << std::endl;
while (res->next()) {
int id = res->getInt("id");
std::string name = res->getString("name");
std::cout << "ID: " << id << ", Name: " << name << std::endl;
}

delete res;
delete stmt;
con->close();
delete con;

return 0;
}

运行结果为:

Table Content:
ID: 1, Name: John
ID: 2, Name: Jane
显示评论