centos7.6二进制安装mariadb10.4.11

写在前面

mariadb是mysql的一个分支,由MySQL社区进行维护更新. 在这里记录我的一次安装过程.
安装libaio库文件
yum install libaio* -y
下载二进制包(mariadb10.4.11下载列表)

注意: 要选择符合自己服务器的操作系统,以及拓展(liunx里可以使用 uname -a 查看是64位还是32位),镜像的话选择:清华大学TUNA协会(清华大学TUNA协会)

安装

因为二进制包是编译好的,我们需要做的就是初始化环境,比如创建mysql账户,创建数据存储的目录等.
附上官方提供的帮助手册:
https://mariadb.com/kb/en/library/installing-mariadb-binary-tarballs/

创建mysql账户

官方编译时,默认指定了运行数据库程序的用户为:mysql. 我们需要创建mysql账户

useradd mysql
创建mariadb程序目录

官方编译时,默认指定的Mariadb的目录为:/usr/local/mysql. 我们可以把压缩包解压至:/usr/local/mysql,不过更通用的办法是建立一个软连接:

tar -zxvf   mariadb-10.4.10-linux-x86_64.tar.gz   #解压mariadb包

ln -s mariadb-10.4.10-linux-x86_64 /usr/local/mysql -r   建立软连接
初始化数据库

假如以/data/mariadb_data 作为数据库存放数据的地方

mkdir /data/mariadb_data -p

前面说了,官方编译时,指的的运行用户是mysql,所以我们刚才创建的目录,mysql需要有读写权限,我们直接把属组属主改为mysql:

chown root.root /data/mariadb_data -R

进入到mysql目录cd /usr/local/mysql,执行以下命令:

./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mariadb_data
Installing MariaDB/MySQL system tables in '/data/mariadb_data' ...
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system.....

--user 是用mysql用户运行
--basedir=是mysql程序的位置
--datadir=是打算让数据存放的位置

初始化完成以后,查看ls /data/mysql/能查看到初始化以后的数据库。

关于mariadb的配置文件

在官方配置好的mariadb读取配置文件有顺序之分。
读取顺序:/etc/my.cnf > /etc/mysql/my.cnf > /usr/etc/my.cnf > ~/.my.cnf
一般来讲,前2个/etc/my.cnf后者/etc/mysql/my.cnf 比较常用,如果配置文件位置多了,反而不好管理,另外一种常用的配置就是在启动mysql时,给它指定好配置文件。

另外需要注意的是,有可能你的旧配置文件/etc/my.cnf存在,这样可能会影响到数据库的配置,此时需要保证该配置文件的配置有效性。

vim /etc/my.cnf

[mysqld]
datadir=/data/mariadb_data
socket=/data/mariadb_data/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
log-error=/data/mariadb_data/log/mariadb.log
pid-file=/data/mariadb_data/pid/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

根据此配置文件,我还需要在/data/mysql下创建2个目录,用于保存日志和pid文件

mkdir /data/mariadb_data/{log,pid}
chown mysql.mysql -R /data/mariadb_data
安全初始化
 /usr/local/mysql/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
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):               #第一次运行直接回车
OK, successfully used password, moving on...

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

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n          #您的根帐户已受保护,因此可以安全地回答“n”。
 ... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] 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] y         #是否禁止root远程登录
 ... 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!

 
注意:运行mysql_secure_installation有个小坑,官方在编译的时候,指定了它连接的socket文件是在/tmp目录下的,所以可以对socket文件做个软连接到/tmp目录下,这样可以解决找不到socket文件的问题。
启动mariadb
/usr/local/mysql/bin/mysqld_safe &                               #后台启动mariadb服务
/usr/local/mysql/bin/mysqladmin -uroot -p shutdown              #停止mariadb服务

写在后面

上述的安装过程在本地启动mysql时,因为使用的是sock通讯方式,所以可以直接使用/usr/local/mysql/bin/mysql 进入数据库.但是用navicat等工具的ssh链接时,还是要输入密码的.

或者本地链接时,带上 -h 127.0.0.1,如/usr/local/mysql/bin/mysql -uroot -p -h 127.0.0.1,这样就需要输入正确的账号密码才能登录mariadb.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章