在FreeBSD 11 Unix服务器上安装MariaDB数据库
如何在基于FreeBSD Unix的系统上安装MariaDB数据库服务器?
MySQL是一种高速,多线程,多用户且功能强大的SQL数据库服务器。
MariaDB是一种数据库服务器,为Oracle MySQL服务器提供了嵌入式替换功能。
MariaDB由MySQL的某些原始作者在社区的帮助下构建。
本教程面显示了如何在FreeBSD 10/11 Unix服务器上安装MariaDB数据库。
如何在FreeBSD上安装MariaDB数据库
MariaDB服务器的安装过程如下:
- 查找MariaDB服务器软件包名称。
- 使用ports或pkg命令安装MariaDB服务器。
- 启动MariaDB服务器
- 设置MariaDB服务器的root密码
- 创建数据库,用户和授予权限
- 使用防火墙打开MariaDB端口
更新所有端口
确保您的端口已安装并且是最新的。
我喜欢做:
# portsnap fetch update && portupgrade -a
FreeBSD MariaDB安装
要安装MariaDB Server,MariaDB Client和MariaDB脚本,请执行以下命令。
有关MySQL和MariaDB服务器的说明
您不能运行同时安装的MariaDB和MySQL。
如果有,则将卸载其中一个,然后再卸载客户端。
如果要同时运行它们,则需要将它们分开监禁。
如果您是新手,最好不要考虑这一点,一次集中精力学习一个或另一个。
因此,例如,如果您具有MySQL并想安装MariaDB,则需要在安装前先卸载。
通过以超级用户身份执行命令来检查您正在使用的版本:
# pkg version | grep mysql
或以用户或root身份输入以下命令:
# mysql --version
或使用pkg命令:
# pkg info | grep mysql
确保在卸载mysql-server之前确保备份数据库。
要卸载它:
# cd /usr/ports/databases/mysql57-server/ && make deinstall clean # cd /usr/ports/databases/mysql57-client/ && make deinstall clean # rm -rf /var/db/mysql/
或使用pkg命令将其删除:
# pkg remove mysql56-server mysql56-client # rm -rf /var/db/mysql/
查找MariaDB服务器版本
运行pkg命令:
# pkg search mariadb
FreeBSD安装MariaDB服务器
要安装端口,请执行并确保您在配置中选中要关闭的内容:
# cd /usr/ports/databases/mariadb103-server/ # make install clean
或者,要使用pkg命令添加二进制软件包,请运行:
# pkg install databases/mariadb103-server
不只安装MariaDB客户端
MariaDB客户端将自动安装。
现在,您应该检查以下选项:
[X] THREADSAFE Build thread-safe client [X] SSL Activate SSL support (yassl)
但是,如果您需要在另一台FreeBSD服务器或 Jail或桌面系统上使用MariaDB客户端,请运行:
# cd /usr/ports/databases/mariadb103-client # make install clean
或者
# pkg install databases/mariadb103-client
输出示例:FreeBSD 11安装mariadb客户端
如何在启动时启动MariaDB?
执行sysrc命令以在启动时启用MaraiDB服务器服务以及其他选项:
# sysrc mysql_enable=YES # sysrc mysql_pidfile=/var/db/mysql/mysql.pid # sysrc mysql_optfile=/usr/local/etc/my.cnf
如何安装MariaDB服务器配置文件?
MariaDB尊重FreeBSD的文件系统布局(并且不检查my.cnf的/etc和/etc/mysql。
您将找到以下默认配置文件:
# ls -l /usr/local/share/mysql/my*.cnf
输出示例:
-rw-r--r-- 1 root wheel 4898 Nov 26 12:56 /usr/local/share/mysql/my-huge.cnf -rw-r--r-- 1 root wheel 20418 Nov 26 12:56 /usr/local/share/mysql/my-innodb-heavy-4G.cnf -rw-r--r-- 1 root wheel 4885 Nov 26 12:56 /usr/local/share/mysql/my-large.cnf -rw-r--r-- 1 root wheel 4898 Nov 26 12:56 /usr/local/share/mysql/my-medium.cnf -rw-r--r-- 1 root wheel 2824 Nov 26 12:56 /usr/local/share/mysql/my-small.cnf
您可以使用上述任何文件。
我总是从一个干净的配置文件开始:
# vi /usr/local/etc/my.cnf
这是我的最小my.cnf文件:
## SERVER ## [mysqld] datadir = /var/db/mysql socket = /var/db/mysql/mysql.sock pid-file = /run/mysql/mysql.pid log-error = /var/db/mysql/mysql-error.log log-queries-not-using-indexes = 1 slow-query-log = 1 slow-query-log-file = /var/db/mysql/mysql-slow.log
如何在FreeBSD 10/11上启动/停止/重启MariaDB?
要启动服务器,您将执行:
# service mysql-server start
要停止服务器,您将执行:
# service mysql-server stop
要重新启动服务器,您将执行:
# service mysql-server restart
要查看服务器状态,您将执行:
# service mysql-server status
您也可以出于相同目的使用以下命令:
## call rc.d script to control MariaDB server ## /usr/local/etc/rc.d/mysql-server start /usr/local/etc/rc.d/mysql-server stop /usr/local/etc/rc.d/mysql-server restart /usr/local/etc/rc.d/mysql-server status
在FreeBSD 10上启动/停止MariaDB服务器
如何为MariaDB设置root用户密码?
安装MariaDB后,您应该为root用户创建密码,执行:
# mysqladmin -S /var/db/mysql/mysql.sock -u root password YOURSECUREPASSWORD
另外,我建议您可以运行以下命令来设置root密码。
该命令还将为您提供删除默认情况下创建的测试数据库和匿名用户的选项。
强烈建议将其用于生产服务器:
# ln -s /var/db/mysql/mysql.sock /tmp/ # /usr/local/bin/mysql_secure_installation
输出示例:
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): 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 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] y ... Success! 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!
从/tmp /中删除文件:
# rm /tmp/mysql.sock
如何连接到MariaDB服务器?
mysql是一种简单的SQL shell,可用于控制mysql/mariadb服务器。
语法为:
mysql mysql -u user -p mysql -h db-hostname-here -u user-name-here -p
如何创建MariaDB数据库和用户?
首先,以root用户身份登录:
mysql -u root -p mysql
输出示例:
使用mysql客户端连接到服务器
创建一个名为foo的新mysql数据库。
在mysql>提示符下执行以下命令:
MariaDB [mysql]> CREATE DATABASE foo;
为名为foo的数据库创建一个名为user1的新用户,并使用一个名为hiddensecret的密码:
MariaDB [mysql]> GRANT ALL ON foo.* TO user1@localhost IDENTIFIED BY 'hiddensecret';
如何使用user1帐户连接到MariaDB数据库foo?
用户user1可以使用以下shell命令连接到foo数据库:
$ mysql -u user1 -p foo
或者
$ mysql -u user1 -h your-mysql-server-host-name-here -p foo
如何启用对MariaDB服务器的远程访问?
编辑my.cnf文件,运行:
# vi /usr/local/etc/my.cnf
确保" skip-networking"行已注释(或删除行),并在" [mysqld]"部分添加以下行:
bind-address=YOUR-SERVER-IP
例如,如果您的MariaDB FreeBSD服务器IP为192.168.1.5:
bind-address=192.168.1.5
保存并关闭文件。
重新启动服务器:
# service mysql-server restart
更新您的pf.conf文件:
## allows mysql client from 192.168.1.200 ## pass in on $ext_if proto tcp from 192.168.1.200 to any port 3306 flags S/SA synproxy state
通过执行以下任一命令,重新启动pf服务并从192.168.1.200测试连接性:
# use nc for port testing ## nc -z -w1 192.168.1.5 3306 # or old good telnet ## echo X | telnet -e X 192.168.1.5 3306 telnet -e X 192.168.1.5 3306<<<"X" ## or use mysql client ## mysql -h 192.168.1.5 -u USER -p DB
如何通过基于LAN的会话授予对现有数据库的访问权限?
让我们假设您一直在从远程IP(地址为192.168.1.200)建立连接,该数据库的用户栏为foo,要授予对该IP地址的访问权限,请在" MariaDB [mysql]>"提示符下为现有数据库执行以下命令,执行:
MariaDB [mysql]> update db set Host='192.168.1.200' where Db='foo'; MariaDB [mysql]> update user set Host='192.168.1.200' where user='bar';
如何在FreeBSD pf防火墙中打开端口
在pf.conf文件中添加以下规则:
pass in on $ext_if proto tcp from any to any port 3306
或仅允许从192.168.1.10访问题描述:
pass in on $ext_if proto tcp from 192.168.1.10 to any port 3306