M-63.第十周作业

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

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

编译安装

1、源码包下载,官网下载地址:https://downloads.mysql.com/archives/community/,点击下载,还有boost_1_59_0.tar.gz这个源码包也要提前下载好,然后将源码包上传到Linux系统

[root@centos7 ~]#ll

total 131916

-rw-r--r-- 1 root root 51363998 Jan 27 21:58 mysql-boost-5.7.30.tar.gz

-rw-r--r-- 1 root root 83709983 Jan 27 21:58 boost_1_59_0.tar.gz

2、安装相关依赖包

[root@centos7 ~]#yum -y install gcc gcc-c++ cmake bison bison-devel zlib-devel libcurl-devel libarchive-devel boost-devel ncurses-devel gnutls-devel libxml2-devel openssl-devel libevent-devel libaio-devel perl-Data-Dumper

3、创建用户和数据目录

[root@centos7 ~]#useradd -r -s /sbin/nologin -d /data/mysql mysql

[root@centos7 ~]#id mysql

uid=995(mysql) gid=992(mysql) groups=992(mysql)

4、创建数据库目录,修改权限

[root@centos7 ~]#mkdir /data/mysql -pv

mkdir: created directory ‘/data/mysql’

[root@centos7 ~]#chown mysql.mysql /data/mysql/

[root@centos7 ~]#ll -d /data/mysql/

drwxr-xr-x 2 mysql mysql 6 Jan 27 20:42 /data/mysql/

5、解压缩源码包

[root@centos7 ~]#tar xvf mysql-boost-5.7.30.tar.gz -C /usr/local/src

[root@centos7 ~]#tar xvf boost_1_59_0.tar.gz -C /usr/local/src

6、源码编译安装 MySQL

[root@centos7 ~]#cd /usr/local/src/mysql-5.7.30/

[root@centos7 mysql-5.7.30]#pwd

/usr/local/src/mysql-5.7.30

[root@centos7 mysql-5.7.30]#cmake . \

> -DCMAKE_INSTALL_PREFIX=/apps/mysql \

> -DMYSQL_DATADIR=/data/mysql/ \

> -DSYSCONFDIR=/etc/ \

> -DMYSQL_USER=mysql \

> -DWITH_INNOBASE_STORAGE_ENGINE=1 \

> -DWITH_ARCHIVE_STORAGE_ENGINE=1 \

> -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \

> -DWITH_PARTITION_STORAGE_ENGINE=1 \

> -DWITHOUT_MROONGA_STORAGE_ENGINE=1 \

> -DWITH_DEBUG=0 \

> -DWITH_READLINE=1 \

> -DWITH_SSL=system \

> -DWITH_ZLIB=system \

> -DWITH_LIBWRAP=0 \

> -DENABLED_LOCAL_INFILE=1 \

> -DMYSQL_UNIX_ADDR=/data/mysql/mysql.sock \

> -DDEFAULT_CHARSET=utf8 \

> -DDEFAULT_COLLATION=utf8_general_ci \

> -DDOWNLOAD_BOOST=1 \

> -DWITH_BOOST=/usr/local/src/boost_1_59_0

[root@centos7 mysql-5.7.30]#make && make install

提示:如果出错,执行rm -f CMakeCache.txt

7、准备环境变量

[root@centos7 mysql-5.7.30]#echo 'PATH=/apps/mysql/bin:$PATH' > /etc/profile.d/mysql.sh

[root@centos7 mysql-5.7.30]#. /etc/profile.d/mysql.sh

8、修改配置文件

[root@centos7 mysql-5.7.30]#cd /apps/mysql/

[root@centos7 mysql]#pwd

/apps/mysql

[root@centos7 mysql]#vim /etc/my.cnf

[mysqld]

explicit_defaults_for_timestamp=true

datadir=/data/mysql/

basedir=/apps/mysql/bin/mysql/

port=3306

pid-file=/data/mysql/mysql.pid

socket=/data/mysql/mysql.socket

symbolic-links=0

character_set_server=utf8

user=mysql

[mysqld_safe]

log-error=/data/mysql/mysql.log

[client]

port=3306

socket=/data/mysql/mysql.socket

default-character-set=utf8

9、初始化数据库

[root@centos7 mysql]#bin/mysqld --initialize-insecure --datadir=/data/mysql/ --user=mysql

10、准备启动脚本,并启动服务

[root@centos7 mysql]#cp /apps/mysql/support-files/mysql.server /etc/init.d/mysqld

[root@centos7 mysql]#chkconfig --add mysqld

[root@centos7 mysql]#systemctl start mysqld

[root@centos7 mysql]#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                                      [::]:*

LISTEN    0      80                          [::]:3306                                    [::]:*

11、修改密码,设置的密码一定要记好(大写字母、小写字母、数字加标点符号,密码要定期更换)

[root@centos7 mysql]# mysql

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 2

Server version: 5.7.30 Source distribution

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its

affiliates. Other names may be trademarks of their respective

owners.

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

mysql> status

--------------

mysql  Ver 14.14 Distrib 5.7.30, for Linux (x86_64) using  EditLine wrapper

Connection id: 2

Current database:

Current user: root@localhost

SSL: Not in use

Current pager: stdout

Using outfile: ''

Using delimiter: ;

Server version: 5.7.30 Source distribution

Protocol version: 10

Connection: Localhost via UNIX socket

Server characterset: utf8

Db    characterset: utf8

Client characterset: utf8

Conn.  characterset: utf8

UNIX socket: /data/mysql/mysql.socket

Uptime: 15 min 17 sec

Threads: 1  Questions: 5  Slow queries: 0  Opens: 105  Flush tables: 1  Open tables: 98  Queries per second avg: 0.005

--------------

mysql> alter user root@'localhost' identified by 'MySQL@2022.';

Query OK, 0 rows affected (0.00 sec)

mysql> exit

Bye

二进制安装

1、安装相关包

yum -y install libaio numactl-libs

2、创建用户和组

groupadd mysql

useradd -r -g mysql -s /bin/false mysql

3、准备程序文件

wget http://mirrors.163.com/mysql/Downloads/MySQL-5.7/mysql-5.7.31-linuxglibc2.12-x86_64.tar.gz

tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local

cd /usr/local/

ln -s mysql-5.7.31-linux-glibc2.12-x86_64/ mysql

chown -R root.root /usr/local/mysql/

4、准备环境变量

echo 'PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh

. /etc/profile.d/mysql.sh

5、准备配置文件

cp /etc/my.cnf{,.bak}

vim /etc/my.cnf

[mysqld]

datadir=/data/mysql

skip_name_resolve=1

socket=/data/mysql/mysql.sock

log-error=/data/mysql/mysql.log

pid-file=/data/mysql/mysql.pid

[client]

socket=/data/mysql/mysql.sock

6、初始化数据库文件并提取root密码,生成 root 空密码

mysqld --initialize-insecure --user=mysql --datadir=/data/mysql

7、准备服务脚本和启动

cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld

chkconfig --add mysqld

service mysqld start

8、修改口令

#修改前面生成的空密码为指定密码(密码要定期修改)

mysqladmin -uroot password MySQL@2022.

9、测试登录

mysql -uroot -pMySQL@2022.


3、二进制安装mariadb10.4

一、下载

[root@localhost src]#wget http://mirrors.dotsrc.org/mariadb//mariadb-10.4.13/bintar-linux-x86_64/mariadb-10.4.13-linux-x86_64.tar.gz

[root@localhost src]# tar -zxvf mariadb-10.4.13-linux-x86_64.tar.gz

[root@localhost src]# mv mariadb-10.4.13-linux-x86_64 /usr/local/mariadb

[root@localhost mariadb]# ls

bin      CREDITS  docs              include        lib  mysql-test  README-wsrep  share      support-files

COPYING  data    EXCEPTIONS-CLIENT  INSTALL-BINARY  man  README.md  scripts      sql-bench  THIRDPARTY

二、配置

新建组及用户

[root@localhost mariadb]# groupadd mysql

[root@localhost mariadb]# useradd -s /sbin/nologin -r -g mysql mysql

配置服务

[root@localhost mariadb]# cp support-files/mysql.server /etc/init.d/mysqld

[root@localhost mariadb]# chmod +x /etc/init.d/mysqld

新建数据目录与权限

[root@localhost mariadb]# mkdir -p /data/mariadb/

[root@localhost mariadb]# chown -R mysql. /data/mariadb

[root@localhost mariadb]# chown -R mysql:mysql /usr/local/mariadb/

配置my.cnf

[root@localhost mariadb]# vim my.cnf

[mysqld]

basedir=/usr/local/mariadb/

datadir=/data/mariadb/

port=3306

pid-file=/data/mariadb/mysql.pid

socket=/tmp/mysql.sock

[mysqld_safe]

log-error=/data/mariadb/mysql.log

[client]

port=3306

socket=/tmp/mysql.sock

default-character-set=utf8

三、数据库初始化

/usr/local/mariadb/scripts/mysql_install_db --defaults-file=/usr/local/mariadb/my.cnf --user=mysql --datadir=/data/mariadb/ --basedir=/usr/local/mariadb/

[root@localhost ~]# /usr/local/mariadb/scripts/mysql_install_db --defaults-file=/usr/local/mariadb/my.cnf --user=mysql --datadir=/data/mariadb/ --basedir=/usr/local/mariadb/

Installing MariaDB/MySQL system tables in '/data/mariadb/' ...

OK

To start mysqld at boot time you have to copy

support-files/mysql.server to the right place for your system

Two all-privilege accounts were created.

One is root@localhost, it has no password, but you need to

be system 'root' user to connect. Use, for example, sudo mysql

The second is mysql@localhost, it has no password either, but

you need to be the system 'mysql' user to connect.

After connecting you can set the password, if you would need to be

able to connect as any of these users with a password and without sudo

See the MariaDB Knowledgebase at http://mariadb.com/kb or the

MySQL manual for more instructions.

You can start the MariaDB daemon with:

cd '/usr/local/mariadb/' ; /usr/local/mariadb//bin/mysqld_safe --datadir='/data/mariadb/'

You can test the MariaDB daemon with mysql-test-run.pl

cd '/usr/local/mariadb//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/

[root@localhost ~]# echo $?

0

四、base目录权限切回root

[root@localhost mariadb]# chown -R root:root /usr/local/mariadb/

五、配置环境变量

配置环境变量

[root@localhost ~]#echo "PATH=/usr/local/mariadb/bin:$PATH" >/etc/profile.d/mariadb.sh

[root@localhost ~]#chmod +x /etc/profile.d/mariadb.sh

[root@localhost ~]#source /etc/profile.d/mariadb.sh

六、启动与登录

[root@localhost ~]# /etc/init.d/mysqld start

Starting mysqld (via systemctl):     

[root@localhost ~]# mysql

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)

报错,

[root@localhost ~]# ps -ef|grep mysql

root      4386  3375  0 13:54 pts/0    00:00:00 grep --color=auto mysql

解决方法:

1、rm -rf /data/mariadb/* 数据目录

[root@localhost ~]# rm -rf /data/mariadb/*

2、重新初始化 ,这里加上–defaults-file=/usr/local/mariadb/my.cnf

[root@localhost ~]# /usr/local/mariadb/scripts/mysql_install_db --defaults-file=/usr/local/mariadb/my.cnf --user=mysql --datadir=/data/mariadb/ --basedir=/usr/local/mariadb/    内容省略

3、换安全模式启动,登录改root密码

mysqld_safe --defaults-file=/usr/local/mariadb/my.cnf &

[root@localhost ~]# mysqld_safe --defaults-file=/usr/local/mariadb/my.cnf &

[1] 4692

[root@localhost ~]# 200605 14:00:41 mysqld_safe Logging to '/data/mariadb/mysql.log'.

200605 14:00:41 mysqld_safe Starting mysqld daemon with databases from /data/mariadb/

[root@localhost ~]# mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 8

Server version: 10.4.13-MariaDB 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)]>

MariaDB [(none)]> show databases;

+--------------------+

| Database          |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test              |

+--------------------+

4 rows in set (0.002 sec)

MariaDB [(none)]> use mysql

MariaDB [mysql]> alter user 'root'@'localhost' identified by '123456';

Query OK, 0 rows affected (0.002 sec)

MariaDB [mysql]> FLUSH PRIVILEGES;

Query OK, 0 rows affected (0.001 sec)

4、配置安全模式

[root@localhost ~]# mysql_secure_installation --basedir=/usr/local/mariadb/

print: /usr/local/mariadb//bin/my_print_defaults

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):

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:

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] 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!

5、重启服务,再mysql登录

[root@localhost ~]# /etc/init.d/mysqld stop

[root@localhost ~]# /etc/init.d/mysqld start

Starting mysqld (via systemctl):                          [  OK  ]

[root@localhost ~]# ps -ef|grep mysql

root      7104    1  0 14:27 ?        00:00:00 /bin/sh /usr/local/mariadb//bin/mysqld_safe --datadir=/data/mariadb/ --pid-file=/data/mariadb/mysql.pid

mysql    7219  7104  4 14:27 ?        00:00:00 /usr/local/mariadb/bin/mysqld --basedir=/usr/local/mariadb/ --datadir=/data/mariadb/ --plugin-dir=/usr/local/mariadb//lib/plugin --user=mysql --log-error=/data/mariadb/mysql.log --pid-file=/data/mariadb/mysql.pid --socket=/tmp/mysql.sock --port=3306

root      7257  3375  0 14:27 pts/0    00:00:00 grep --color=auto mysql

[root@localhost ~]#

[root@localhost ~]# mysql  这里可以正常登录了

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 9

Server version: 10.4.13-MariaDB 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          |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

+--------------------+

3 rows in set (0.001 sec)

你可能感兴趣的:(M-63.第十周作业)