Ubuntu Linux系统MariaDB10.3源码编译安装笔记

MySQL和MariaDB可算是真正的亲姐妹,二者分别取名自“MySQL之父”Michael Widenius(江湖上称作Monty)的大女儿My Widenius(与前妻所生)和小女儿Maria Widenius(与现任妻子所生)。Oracle收购Sun公司后,之前被Monty卖给Sun的MySQL也被Oracle继承了。Oracle是世界上最大的商业数据库厂商,因为担心Oracle独裁MySQL和未来闭源的不确定性,在“拯救MySQL”活动的推动下,Monty又领导了MariaDB的开发和推广。MariaDB是原MySQL的最大分支,源码编译安装流程基本继承自MySQL。之前的工作中使用的RDBMS都是MySQL,现在新的项目要求使用MariaDB,就在本地先试着安装一下。我的系统环境是Ubuntu 18.04(LTS)。因为已经源码编译安装了MySQL5.7,所以安装MariaDB时,需要一些额外的修改。这里记录一下安装过程。

①安装依赖

源码编译安装需要若干Linux环境依赖,Ubuntu系统详细参考https://mariadb.com/kb/en/library/building-mariadb-on-ubuntu/。把里面的命令复制到终端分别跑一下即可。

apt install software-properties-common devscripts equivs
apt-key adv --recv-keys --keyserver hkp://keyserver.ubuntu.com:80 0xF1656F24C74CD1D8
add-apt-repository --update --yes --enable-source 'deb [arch=amd64] http://nyc2.mirrors.digitalocean.com/mariadb/repo/10.3/ubuntu '$(lsb_release -sc)' main'
apt-get build-dep mariadb-10.3

②下载源码

当前10.3是Stable版本,10.4还是RC,就下载10.3吧https://downloads.mariadb.org/mariadb/。

wget http://ftp.yz.yamagata-u.ac.jp/pub/dbms/mariadb//mariadb-10.3.14/source/mariadb-10.3.14.tar.gz
tar -xz -f mariadb-10.3.14.tar.gz
cd mariadb-10.3.14/ ; ls

③创建用户

如果本地没有安装MySQL,以下命令中的mariadb可以改成mysql。

groupadd mariadb
useradd -g mariadb mariadb
mkdir -p /usr/local/mariadb/data
chown -R mariadb /usr/local/mariadb
chgrp -R mariadb /usr/local/mariadb

④编译安装

MySQL默认的全局配置文件是在“/etc/my.cnf”,MariaDB仍然沿用。但是因为本地已经有运行的MySQL,为避免冲突,通过“-DEFAULT_SYSCONFDIR”选项重新指定配置文件目录。如果本地没有安装MySQL,该选项省略即可,命令中的mariadb也可以改成mysql。

cmake -DEFAULT_SYSCONFDIR=/opt -DCMAKE_INSTALL_PREFIX=/usr/local/mariadb -DMYSQL_DATADIR=/usr/local/mariadb/data -DDEFAULT_CHARSET=utf8 -DDEFAULT_COLLATION=utf8_general_ci -DEXTRA_CHARSETS=all
make && make install

⑤初始服务

首先手动写一个简单的配置文件,因为原MySQL的3306端口已经被占用,重新指定一个新端口。

vim /opt/my.cnf


[client]
port = 6033
socket = /tmp/mariadb.sock

[mysqld]
socket = /tmp/mariadb.sock
basedir = /usr/local/mariadb
datadir = /usr/local/mariadb/data
pid_file = /usr/local/mariadb/data/linux.pid

然后初始化MariaDB安装,详细参考https://mariadb.com/kb/en/library/mysql_install_db/。在初始化的过程中通过“--defaults-file”选项指定配置文件。

/usr/local/mariadb/scripts/mysql_install_db --defaults-file=/opt/my.cnf --user=mariadb --basedir=/usr/local/mariadb --datadir=/usr/local/mariadb/data

初始化完成后就可以启动MariaDB了。本地因为已经将MySQL写入service,就参考安装文档的说明https://mariadb.com/kb/en/library/generic-build-instructions/,手动启动了。

/usr/local/mariadb/bin/mysqld_safe --defaults-file=/opt/my.cnf --port=6033 --user=mariadb &

如果本地没有MySQL,可以把MariaDB写入开机服务,例如:

cp /usr/local/mariadb/support-files/mysql.server /etc/init.d/mysqld
chmod +x /etc/init.d/mysqld
update-rc.d mysqld defaults
service mysqld start

服务开启后,进程列表中就能看到MySQL / MariaDB的守护进程daemon了。接下来运行一个MariaDB自带的数据库安全安装处理脚本。详细参考https://mariadb.com/kb/en/library/mysql_secure_installation/。该脚本是为数据库安装用于生产环境而准备的,主要做以下四件工作:

  • 为root账户设置密码
  • 设置禁止root账户在localhost以外登录
  • 移除匿名账户(anonymous-user)
  • 移除测试库(测试库默认可被匿名账户连接访问而用于Debug)
/usr/local/mariadb/bin/mysql_secure_installation --defaults-file=/opt/my.cnf

该程序会接收若干手动输入,具体如下。

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none): ( ˇˍˇ )这里回车(不是还没设置过root密码吗)( ˇˍˇ )
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] y ( ˇˍˇ )这里输入y开始设置root密码( ˇˍˇ )
New password: 
Re-enter new password: ( ˇˍˇ )连续输入两次密码(要记住密码啊)( ˇˍˇ )
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y ( ˇˍˇ )这里移除匿名账户( ˇˍˇ )
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n ( ˇˍˇ )是否禁止root账户远程访问(本地自己用当然不要啦 / 生产环境的话选择y)( ˇˍˇ )
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] y ( ˇˍˇ )移除测试库( ˇˍˇ )
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y ( ˇˍˇ )重新加载权限表( ˇˍˇ )
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

⑥连接配置

到这里就可以连接使用MariaDB了,首先建个软连接。

ln -s /usr/local/mariadb/bin/mysql /usr/bin/mariadb

终端命令行连接。

mariadb --defaults-file=/opt/my.cnf --user=root --password=1024

这是第一次登录MariaDB客户端窗口。

root@linux:~/mariadb-10.3.14# mariadb --defaults-file=/opt/my.cnf --user=root --password=1024
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 23
Server version: 10.3.14-MariaDB Source 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)]>

因为本地测试用,可以把账户和密码信息写入配置文件[client]组下面,以后连接就不用输入了。

vim /opt/my.cnf


[client]
user = root
password = 1024
port = 6033
socket = /tmp/mariadb.sock

修改完配置文件需要重启生效。

/usr/local/mariadb/bin/mysqladmin --defaults-file=/opt/my.cnf --user=root --password=1024 shutdown
/usr/local/mariadb/bin/mysqld_safe --defaults-file=/opt/my.cnf --port=6033 --user=mariadb &

最后连接MariaDB修改`user`权限表,允许root账户远程连接。

root@linux:~/mariadb-10.3.14# mariadb --defaults-file=/opt/my.cnf
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 10.3.14-MariaDB Source 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)]> SELECT `Host` FROM `mysql`.`user` WHERE `User` = 'root'\G
*************************** 1. row ***************************
Host: 127.0.0.1
*************************** 2. row ***************************
Host: ::1
*************************** 3. row ***************************
Host: linux
*************************** 4. row ***************************
Host: localhost
4 rows in set (0.001 sec)

MariaDB [(none)]> UPDATE `mysql`.`user` SET `Host` = '%' WHERE `User` = 'root' AND `Host` = 'linux';
Query OK, 1 row affected (0.002 sec)
Rows matched: 1  Changed: 1  Warnings: 0

MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> 

到此,该MariaDB就可以使用其他数据库客户端工具以root账户远程连接访问了。

你可能感兴趣的:(#,MySQL,/,MariaDB,#,Unix,/,Linux)