在安装之前我们先检查环境,确保iptables关闭,selinux关闭,确保主机上没有安装mariadb数据库
一、先准备Mariadb10.2.15二进制安装包,我们可以到https://downloads.mariadb.org/mariadb/10.2.15/下载,这里我们选择mariadb-10.2.15-linux-x86_64.tar.gz这个包
官方会提示,请不要在生产环境使用beta和alpha版本的数据库。
他们的主要功能一般是用来测试和排错,所以不稳定。
这里我们选择安装一个10.2系列的Stable的数据库。
二、准备用户
[root@centos7 ~]#groupadd -r -g 306 mysql
[root@centos7 ~]#useradd -r -g 306 -u 306 -m -d /data/mariadb mysql
[root@centos7 ~]# getent passwd mysql
mysql:x:306:306::/data/mariadb:/sbin/nologin
三、准备数据目录
以/data/mariadb为例,建议使用逻辑卷
[root@centos7 ~]# mkdir /data/mariadb
[root@centos7 ~]#chown mysql:mysql /data/mariadb //把文件属主和属组改为mysql
[root@centos7 local]# chmod 770 /data/mariadb/
四、准备二进制程序,由于二进制程序是别人编译好的,因此它的安装目录是指定好的。一般在/usr/local下。
[root@centos7 local]# tar xvf mariadb-10.2.15-linux-x86_64.tar.gz -C /usr/local
进入/usr/local/目录下ls可以看到mariadb已经解压成功,由于是带有版本号的,不便后期管理,所以这里我们创建一个软链接
[root@centos7 ~]# cd /usr/local
[root@centos7 local]# ls
bin etc games include lib lib64 libexec mariadb-10.2.15-linux-x86_64 sbin share src
[root@centos7 local]# ln -sv mariadb-10.2.15-linux-x86_64/ ./mysql
修改mysql这个目录的属性
[root@centos7 local]# chown -R root:mysql /usr/local/mysql
五、把二进制程序导入PATH变量中。
[root@centos7 mysql]# echo PATH=/usr/local/mysql/bin:$PATH > /etc/profile.d/mysql.sh
[root@centos7 mysql]# . /etc/profile.d/mysql.sh
六、 准备配置文件
[root@centos7 mysql]# mkdir /etc/mysql
在/usr/local/mysql/support-files目录下存放着服务脚本和一些配置文件的模板,可以根据自己的需求把配置文件模板复制到对应目录下并修改。
[root@centos7 mysql]# ls support-files/
binary-configure my-huge.cnf my-large.cnf my-small.cnf mysql-log-rotate policy wsrep_notify
magic my-innodb-heavy-4G.cnf my-medium.cnf mysqld_multi.server mysql.server wsrep.cnf
[root@centos7 mysql]# cp ./support-files/my-huge.cnf /etc/mysql/my.cnf //复制并改名
[root@centos7 mysql]# vim /etc/mysql/my.cnf
[mysqld]中添加三个选项:
datadir = /data/mariadb
innodb_file_per_table = on
skip_name_resolve = on //禁止主机名解析,建议使用
七、创建数据库文件
[root@centos7 mysql]# cd /usr/local/mysql //进入这个目录下,一定要在这个目录下运行mysql_install_db脚本程序,因为要调用该目录下的其他脚本文件
[root@centos7 mysql]# pwd
/usr/local/mysql
[root@centos7 mysql]# ./scripts/mysql_install_db --datadir=/data/mariadb --user=mysql //执行脚本程序
在执行脚本的时候报错误,提示我们缺少libaio.so库文件。
./bin/mysqld: error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
解决方法:
[root@centos7 mysql]# yum install -y libaio //安装后在初始化就OK了
[root@centos7 mysql]# ./scripts/mysql_install_db --datadir=/data/mariadb --user=mysql
Installing MariaDB/MySQL system tables in '/data/mariadb' ...
2019-07-04 8:37:53 140031918196544 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
2019-07-04 8:40:17 140031840286464 [Warning] Failed to load slave replication state from table mysql.gtid_slave_pos: 1146: Table 'mysql.gtid_slave_pos' doesn't exist
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:
'./bin/mysqladmin' -u root password 'new-password'
'./bin/mysqladmin' -u root -h centos7.cwj.com password 'new-password'
Alternatively you can run:
'./bin/mysql_secure_installation'
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.
You can start the MariaDB daemon with:
cd '.' ; ./bin/mysqld_safe --datadir='/data/mariadb'
You can test the MariaDB daemon with mysql-test-run.pl
cd './mysql-test' ; perl mysql-test-run.pl
Please report any problems at http://mariadb.org/jira
The latest information about MariaDB is available at http://mariadb.org/.
You can find additional information about the MySQL part at:
http://dev.mysql.com
Consider joining MariaDB's strong and vibrant community:
https://mariadb.org/get-involved/
其实都是一些输出信息。但是这里仔细看的话,他们有教我们下一步怎么做。
八、准备日志文件
[root@centos7 mariadb]# touch /var/log/mysqld.log
[root@centos7 mariadb]# chown mysql /var/log/mysqld.log
[root@centos7 mariadb]# ll /var/log/mysqld.log
-rw-r–r-- 1 mysql root 0 Jul 4 10:42 /var/log/mysqld.log
九、准备服务脚本,并启动服务
服务脚本放在/usr/local/mysql/support-files目录下
[root@centos7 mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld
[root@centos7 mysql]# chkconfig --add mysqld
[root@centos7 mysql]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@centos7 mysql]# service mysqld start
Starting mysqld (via systemctl): [ OK ]
[root@centos7 mysql]# ss -ntl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:111 *:*
LISTEN 0 10 10.10.20.45:53 *:*
LISTEN 0 10 192.168.147.131:53 *:*
LISTEN 0 10 127.0.0.1:53 *:*
LISTEN 0 128 *:22 *:*
LISTEN 0 128 127.0.0.1:953 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 127.0.0.1:6010 *:*
LISTEN 0 128 127.0.0.1:6011 *:*
LISTEN 0 128 127.0.0.1:6012 *:*
LISTEN 0 128 127.0.0.1:6013 *:*
LISTEN 0 128 127.0.0.1:6014 *:*
LISTEN 0 128 :::111 :::*
LISTEN 0 10 ::1:53 :::*
LISTEN 0 128 :::22 :::*
LISTEN 0 128 ::1:953 :::*
LISTEN 0 100 ::1:25 :::*
LISTEN 0 128 ::1:6010 :::*
LISTEN 0 128 ::1:6011 :::*
LISTEN 0 128 ::1:6012 :::*
LISTEN 0 128 ::1:6013 :::*
LISTEN 0 128 ::1:6014 :::*
LISTEN 0 80 :::3306 :::*
十、安全初始化
[root@centos7 mysql]# /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
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] n
... 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!
十一、使用数据库
root@centos7 mysql]# mysql
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 10.2.15-MariaDB-log 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)]> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.03 sec)
MariaDB [(none)]>