在centos6上安装二进制的mariadb

操作前的声明:

(1)、机器 centos6

(2)、在centos6里面,你进入数据库之后会发现,他的提示符一直长这样:

mysql> use test;
Database changed
mysql> 

不管我进入哪一个数据库,他的提示符一直都是长这样,而我有时候编写一个shell脚本,时间短些还好,时间长些我就记不得自己到底是在哪一个数据库里面了!岂不是很烦!今天就是来给大家解决这个问题的。

1.下载

从downloads.mariadb.org  下载所需版本的mariadb的二进制包 mariadb-10.3.9-linux-x86_64.tar.gz。在这里我给大家分享一个自己存在网盘里的资源。

百度网盘链接https://pan.baidu.com/s/12KqOzyFQTNOSgRT3fOcopg

密码:qmuz

2、创建用户及其组

[root@centos6(nanyibo) ~]# mkdir -p /app/dbdata
[root@centos6(nanyibo) ~]# groupadd -g 36 -r mysql
[root@centos6(nanyibo) ~]# useradd -u 36 -r -g mysql -m -d /app/dbdata -s /sbin/nologin mysql

先查询一下有没有名字为mysql的用户和组,如果有,则需要先删除。

解释一下上面的两条语句

(1)groupadd -g 36 -r mysql

创建一个gid为36的系统组

(2)useradd -u 36 -r -g mysql -m -d /app/dbdata -s /sbin/nologin mysql

创建系统用户(-r与-m配合)mysql,id为36,主组为mysql,家目录为/app/dbdata,不可交互式登录。

3、创建逻辑卷并挂载到mysql的家目录/app/dbdata下。

[root@centos6(nanyibo) ~]# fdisk /dev/sda
#创建一个LVM分区,这里我就不写了

#创建逻辑卷
[root@centos6(nanyibo) ~]# partx -a /dev/sda
[root@centos6(nanyibo) ~]# pvcreate /dev/sda6
[root@centos6(nanyibo) ~]# vgcreate vgmysql /dev/sda6
[root@centos6(nanyibo) ~]# lvcreate -l +100%FREE -n lvmysql vgmysql

#格式化逻辑卷
[root@centos6(nanyibo) ~]# mkfs.ext4 /dev/vgmysql/lvmysql

#将挂载信息写入配置文件,保证重启后仍然有效
[root@centos6(nanyibo) ~]# vim /etc/fstab 
/dev/vgmysql/lvmysql    /app/dbdata             ext4    defaults        0 0

#重新挂载,让写入的配置信息生效
[root@centos6(nanyibo) ~]# mount -a

#我们在第二步指定了用户mysql的家目录,/app/dbdata所属人所属组都变成了mysql。神奇之处来了,我们完成挂载之后,/app/dbdata所属人所属组变了!赶快修改一下
[root@centos6(nanyibo) ~]# chown mysql.mysql /app/dbdata
[root@centos6(nanyibo) ~]# chmod 700 /app/dbdata

4、解压

这里就用我给大家提供的包来给大家做详细的讲解吧。

[root@centos6(nanyibo) ~]# tar -xvf mariadb-10.2.14-linux-x86_64.tar.gz -C /usr/local/
[root@centos6(nanyibo) ~]# cd /usr/local/
[root@centos6(nanyibo) local]# ln -sv mariadb-10.2.14-linux-x86_64 mysql
`mysql' -> `mariadb-10.2.14-linux-x86_64'

-C 将mariadb-10.2.14-linux-x86_64.tar.gz压缩到指定的/usr/local/路径下。

再将我们压缩出来的目录mariadb-10.2.14-linux-x86_64做一个软连接至mysql,这样你就可以直接以mysql运行数据库了。

5、创建配置文件

[root@centos6(nanyibo) local]# cd /usr/local/mysql/
[root@centos6(nanyibo) mysql]# mkdir /etc/mysql
[root@centos6(nanyibo) mysql]# cp support-files/my-huge.cnf /etc/mysql/my.cnf
[root@centos6(nanyibo) mysql]# vim /etc/mysql/my.cnf
[mysqld]
……
datadir         = /app/dbdata
innodb_file_per_table   = on
skip_name_resolve = on
……

etc是我们的配置文件目录,如果我们想永久性的使用,就必须在etc下创建自己的配置文件,前三行无需解释,在etc下创建mysql目录,再将人家提供给我们的配置文件拷贝进去就可以了。

在配置文件里面添加三行

(1)datadir         = /app/dbdata   表示我们以后数据库内数据存放的路径

innodb_file_per_table   = on


skip_name_resolve = on

这两行表示以后的在数据库存储的库啊表啊,都生成一个单独的文件。

6.创建数据库

[root@centos6(nanyibo) mysql]# scripts/mysql_install_db --datadir=/app/dbdata --user=mysql

7、配置启动脚本

[root@centos6(nanyibo) mysql]# cp support-files/mysql.server /etc/init.d/mysqld
[root@centos6(nanyibo) mysql]# chkconfig --add mysqld
[root@centos6(nanyibo) mysql]# chkconfig mysqld on
[root@centos6(nanyibo) mysql]# service mysqld restart
MariaDB server PID file could not be found!                [FAILED]
Starting MariaDB.180710 18:00:14 mysqld_safe Logging to '/var/log/mysqld.log'.
180710 18:00:14 mysqld_safe Starting mysqld daemon with databases from /app/dbdata
                                                           [  OK  ]

不要被那个  [FAILED]  吓到了,因为我们还没有mysqld服务,何来停止服务?

8.配置环境变量PATH

[root@centos6(nanyibo) ~]# vim /etc/profile.d/*.sh
export PATH=/usr/local/mysql/bin:$PATH
[root@centos6(nanyibo) ~]# source /etc/profile.d/*.sh

我们想要在命令行直接使用mysql命令,还需要在/etc/profile.d/下找任意一个以.sh结尾的文件,添加一行export PATH=/usr/local/mysql/bin:$PATH,source重读一下该配置文件,让变量PATH生效。

9、初始化mysql

[root@centos6(nanyibo) ~]# mysql_secure_installation 
Enter current password for root (enter for none):   初始密码是回车键 
Change the root password? [Y/n]                     这里我们最好重置一下root密码
Remove anonymous users?[Y/n]                        删除匿名用户?看心情
Disallow root login remotely?[Y/n]                  远程禁止跟用户登录,最好选n
Remove test database and access to it?              删除测试数据库并访问它?随意,影响不大
Reload privilege tables now?                        现在重新加载权限表? 选y,要不然我们白忙活了

10、打完收工

初始化完成了,让我们来看一看效果吧

[root@localhost /]# mysql -uroot -pcentos

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 102
Server version: 10.2.14-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            |
+---------------------+
| #mysql50#lost+found |
| information_schema  |
| magedu              |
| mysql               |
| performance_schema  |
+---------------------+
5 rows in set (0.00 sec)

MariaDB [(none)]> use mysql;
Database changed
MariaDB [mysql]> 

完美!我们进入mysql库之后,下面的提示符从none变成了mysql!

然而,这里面有一个问题,而且还很严重,你发现了吗?在回顾一下我们登录数据库时敲的命令:

[root@localhost /]# mysql -uroot -pcentos

我们的数据库密码居然赤裸裸的放在那里!你可能会说:我打完[root@localhost /]# mysql -uroot -p后直接按一下回车,输入密码别人不就看不到了吗?然而请思考,这样做写在脚本里面合适吗?下一篇博客将解决这个问题(还没来得及请教老师)。

猜你喜欢

转载自blog.csdn.net/qq_34208467/article/details/82757136