您的位置:首页 > 数据库 > MariaDB

通用二进制方式安装mariadb

2017-09-27 08:43 260 查看

目录

目录
mariadb下载

环境准备

准备用户

准备二进制程序

准备配置文件

创建数据库文件

准备服务脚本

准备日志文件

设置环境变量

连接数据库测试

安全初始化

优化后连接

mariadb下载

官网:http://mariadb.org/

目前最新版本为:10.3.1,稳定版本为:10.2.8

本次实验使用的版本为:稳定版10.2.8

0. 环境准备

[root@centos7 mysql]# rpm -qa libaio        <==判断系统安装这两个lib包,如果不按爪给你这两个lib包在初始化是会报错的
libaio-0.3.109-13.el7.x86_64
libaio-0.3.109-13.el7.i686
[root@centos7 dbdata]# yum install -y libaio.so.1
[root@centos7 mysql]# yum install -y libaio.x86_64 libaio-devel.x86_64


1. 准备用户

mariadb和mysql运行使用的用户都是mysql,所以需要创建mysql用户

[root@centos7 ~]# getent passwd mysql       <==mysql用户不存在
[root@centos7 ~]# useradd -r -m -d /app/dbdata -s /sbin/nologin mysql       <==创建mysql用户,指定家目录为/app/dbdata,且为系统用户,默认shell为/sbin/nologin无法登陆
[root@centos7 ~]# getent passwd mysql
4000
<==确认用户mysql信息
mysql:x:997:995::/app/dbdata:/sbin/nologin
[root@centos7 ~]# ll /app/dbdata/ -d            <==查看mysql家目录
drwx------. 2 mysql mysql 62 Sep 25 09:43 /app/dbdata/


2. 准备二进制程序

[root@centos7 app]# tar xf mariadb-10.2.8-linux-x86_64.tar.gz -C /usr/local/         <==解压mariadb到/usr/local目录下
[root@centos7 app]# cd /usr/local/      <==切换到/usr/local目录
[root@centos7 local]# ls
bin  games    lib    libexec                      sbin   src
etc  include  lib64  mariadb-10.2.8-linux-x86_64  share
[root@centos7 local]# ln -sv mariadb-10.2.8-linux-x86_64/ mysql     <==创建软链接,名字必须交mysql
‘mysql’ -> ‘mariadb-10.2.8-linux-x86_64/’
[root@centos7 local]# ls        <==确认软链接已经别创建
bin  games    lib    libexec                      mysql  share
etc  include  lib64  mariadb-10.2.8-linux-x86_64  sbin   src


3. 准备配置文件

配置格式:类ini格式,各程序由单个配置文件提供配[prog_name]

配置文件查找次序:后面覆盖前面的配置文件

/etc/my.cnf –> /etc/mysql/my.cnf –> –default-extrafile=/PATH/TO/CONF_FILE –> ~/.my.cnf

[root@centos7 local]# mkdir /etc/mysql      <==创建mysql目录
[root@centos7 mysql]# cp /usr/local/mariadb-10.2.8-linux-x86_64/support-files/my-huge.cnf /etc/mysql/my.cnf           <==cpmysql配置文件
[root@centos7 mysql]# vim /etc/mysql/my.cnf     <==编辑配置文件,添加一下三行,在[mysqld]后面
[mysqld]
datadir = /app/dbdata       <==指定数据库文件路径,必须指定为/app/dbdata
innodb_file_per_table = on  <==使用innodb引擎,每个表都是一个独立的文件
skip_name_resolve = on      <==禁止主机名解析


4. 创建数据库文件

[root@centos7 ~]# ls /app/dbdata/           <==没有文件
[root@centos7 mysql]# ./scripts/mysql_install_db --help     <==查看此脚本的帮助信息,此脚本是用于创建数据库文件的脚本,创建数据库文件相当于初始化
Usage: ./scripts/mysql_install_db [OPTIONS]
--auth-root-authentication-method=normal|socket
Chooses the authentication method for the created initial
root user. The default is 'normal' to creates a root user
that can login without password, which can be insecure.
The alternative 'socket' allows only the system root user
to login as MariaDB root; this requires the unix socket
authentication plugin.
--auth-root-socket-user=user
Used with --auth-root-authentication-method=socket. It
specifies the name of the MariaDB root account, as well
as of the system account allowed to access it. Defaults
to 'root'.
--basedir=path       The path to the MariaDB installation directory.
--builddir=path      If using --srcdir with out-of-directory builds, you
will need to set this to the location of the build
directory where built files reside.
--cross-bootstrap    For internal use.  Used when building the MariaDB system
tables on a different host than the target.
--datadir=path       The path to the MariaDB data directory.
--defaults-extra-file=name
Read this file after the global files are read.
--defaults-file=name Only read default options from the given file name.
--force              Causes mysql_install_db to run even if DNS does not
work.  In that case, grant table entries that
normally use hostnames will use IP addresses.
--help               Display this help and exit.
--ldata=path         The path to the MariaDB data directory. Same as
--datadir.
--no-defaults        Don't read default options from any option file.
--defaults-file=path Read only this configuration file.
--rpm                For internal use.  This option is used by RPM files
during the MariaDB installation process.
--skip-auth-anonymous-user
Do not install an unprivileged anonymous user.
--skip-name-resolve  Use IP addresses rather than hostnames when creating
grant table entries.  This option can be useful if
your DNS does not work.
--srcdir=path        The path to the MariaDB source directory.  This option
uses the compiled binaries and support files within the
source tree, useful for if you don't want to install
MariaDB yet and just want to create the system tables.
--user=user_name     The login username to use for running mysqld.  Files
and directories created by mysqld will be owned by this
user.  You must be root to use this option.  By default
mysqld runs using your current login name and files and
directories that it creates will be owned by you.

All other options are passed to the mysqld program

[root@centos7 mysql]# ./scripts/mysql_install_db --datadir=/app/dbdata --user=mysql        <==指定mysql数据存储目录为/app/dbdata,用户为mysql
Installing MariaDB/MySQL system tables in '/app/dbdata' ...
2017-09-25 11:02:27 140386981918528 [Warning] 'THREAD_CONCURRENCY' is deprecated and will be removed in a future release.
2017-09-25 11:02:30 140386857879296 [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.haiyun.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='/app/dbdata'

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/ 
at http://mariadb.org/jira [root@centos7 mysql]# ls /app/dbdata/       <==可以看到文件已经生成
aria_log.00000001  ibdata1      mysql             mysql-bin.state
aria_log_control   ib_logfile0  mysql-bin.000001  performance_schema
ib_buffer_pool     ib_logfile1  mysql-bin.index   test


5. 准备服务脚本

[root@centos7 mysql]# pwd
/usr/local/mysql
[root@centos7 mysql]# cp support-files/mysql.server /etc/init.d/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]'.

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]# 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]# chkconfig mysqld on


6. 准备日志文件

[root@centos7 mysql]# service mysqld start
Starting mysqld (via systemctl):                           [  OK  ]
[root@centos7 mysql]# setfacl -m u:mysql:rwx /var/log/
[root@centos7 mysql]# service mysqld start
Starting mysqld (via systemctl):  Job for mysqld.service failed because the control process exited with error code. See "systemctl status mysqld.service" and "journalctl -xe" for details.
[FAILED]
[root@centos7 mysql]# mkdir /var/log/mariadb        <==创建日志目录
[root@centos7 mysql]# touch /var/log/mariadb/mariadb.log    <==穿件日志文件
[root@centos7 mysql]# setfacl -R -m u:mysql:rwx /var/log/mariadb        <==设置acl使mysql用户对/var/log/mariadb目录有权限


7. 设置环境变量

[root@centos7 mysql]# vim /etc/profile.d/mysql.sh
export PATH=/usr/local/mysql/bin/:$PATH
[root@centos7 mysql]# source /etc/profile.d/mysql.sh    <==同步环境变量到本地
[root@centos7 mysql]# echo $PATH
/usr/local/mysql/bin/:/usr/local/mysql/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin


8. 连接数据库测试

[root@centos7 mysql]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.2.8-MariaDB-log MariaDB Server

Copyright (c) 2000, 2017, 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.00 sec)

MariaDB [(none)]> SELECT USER();
+----------------+
| USER()         |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.00 sec)


9. 安全初始化

[root@centos7 mysql]# 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] n
... skipping.

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!


10. 优化后连接

[root@centos7 mysql]# mysql         <==优化后就无法连接了
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@centos7 mysql]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 19
Server version: 10.2.8-MariaDB-log MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: