二进制安装MySQL\MariaDB

二进制安装MySQL\MariaDB

操作系统:Centos7.6

1.准备用户和组

创建组mysql并指定为系统组和组ID
groupadd -r -g 336 mysql
创建用户mysql并指定为系统用户,主组为mysql,shell类型为nologin,指定家目录(家目录一会再手动创建)
useradd -r -g mysql -u 336 -s /sbin/nologin -d /data/mysql mysql

2.准备二进制程序文件和相关文件属性

准备安装包,要是觉得官方下载速度慢的可以自行寻找国内的安装包。
mariadb-10.2.23-linux-x86_64.tar.gz
下载地址:https://downloads.mariadb.org/interstitial/mariadb-10.2.23/bintar-linux-x86_64/mariadb-10.2.23-linux-x86_64.tar.gz/from/http%3A//mirrors.neusoft.edu.cn/mariadb/
解包到/usr/local/,编译时已经指定了该路径,所以文件路径放错会导致MySQL启动失败,然后指定该目录所有者和所属组为root。
tar xvf mariadb-10.2.23-linux-x86_64.tar.gz -C /usr/local/  #解包到/usr/local/
chown -R root.root /usr/local/  #指定所有者
进入/usr/local/把mariadb-10.2.23-linux-x86_64目录更名为mysql,或者创建软链接,推荐创建软链接。
cd /usr/local/
ln -s mariadb-10.2.23-linux-x86_64/ mysql  #创建软连接

示例

    [15:00:42root@node2-centos7 local]#ll
    total 4
    drwxr-xr-x.  2 root root    6 Apr 11  2018 bin
    drwxr-xr-x.  2 root root    6 Apr 11  2018 etc
    drwxr-xr-x.  2 root root    6 Apr 11  2018 games
    drwxr-xr-x.  2 root root    6 Apr 11  2018 include
    drwxr-xr-x.  2 root root    6 Apr 11  2018 lib
    drwxr-xr-x.  2 root root    6 Apr 11  2018 lib64
    drwxr-xr-x.  2 root root    6 Apr 11  2018 libexec
    drwxrwxr-x. 12 root root 4096 Mar 25 09:24 mariadb-10.2.23-linux-x86_64
    lrwxrwxrwx.  1 root root   29 Apr 27 15:00 mysql -> mariadb-10.2.23-linux-x86_64/
    drwxr-xr-x.  2 root root    6 Apr 11  2018 sbin
    drwxr-xr-x.  5 root root   49 Apr 10 00:28 share
    drwxr-xr-x.  2 root root    6 Apr 11  2018 src

3.准备数据库数据目录和数据
创建mysql用户家目录,然后所有者属组指定为mysql(后面会有MariaDB/MySQL系统表等文件生成在这)
mkdir /data/mysql -pv
chown mysql.mysql /data/mysql/
生成MySQL系统表文件,进入/usr/local/mysql目录然后执行scripts目录里的可执行程序/mysql_install_db,注意不要进入scripts目录里面执行,一定要确保当前工作目录是/usr/local/mysql(pwd命令查看)
./scripts/mysql_install_db --datadir=/data/mysql --user=mysql

示例

[15:02:39root@node2-centos7 mysql]#./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
Installing MariaDB/MySQL system tables in '/data/mysql' ...
OK

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

.........
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/

[15:04:08root@node2-centos7 mysql]#ll /data/mysql/  #确认下mysql的家目录是不是有文件生成了
total 110620
-rw-rw----. 1 mysql mysql    16384 Apr 27 15:03 aria_log.00000001
-rw-rw----. 1 mysql mysql       52 Apr 27 15:03 aria_log_control
-rw-rw----. 1 mysql mysql      938 Apr 27 15:03 ib_buffer_pool
-rw-rw----. 1 mysql mysql 12582912 Apr 27 15:03 ibdata1
-rw-rw----. 1 mysql mysql 50331648 Apr 27 15:03 ib_logfile0
-rw-rw----. 1 mysql mysql 50331648 Apr 27 15:03 ib_logfile1
drwx------. 2 mysql root      4096 Apr 27 15:03 mysql
drwx------. 2 mysql mysql       20 Apr 27 15:03 performance_schema
drwx------. 2 mysql root         6 Apr 27 15:03 test

4.准备Mysql的服务器端的配置文件
在/etc下创建配置文件的目录
mkdir /etc/mysql
拷贝模版配置文件到创建的目录里面,并命名为my.cnf
cp /usr/local/mysql/support-files/my-huge.cnf /etc/mysql/my.cnf
只需要在[mysqld]下面加入一行datadir=/data/mysql就行了,用来指明规划好的路径,不然会找不到数据库默认的路径在哪

示例

[15:05:12root@node2-centos7 mysql]#vi /etc/my.cnf 
[mysqld]
datadir=/var/lib/mysql

5.准备服务启动脚本

启动脚本不会写?觉得写的麻烦?没关系,官方也早就准备了启动脚本模版给我们,同样也在support-files目录下,启动脚本放在/etc/init.d/目录,命名为mysqld
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
添加mysqld系统服务
chkconfig --add mysqld

示例

[15:21:57root@node2-centos7 mysql]#chkconfig --add mysqld
[15:22:11root@node2-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

6.PATH变量
定义PATH变量
echo PATH='/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
./etc/profile.d/mysql.sh

7.启动服务

service mysqld start

示例

[15:22:17root@node2-centos7 mysql]#service mysqld start
Starting mysqld (via systemctl):                           [  OK  ]
[15:22:33root@node2-centos7 mysql]#ss -ntl  #确认3306端口是否已经起来
State      Recv-Q Send-Q            Local Address:Port                           Peer Address:Port              
LISTEN     0      128                           *:80                                        *:*                  
LISTEN     0      128                           *:22                                        *:*                  
LISTEN     0      100                   127.0.0.1:25                                        *:*                  
LISTEN     0      80                           :::3306                                     :::*                  
LISTEN     0      128                          :::80                                       :::*                  
LISTEN     0      128                          :::22                                       :::*                  
LISTEN     0      100                         ::1:25                                       :::*                  

8.MySQL安全加固
设置完密码后全部全选y回车。注意,该脚本每一步都是立即生效的,也就是说就算中断该脚本
mysql_secure_installation

[21:26:28root@node2-centos7 ~]#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):   #输入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  #设置密码
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!

9.测试连接
mysql -u指定用户(root,注意mysql的root用户并非Centos上的root用户) -p(输入刚设置的密码)
mysql -uroot -ppassword

大功告成

[23:14:18root@node2-centos7 ~]#mysql -uroot -*******
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 26
Server version: 10.2.23-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)]>
posted @ 2019-04-27 23:35  绿鲤鱼与理理与驴  阅读(238)  评论(0编辑  收藏  举报