=================================
/*
* @系统:CentOS7.9
* @描述:数据库安装的几种方法二
*/
=================================
一、获取安装包
二进制包(https://downloads.mariadb.org/interstitial/mariadb-10.4.13/bintar-linux-x86_64/mariadb-10.4.13-linux-x86_64.tar.gz/from/http%3A//mirrors.tuna.tsinghua.edu.cn/mariadb/)
#进入到安装包存放位置
[root@51try ~]# cd /usr/local/src/
#上传文件
[root@51try src]# rz #选择上传文件
[root@51try src]# ls
mariadb-10.5.4-linux-x86_64.tar.gz
二、安装
1. 检查环境
[root@51try src]# ss -ntl #检查端口
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 [::]:22 [::]:*
LISTEN 0 100 [::1]:25 [::]:*
[root@51try src]# rpm -q mysql
package mysql is not installed
[root@51try src]# rpm -q mariadb
package mariadb is not installed
[root@51try src]# getent passwd mysql #检查mysql用户是否创建
2. 创建用户和组
[root@51try src]# useradd -r -d /data/mariadb -s /sbin/nologin mysql
[root@51try src]# getent passwd mysql
mysql:x:998:996::/data/mariadb:/sbin/nologin
[root@51try src]# id mysql
uid=998(mysql) gid=996(mysql) groups=996(mysql)
3. 解压到指定路径并创建软连接
[root@51try src]# tar xvf mariadb-10.5.4-linux-x86_64.tar.gz -C /usr/local/
[root@51try src]# cd /usr/local/
[root@51try local]# ls
bin etc games include lib lib64 libexec mariadb-10.5.4-linux-x86_64 sbin share src
[root@51try local]# ln -s mariadb-10.5.4-linux-x86_64/ mysql
4. 修改所所有者
[root@51try local]# ls -l mysql/
total 176
drwxrwxr-x 2 1000 1000 4096 Jun 22 11:50 bin
-rw-r--r-- 1 1000 1000 17987 Jun 23 23:10 COPYING
-rw-r--r-- 1 1000 1000 2354 Jun 23 23:10 CREDITS
-rw-r--r-- 1 1000 1000 8245 Jun 23 23:10 EXCEPTIONS-CLIENT
drwxrwxr-x 3 1000 1000 19 Jun 23 23:39 include
-rw-r--r-- 1 1000 1000 8782 Jun 23 23:10 INSTALL-BINARY
drwxrwxr-x 5 1000 1000 275 Jun 22 11:50 lib
drwxrwxr-x 4 1000 1000 30 Jun 23 23:39 man
drwxrwxr-x 9 1000 1000 4096 Jun 23 23:39 mysql-test
-rw-r--r-- 1 1000 1000 2973 Jun 23 23:10 README.md
-rw-r--r-- 1 1000 1000 19520 Jun 23 23:10 README-wsrep
drwxrwxr-x 2 1000 1000 56 Jun 23 23:39 scripts
drwxrwxr-x 31 1000 1000 4096 Jun 23 23:39 share
drwxrwxr-x 4 1000 1000 4096 Jun 23 23:39 sql-bench
drwxrwxr-x 3 1000 1000 165 Jun 23 23:39 support-files
-rw-r--r-- 1 1000 1000 86263 Jun 23 23:10 THIRDPARTY
[root@51try local]# chown -R root:root mysql/
[root@51try local]# ls -l mysql/
total 176
drwxrwxr-x 2 root root 4096 Jun 22 11:50 bin
-rw-r--r-- 1 root root 17987 Jun 23 23:10 COPYING
-rw-r--r-- 1 root root 2354 Jun 23 23:10 CREDITS
-rw-r--r-- 1 root root 8245 Jun 23 23:10 EXCEPTIONS-CLIENT
drwxrwxr-x 3 root root 19 Jun 23 23:39 include
-rw-r--r-- 1 root root 8782 Jun 23 23:10 INSTALL-BINARY
drwxrwxr-x 5 root root 275 Jun 22 11:50 lib
drwxrwxr-x 4 root root 30 Jun 23 23:39 man
drwxrwxr-x 9 root root 4096 Jun 23 23:39 mysql-test
-rw-r--r-- 1 root root 2973 Jun 23 23:10 README.md
-rw-r--r-- 1 root root 19520 Jun 23 23:10 README-wsrep
drwxrwxr-x 2 root root 56 Jun 23 23:39 scripts
drwxrwxr-x 31 root root 4096 Jun 23 23:39 share
drwxrwxr-x 4 root root 4096 Jun 23 23:39 sql-bench
drwxrwxr-x 3 root root 165 Jun 23 23:39 support-files
-rw-r--r-- 1 root root 86263 Jun 23 23:10 THIRDPARTY
5. 添加环境变量
[root@51try ~]# echo PATH=/usr/local/mysql/bin:$PATH > /etc/profile.d/mysql.sh
[root@51try ~]# . /etc/profile.d/mysql.sh
[root@51try ~]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
三、配置
1. 修改数据库存放位置(可以新增一个磁盘存放)
[root@51try ~]# mkdir /data/mariadb
2. 修改属组
[root@51try ~]# chown -R mysql.mysql /data/mariadb
3. 修改权限(为了安全需要修改权限为770)
[root@51try ~]# chmod 770 /data/mariadb
四、初始化
1. 进入到二进制程序目录
[root@51try ~]# cd /usr/local/mysql/
2. 执行初始化脚本文件(切记不要进入到进脚本存放位置的文件夹)
[root@51try mysql]# scripts/mariadb-install-db --datadir=/data/mariadb --user=mysql
Installing MariaDB/MySQL system tables in '/data/mariadb' ...
OK #出现这个就证明没问题了(有些可能只有mysql_install_db,一样一样的)
3. 修改配置文件(因为没有找到提供的配置文件,所以直接使用原有的)
[root@51try mysql]# cd
[root@51try ~]# vim /etc/my.cnf
================================
[mysqld]
datadir=/data/mariadb
socket=/usr/local/mysql/mysql.sock
[client]
port=3306
socket=/usr/local/mysql/mysql.sock
4. 设置开机自启动
[root@51try ~]# cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mariadb #拷贝启动脚本
[root@51try ~]# 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 :off 1:off 2:off 3:off 4:off 5:off 6:off
network :off 1:off 2:on 3:on 4:on 5:on 6:off
[root@51try ~]# chkconfig --add mariadb #添加启动服务
[root@51try ~]# 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]'.
mariadb :off 1:off 2:on 3:on 4:on 5:on 6:off
netconsole :off 1:off 2:off 3:off 4:off 5:off 6:off
network :off 1:off 2:on 3:on 4:on 5:on 6:off
5. 设置权限
[root@51try ~]# setfacl -R -m u:mysql:rwx /usr/local/mysql/ #授予mysql用户读写执行权限
#注:也可以将socket文件放到有执行权限的文件夹中
6. 添加服务
[root@51try ~]# systemctl start mariadb
[root@51try ~]# ss -nutl
Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
tcp LISTEN 0 128 *:22 *:*
tcp LISTEN 0 100 127.0.0.1:25 *:*
tcp LISTEN 0 128 [::]:22 [::]:*
tcp LISTEN 0 100 [::1]:25 [::]:*
tcp LISTEN 0 80 [::]:3306 [::]:*
7. 运行安全脚本
[root@51try ~]# mysql_secure_installation
#执行后出现报错,需要修改socket路径
[root@51try ~]# /usr/local/mysql/bin/mysqld --print-defaults #查看编译路径
/usr/local/mysql/bin/mysqld would have been started with the following arguments:
--datadir=/data/mariadb --socket=/tmp/mysql.sock --symbolic-links=0
#解决方案
[root@51try ~]# vim /etc/my.cnf
================
[mysqld]
datadir=/data/mariadb
socket=/tmp/mysql.sock
# socket=/usr/local/mysql/mysql.sock
[client]
port=3306
socket=/tmp/mysql.sock
# socket=/usr/local/mysql/mysql.sock
[root@51try ~]# systemctl restart mariadb #重启服务
[root@51try ~]# mysql_secure_installation
=======================================
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] y
Enabled successfully!
Reloading privilege tables..
... Success!
You already have your root account protected, so you can safely answer 'n'.
Change the root password? [Y/n] y #设置密码
New password: root1234
Re-enter new password: root1234
Password updated successfully!
Reloading privilege tables..
... Success!
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 #是否禁用root用户远程登录
... 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!
8. 设置开机启动
[root@51try ~]# /sbin/chkconfig mariadb on
五、连接数据库
[root@51try ~]# mysql -uroot -p #切记不要在-p后面加密码,这样等于明文
Enter password:
MariaDB [(none)]> status --查看一下信息
--------------
mysql Ver 15.1 Distrib 10.5.4-MariaDB, for Linux (x86_64) using readline 5.1
MariaDB [(none)]> \q --退出数据库
Bye
未完待续......
来源 https://mp.weixin.qq.com/s/vKHLvdZUMUhQ62G8-XayeQ