安装mariadb,输入指令

1
pkg install mariadb -y

安装后查看是否存在目录my.cnf.d ,如果没有的话可能会失败,这时候我们就需要为其手动创建一个文件夹。

可以cd一下查看是否存在不存在的话就创建它,没有报错cd的进去的话就是存在该目录,则可以继续下一步

查看命令
1
cd /data/data/com.termux/files/usr/etc/my.cnf.d
如果不存在,则创建:
1
mkdir /data/data/com.termux/files/usr/etc/my.cnf.d

启动数据库

启动前初始化一下数据库,mysql_install_db ,初始化完毕后输入启动命令

1
mysqld

输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
~ $ mysqld
mysqld: Deprecated program name. It will be removed in a future release, use '/data/data/com.termux/files/usr/bin/mariadbd' instead
2024-07-16 17:43:38 0 [Note] Starting MariaDB 11.1.2-MariaDB source revision 9bc25d98209df6810f7a7d5e7dd3ae677a313ab5 as process 13403
2024-07-16 17:43:38 0 [Note] InnoDB: Compressed tables use zlib 1.3
2024-07-16 17:43:38 0 [Note] InnoDB: Number of transaction pools: 1
2024-07-16 17:43:38 0 [Note] InnoDB: Using generic crc32 instructions
2024-07-16 17:43:38 0 [Note] InnoDB: Initializing buffer pool, total size = 128.000MiB, chunk size = 2.000MiB
2024-07-16 17:43:38 0 [Note] InnoDB: Completed initialization of buffer pool
2024-07-16 17:43:39 0 [Note] InnoDB: Buffered log writes (block size=512 bytes)
2024-07-16 17:43:39 0 [Note] InnoDB: End of log at LSN=47311
2024-07-16 17:43:39 0 [Note] InnoDB: Opened 3 undo tablespaces
2024-07-16 17:43:39 0 [Note] InnoDB: 128 rollback segments in 3 undo tablespaces are active.
2024-07-16 17:43:39 0 [Note] InnoDB: Setting file './ibtmp1' size to 12.000MiB. Physically writing the file full; Please wait ...
2024-07-16 17:43:39 0 [Note] InnoDB: File './ibtmp1' size is now 12.000MiB.
2024-07-16 17:43:39 0 [Note] InnoDB: log sequence number 47311; transaction id 14
2024-07-16 17:43:39 0 [Note] InnoDB: Loading buffer pool(s) from /data/data/com.termux/files/usr/var/lib/mysql/ib_buffer_pool
2024-07-16 17:43:39 0 [Note] Plugin 'FEEDBACK' is disabled.
2024-07-16 17:43:39 0 [Note] Server socket created on IP: '::'.
2024-07-16 17:43:39 0 [Note] Server socket created on IP: '0.0.0.0'.
2024-07-16 17:43:39 0 [Note] mysqld: Event Scheduler: Loaded 0 events
2024-07-16 17:43:39 0 [Note] InnoDB: Buffer pool(s) load completed at 240716 17:43:39
2024-07-16 17:43:39 0 [Note] mysqld: ready for connections.
Version: '11.1.2-MariaDB' socket: '/data/data/com.termux/files/usr/var/run/mysqld.sock' port: 3306 MariaDB Server

访问数据库

数据库访问

1
mysql -uroot -p

输入后不用输入密码直接回车就好,默认是没有密码的。

登陆后的输出结果:

1
2
3
4
5
6
7
8
9
10
11
12
13
~ $ mysql -uroot -p
mysql: Deprecated program name. It will be removed in a future release, use '/data/data/com.termux/files/usr/bin/mariadb' instead
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 11.1.2-MariaDB MariaDB Server

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)]>

设置密码

输入命令:

1
ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';

输出结果:

1
2
MariaDB [(none)]> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.050 sec)

开启远程访问

方法一:

允许所有主机连接,在mysql的时候我们通常使用一下命令来开启

1
2
3
use mysql;
update user set Host='%' where User='root';
flush privileges;

无疑例外的会出现错误

1
1356 - View 'mysql.user' references invalid table(s) or column(s) or function(s) or definer/invoker of view lack rights to use them

这是因为现在的是mariadb了,这个就不适用了,因为mariadb中没有了user表,而是移动到了视图中,且这个命令是用来表的自然不适用于修改视图。mysql.user从10.4版本开始提供了一个新表mysql.global_priv来替代mysql.user. 则修改命令为:

1
2
UPDATE mysql.global_priv SET Host='%' WHERE User='root';
flush privileges;

方法二:
授权方式,授权所有主机连接

1
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

或者授权制定主机访问(推荐),例如指定授权给192.168.1.3主机使用root用户登录

1
GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.168.1.3' IDENTIFIED BY '12346' WITH GRANT OPTION; 
  • GRANT ALL PRIVILEGES: 表示授予所有权限。
  • ON *.*: 表示权限适用于所有数据库(*.* 表示所有数据库和所有表)。
  • TO 'root'@'%': 表示授予权限给用户名为 root,并且可以从任何主机('%' 表示通配符,代表任何主机)连接。
  • IDENTIFIED BY '123456': 表示使用密码 123456 进行身份验证。
  • WITH GRANT OPTION: 表示允许用户将自己拥有的权限授予其他用户

修改配置(可选)

先对配置文件进行备份

1
cp /data/data/com.termux/files/usr/etc/my.cnf /data/data/com.termux/files/usr/etc/my.cnf.back

备份后再输入以下命令来进行修改配置文件

1
vim /data/data/com.termux/files/usr/etc/my.cnf

以下是我配置的内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[mysqld]

# 监听的ip地址,127.0.0.1,仅限本机访问,0.0.0.0 全地址可以访问(任何主机)
bind-address=0.0.0.0
skip_host_cache
skip-name-resolve=1
#设置端口号
port=3306

#设置mysql运行用户
user=root


# 允许最大连接数
max_connections=10000

# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10

# 服务器使用的默认字符集
character_set_server=utf8mb4

# 建议禁用符号链接,以防止各种安全风险
symbolic-links=0

# 设置时区
default-time_zone='+8:00'

# 不使用SSL
skip_ssl

#TIMESTAMP类型和其他的类型有点不一样,不添加该项配置,否则启动时,会报警
explicit_defaults_for_timestamp=true

[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4

使用进程守护运行

开启进程守护命令:

1
nohup mysqld &

当前目录会生成一个文件nohup.out 用来存储日志。可以用来tail -f nohup.out查看日志。

结束运行

查看运行的pid

1
ps -ef |grep mysql

输出结果:

1
2
u0_a181  11106  7164  1  1970 pts/3    00:00:00 grep mysql
u0_a181 13403 1 0 1970 ? 00:00:01 mysqld

杀掉进程13403,就算是结束掉进程了

1
kill -9 13403