数据库产品排名:https://db-engines.com/en/ranking
MariaDB知识库: https://mariadb.com/kb/en/ , 有一部分中文:https://mariadb.com/kb/zh-cn/
安装
参考 How To Install MariaDB on Ubuntu 22.04
sudo apt update
sudo apt upgrade
sudo apt install mariadb-server
sudo mysql_secure_installation
VPS开放3306端口并执行 sudo vi /etc/mysql/mariadb.conf.d/50-server.cnf
,注释掉 bind-address = 127.0.0.1
, 更新数据库表:
$ mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 32
Server version: 10.6.12-MariaDB-0ubuntu0.22.04.1 Ubuntu 22.04
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mysql]> select host,user from user where user = 'root';
+-----------+------+
| Host | User |
+-----------+------+
| localhost | root |
+-----------+------+
1 row in set (0.001 sec)
MariaDB [mysql]> UPDATE mysql.global_priv SET Host='%' WHERE User='root';
Query OK, 1 row affected (0.002 sec)
Rows matched: 1 Changed: 1 Warnings: 0
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.000 sec)
MariaDB [mysql]> select host,user from user where user = 'root';
+------+------+
| Host | User |
+------+------+
| % | root |
+------+------+
1 row in set (0.001 sec)
重启数据库 sudo service mariadb restart
WSL安装客户端: sudo apt install mariadb-client
shell下测试连接: mysql -u root -h VPS_ip -D mysql -p
python驱动,参考: https://mariadb.com/resources/blog/how-to-connect-python-programs-to-mariadb/
pip install mariadb
测试脚本:
import mariadb
import sys
db_user='root'
db_user_passwd = 'db_password'
db_host_ip = "db_ip"
# Connect to MariaDB Platform
try:
conn = mariadb.connect(
user=db_user,
password=db_user_passwd,
host=db_host_ip,
port=3306,
database="mysql"
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB Platform: {e}")
sys.exit(1)
# Get Cursor
cur = conn.cursor()
cur.execute(
"SELECT host,user FROM user WHERE user=?", ('root',))
# Print Result-set
for (host, user) in cur:
print(f"Host: {host}, User: {user}")
# Close Connection
conn.close()
输出:Host: %, User: root
新建数据库用户并赋权
VPS上登录到root,更新root仅允许本地访问,创建新用户test_user_name可以从VPS外访问,将数据库db_name的权限赋予该用户
mysql -u root -p
UPDATE mysql.global_priv SET Host='localhost' WHERE User='root';
CREATE USER 'test_user_name'@'%' IDENTIFIED BY 'test_user_name_password';
GRANT ALL ON db_name.* to test_user_name@'%' IDENTIFIED BY 'test_user_name_password';
FLUSH privileges;
pycharm 客户端
视图,工具窗口,数据库,添加数据库
使用ORM: SQLAlchemy
1.数据库驱动
参考:https://mariadb.com/resources/blog/using-sqlalchemy-with-mariadb-connector-python-part-1/ ,安装 MariaDB Connector/C 参考:https://mariadb.com/docs/skysql/connect/programming-languages/c/install/
2.安装sqlalchemy: conda install sqlalchemy
win10下使用
- 下载安装:https://mariadb.org/download/
- 参考版本支持时间下载最长支持版本:https://mariadb.org/about/#maintenance-policy
- 下载zip包, 将解压目录加入系统path
D:\mariadb-10.6.10-winx64\bin
- windows客户端:SQLyog Community Edition,https://github.com/webyog/sqlyog-community
- 安装服务:以管理员身份运行终端,运行下面命令
>mysql_install_db.exe --service=MariaDB --password=password
Default data directory is D:\mariadb-10.6.10-winx64\data
Running bootstrap
Registering service 'MariaDB'
Creating my.ini file
2022-10-06 15:10:07 0 [Note] D:\mariadb-10.6.10-winx64\bin\mysqld.exe (server 10.6.10-MariaDB) starting as process 67812 ...
Removing default user
Setting root password
Creation of the database was successful
启动/停止服务:管理员身份在命令行运行 sc start MariaDB,sc start MariaDB
MariaDB 登录、退出 MariaDB
C:\>mysql -uroot -ppassword
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 5
Server version: 10.6.10-MariaDB mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.001 sec)
MariaDB [(none)]> quit
Bye
MariaDB 数据类型
参考:https://www.w3cschool.cn/mariadb/mariadb_data_types.html
数字数据类型
TINYINT - 此数据类型表示落入-128到127的有符号范围内的小整数,以及0到255的无符号范围。
BOOLEAN - 此数据类型将值0与“false”相关联,值1与“true”相关联。
SMALLINT - 此数据类型表示-32768到32768的有符号范围内的整数,以及0到65535的无符号范围。
MEDIUMINT - 此数据类型表示有符号范围-8388608到8388607中的整数,无符号范围0到16777215。
INT(也为INTEGER) - 此数据类型表示正常大小的整数。当标记为unsigned时,范围跨越0到4294967295.当有符号(默认设置)时,范围跨越-2147483648到2147483647.当列设置为ZEROFILL(无符号状态)时,其所有值都由零添加INT值中的M个数字。
BIGINT - 此数据类型表示有符号范围9223372036854775808到9223372036854775807内的整数,无符号范围0到18446744073709551615。
DECIMAL(DEC,NUMERIC,FIXED) - 该数据类型表示精确的定点数,M指定其数字,D指定小数后的数字。 M值不添加“ - ”或小数点。如果D设置为0,则不会出现小数或小数部分,并且该值将舍入为最接近的DECIMAL INSERT。最大允许位数为65,小数位数的最大值为30.默认值M的默认值为10,省略时D为0。
FLOAT - 此数据类型表示值0的小的浮点数或以下范围内的数字 :
-3.402823466E + 38至-1.175494351E-38
1.175494351E-38至3.402823466E + 38
DOUBLE(也是REAL和DOUBLE PRECISION) - 此数据类型表示值0的正常大小的浮点数,或以下范围内的值 -
-1.7976931348623157E + 308至-2.2250738585072014E-308
2.2250738585072014E-308至1.7976931348623157E + 308
BIT - 此数据类型表示位字段,M指定每个值的位数。省略M时,默认值为1.位值可以通过“b'[value]'”应用,其中值表示0和1中的位值。零填充从左边自动发生全长;例如,“10”变为“0010”。
日期和时间数据类型
MariaDB支持的日期和时间数据类型如下 -
DATE - 此数据类型表示日期范围“1000-01-01”到“9999-12-31”,并使用“YYYY-MM-DD”日期格式。
TIME - 此数据类型表示“-838:59:59.999999”到“838:59:59.999999”的时间范围。
DATETIME - 此数据类型表示范围“1000-01-01 00:00:00.000000”至“9999-12-31 23:59:59.999999”。它使用“YYYY-MM-DD HH:MM:SS”格式 。
TIMESTAMP - 此数据类型表示“YYYY-MM-DD HH:MM:DD”格式的时间戳。 它主要用于详细描述数据库修改的时间,例如插入或更新。
YEAR - 此数据类型表示4位数格式的年份。 四位数格式允许在1901到2155和0000范围内的值。
字符串数据类型
MariaDB支持的字符串类型值如下 -
String literals - 此数据类型表示用引号括起来的字符序列。
CHAR - 此数据类型表示包含指定长度的空格的右侧带有固定长度的字符串。 M表示字符的列长度,取值范围为0〜255,缺省值为1。
VARCHAR - 此数据类型表示一个可变长度字符串,M范围(最大列长度)为0到65535。
BINARY - 此数据类型表示二进制字节字符串,M为列长度(以字节为单位)。
VARBINARY - 此数据类型表示可变长度的二进制字节字符串,M为列长度。
TINYBLOB - 此数据类型表示最大长度为255(28 - 1)个字节的blob列。在存储中,每个都使用一个字节长度的前缀,表示值中的字节数量。
BLOB - 此数据类型表示最大长度为65,535(216 - 1)个字节的blob列。在存储中,每个都使用两字节长度的前缀,表示值中的字节数量。
MEDIUMBLOB - 此数据类型表示最大长度为16,777,215(224 - 1)个字节的blob列。在存储中,每个都使用一个三字节长度前缀,表示值中的字节数量。
LONGBLOB - 此数据类型表示最大长度为4,294,967,295(232 - 1)个字节的blob列。在存储中,每个使用四字节长度的前缀,表示值中的字节数量。
TINYTEXT - 此数据类型表示最大长度为255(28 - 1)个字符的文本列。在存储中,每个都使用一个字节长度的前缀,表示值中的字节数量。
TEXT - 此数据类型表示最大长度为65,535(216 - 1)个字符的文本列。在存储中,每个都使用两字节长度的前缀,表示值中的字节数量。
MEDIUMTEXT - 此数据类型表示最大长度为16,777,215(224 - 1)个字符的文本列。在存储中,每个都使用三字节长度前缀,表示值中的字节数量。
LONGTEXT - 此数据类型表示最大长度为4,294,967,295或4GB(232 - 1)个字符的文本列。在存储中,每个使用四字节长度的前缀,表示值中的字节数量。
ENUM - 此数据类型表示一个列表中只有一个值的字符串对象。
SET - 此数据类型表示一个列表中具有零个或多个值的字符串对象,最多包含64个成员。 SET值在内部作为整数值存在。
python环境的使用
安装Python连接器:https://mariadb-corporation.github.io/mariadb-connector-python/
基本使用: https://mariadb-corporation.github.io/mariadb-connector-python/usage.html
一个完整示例:
# -*- coding: utf-8 -*-
# test01.py
import mariadb
# connection parameters
conn_params= {
"user" : "root",
"password" : "password",
"host" : "localhost",
}
# Establish a connection
connection= mariadb.connect(**conn_params)
cursor= connection.cursor()
#准备数据库 test
cursor.execute("show databases;")
row= cursor.fetchall()
db_list=set([item[0] for item in row])
if 'test' not in db_list:
cursor.execute('create database test;')
cursor.execute('use test;')
# 准备表
SQL ="""CREATE TABLE IF NOT EXISTS countries(
name VARCHAR(100) NOT NULL,
country_code CHAR(3) NOT NULL,
capital VARCHAR(100) NOT NULL,
PRIMARY KEY (country_code)
);"""
cursor.execute(SQL)
# 准备数据
# Populate countries table with some data
cursor.execute("INSERT INTO countries(name, country_code, capital) VALUES (?,?,?)", ("Germany", "GER", "Berlin"))
# 查询数据
# retrieve data
cursor.execute("SELECT name, country_code, capital FROM countries")
# print content
row= cursor.fetchone()
print(*row, sep=' ') # Germany GER Berlin
# free resources
cursor.close()
connection.close()