其他分享
首页 > 其他分享> > 第十周

第十周

作者:互联网

第十周作业

1、在阿里云服务器搭建openv-p-n(有条件的同学再做)

2、通过编译、二进制安装MySQL5.7

[root@centos7 ~]#cat install_online_mysql5.7or8.0_for_centos.sh

#!/bin/bash

. /etc/init.d/functions

SRC_DIR=`pwd`
MYSQL='mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz'
COLOR='echo -e \E[01;31m'
END='\E[0m'
MYSQL_ROOT_PASSWORD=magedu


check (){

if [ $UID -ne 0 ]; then
action "当前用户不是root,安装失败" false
exit 1
fi

cd $SRC_DIR
if [ ! -e $MYSQL ];then
$COLOR"缺少${MYSQL}文件"$END
$COLOR"请将相关软件放在${SRC_DIR}目录下"$END
exit
elif [ -e /usr/local/mysql ];then
action "数据库已存在,安装失败" false
exit
else
return
fi
}

install_mysql(){
$COLOR"开始安装MySQL数据库..."$END
yum -y -q install libaio numactl-libs libaio &> /dev/null
cd $SRC_DIR
tar xf $MYSQL -C /usr/local/
MYSQL_DIR=`echo $MYSQL| sed -nr 's/^(.*[0-9]).*/\1/p'`
ln -s /usr/local/$MYSQL_DIR /usr/local/mysql
chown -R root.root /usr/local/mysql/
id mysql &> /dev/null || { useradd -s /sbin/nologin -r mysql ; action "创建mysql用户"; }

echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh
. /etc/profile.d/mysql.sh
ln -s /usr/local/mysql/bin/* /usr/bin/
cat > /etc/my.cnf <<-EOF
[mysqld]
server-id=1
log-bin
datadir=/data/mysql
socket=/data/mysql/mysql.sock
log-error=/data/mysql/mysql.log
pid-file=/data/mysql/mysql.pid
[client]
socket=/data/mysql/mysql.sock
EOF
mysqld --initialize --user=mysql --datadir=/data/mysql
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
chkconfig --add mysqld
chkconfig mysqld on
service mysqld start
sleep 3
[ $? -ne 0 ] && { $COLOR"数据库启动失败,退出!"$END;exit; }
MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log`
mysqladmin -uroot -p$MYSQL_OLDPASSWORD password $MYSQL_ROOT_PASSWORD &>/dev/null
action "数据库安装完成"
}


check

install_mysql


3、二进制安装mariadb10.4

安装前的准备:

1、yum info mariadb,找到官网,http://mariadb.org,找到mariadb-10.4.22-linux-glibc_214-x86_64.tar.gz版本,镜像站点可以选择国内的阿里云,点击下载即可

 

安装步骤:

1、安装相关包

[root@centos7 ~]#yum -y install libaio numactl-libs
2、创建用户和组

[root@centos7 ~]#groupadd mysql
[root@centos7 ~]#useradd -r -g mysql -s /bin/false mysql
3、准备程序文件

[root@centos7 ~]#tar xfv mariadb-10.4.22-linux-glibc_214-x86_64.tar.gz -C /usr/local
[root@centos7 ~]#cd /usr/local/
[root@centos7 local]#ln -s mariadb-10.4.22-linux-glibc_214-x86_64 mysql
[root@centos7 local]#chown -R root.root /usr/local/mysql/
4、准备环境变量

[root@centos7 local]#echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@centos7 local]#. /etc/profile.d/mysql.sh
5、创建数据库目录,修改权限

[root@centos7 local]#mkdir /data/mysql -pv
mkdir: created directory ‘/data/mysql’
[root@centos7 local]#chown -R mysql:mysql /data/mysql/
6、准备配置文件

[root@centos7 local]#cd /usr/local/mysql/
[root@centos7 mysql]#cp /etc/my.cnf{,.bak}
[root@centos7 mysql]#vim /etc/my.cnf
[root@centos7 mysql]#cat /etc/my.cnf
[mysqld]
#datadir=/var/lib/mysql
datadir=/data/mysql
socket=/var/lib/mysql/mysql.sock
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd

[mysqld_safe]
#log-error=/var/log/mariadb/mariadb.log
log-error=/data/mysql/mysql.log
pid-file=/var/run/mariadb/mariadb.pid

#
# include all files from the config directory
#
!includedir /etc/my.cnf.d

7、创建配置文件中指定的路径并修改权限

[root@centos7 mysql]#mkdir /var/lib/mysql
[root@centos7 mysql]#chown -R mysql:mysql /var/lib/mysql/
[root@centos7 mysql]#touch /var/lib/mysql/mysql.sock
[root@centos7 mysql]#ll /var/lib/mysql
total 0
-rw-r--r-- 1 root root 0 Feb 1 13:00 mysql.sock
[root@centos7 mysql]#chmod guo+wr /var/lib/mysql/mysql.sock
[root@centos7 mysql]#ll /var/lib/mysql
total 0
-rw-rw-rw- 1 root root 0 Feb 1 13:00 mysql.sock
[root@centos7 mysql]#touch /data/mysql/mysql.log
[root@centos7 mysql]#ll /data/mysql/
total 0
-rw-r--r-- 1 root root 0 Feb 1 13:18 mysql.log
[root@centos7 mysql]#chmod guo+rw /data/mysql/mysql.log
[root@centos7 mysql]#ll /data/mysql/mysql.log
-rw-rw-rw- 1 root root 0 Feb 1 13:18 /data/mysql/mysql.log
[root@centos7 mysql]#mkdir /var/run/mariadb
[root@centos7 mysql]#chown -R mysql:mysql /var/run/mariadb/
[root@centos7 mysql]#touch /var/run/mariadb/mariadb.pid
[root@centos7 mysql]#ll /var/run/mariadb/
total 0
-rw-r--r-- 1 root root 0 Feb 1 13:32 mariadb.pid
[root@centos7 mysql]#chmod guo+rw /var/run/mariadb/mariadb.pid
[root@centos7 mysql]#ll /var/run/mariadb/
total 0
-rw-rw-rw- 1 root root 0 Feb 1 13:32 mariadb.pid
8、初始化数据库文件并生成 root 空密码

[root@centos7 mysql]#./scripts/mysql_install_db --user=mysql --datadir=/data/mysql
9、启动MariaDB守护程序

[root@centos7 mysql]#./bin/mysqld_safe --user=mysql --datadir=/data/mysql &
10、测试MariaDB守护程序

[root@centos7 mysql]#cd ./mysql-test ; perl mysql-test-run.pl
11、准备服务脚本和启动

[root@centos7 mysql]#cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@centos7 mysql]#chkconfig --add mysqld
[root@centos7 mysql]#chkconfig --list mysqld

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

[root@centos7 mysql]#systemctl start mysqld.service

12、数据库的登录、查询,修改密码,Ctrl+D,退出数据库

[root@centos7 mysql]#ln -s /var/lib/mysql/mysql.sock /tmp/mysql.sock
[root@centos7 mysql]#./bin/mysql -uroot -p
MariaDB [(none)]> show databases;
MariaDB [(none)]> use mysql;
MariaDB [mysql]> select user,host,password from user;
MariaDB [mysql]> SELECT VERSION();
+-----------------+
| VERSION() |
+-----------------+
| 10.4.22-MariaDB |
+-----------------+
MariaDB [mysql]> GRANT ALL PRIVILEGES ON *.* TO root@'localhost' IDENTIFIED BY "MySQL@2022.";
MariaDB [mysql]> FLUSH PRIVILEGES;
MariaDB [mysql]> Bye
[root@centos7 mysql]#
13、登录测试

[root@centos7 mysql]#mysql -uroot -pMySQL@2022.
14、加固MySQL服务器,在安装完成后,运行mysql_secure_installation命令,提高安全性

运行脚本:mysql_secure_installation

设置数据库管理员root口令
禁止root远程登录
删除anonymous用户帐号
删除test数据库

[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
haven't set the root password yet, 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 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] n #root帐户已受保护,回答“n”
... skipping.
You already have your root account protected, so you can safely answer 'n'.
Change the root password? [Y/n] n #已经设置好密码了,不用改,回答“n”
... skipping.
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 #删除匿名用户,回答“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 #不允许root用户远程登录,回答“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 database and access to it? [Y/n] y #删除test数据库,回答“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 #重新加载特权表,回答“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,data,centos7,mysql,mariadb,local
来源: https://www.cnblogs.com/glc19980204/p/16395256.html