mysql二进制安装

mysql进阶(上)


文章目录

  • mysql进阶(上)
    • 1. 二进制安装mysql
    • 2. mysql配置文件
    • 3. 数据库密码破解
      • 3.1 编辑mysql配置文件,添加一行: skip-grant-tables
      • 3.2 重启服务
      • 3.3 验证登入
      • 3.4 修改密码
      • 3.5 删除修改/etc/my.cnf中skip-grant-tables并重启服务
      • 3.6 用新的密码去验证登入

1. 二进制安装mysql

//下载二进制格式的mysql软件包

[root@150 ~]# cd /usr/src
[root@150 src]# wget https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
--2022-07-26 18:25:40--  https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
Resolving downloads.mysql.com (downloads.mysql.com)... 23.76.76.109, 2402:4f00:4002:194::2e31, 2402:4f00:4002:182::2e31
Connecting to downloads.mysql.com (downloads.mysql.com)|23.76.76.109|:443... connected.
HTTP request sent, awaiting response... 302 Moved Temporarily
Location: https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz [following]
--2022-07-26 18:25:41--  https://cdn.mysql.com/archives/mysql-5.7/mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz
Resolving cdn.mysql.com (cdn.mysql.com)... 23.194.211.12
Connecting to cdn.mysql.com (cdn.mysql.com)|23.194.211.12|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 674830866 (644M) [application/x-tar-gz]
Saving to: ‘mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz’

mysql-5.7.38-linux-glibc2.12-x86_64.tar.g 100%[===================================================================================>] 643.57M  28.3MB/s    in 25s

2022-07-26 18:26:07 (25.3 MB/s) - ‘mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz’ saved [674830866/674830866]

[root@150 src]# ls
annobin  debug  kernels  mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz  mysql57-community-release-el7-11.noarch.rpm
[root@150 src]#

//创建用户和组

[root@150 src]# groupadd -r mysql
[root@150 src]# useradd -M -r -s  /sbin/nologin -g mysql mysql
[root@150 src]# id mysql
uid=299(mysql) gid=299(mysql) groups=299(mysql)
[root@150 src]#

//解压软件至/usr/local/

[root@150 src]# tar xf mysql-5.7.38-linux-glibc2.12-x86_64.tar.gz -C /usr/local/
[root@150 src]# ls /usr/local
apache  apr  apr-until  bin  etc  games  include  lib  lib64  libexec  mysql-5.7.38-linux-glibc2.12-x86_64  nginx  sbin  share  src
[root@150 src]# cd /usr/local/
[root@150 local]#
[root@150 local]#
[root@150 local]# ln -sv mysql-5.7.38-linux-glibc2.12-x86_64/  mysql
'mysql' -> 'mysql-5.7.38-linux-glibc2.12-x86_64/'
[root@150 local]# ll
total 0
drwxr-xr-x. 14 root root 164 Jul 14 18:59 apache
drwxr-xr-x.  6 root root  58 Jul 14 18:49 apr
drwxr-xr-x.  5 root root  43 Jul 14 18:54 apr-until
drwxr-xr-x.  2 root root   6 Jun 22  2021 bin
drwxr-xr-x.  2 root root   6 Jun 22  2021 etc
drwxr-xr-x.  2 root root   6 Jun 22  2021 games
drwxr-xr-x.  2 root root   6 Jun 22  2021 include
drwxr-xr-x.  2 root root   6 Jun 22  2021 lib
drwxr-xr-x.  3 root root  17 Jun 27 14:18 lib64
drwxr-xr-x.  2 root root   6 Jun 22  2021 libexec
lrwxrwxrwx.  1 root root  36 Jul 26 18:36 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
drwxr-xr-x.  9 root root 129 Jul 26 18:33 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 11 root root 164 Jul 14 16:31 nginx
drwxr-xr-x.  2 root root   6 Jun 22  2021 sbin
drwxr-xr-x.  5 root root  49 Jun 27 14:18 share
drwxr-xr-x.  2 root root   6 Jun 22  2021 src
[root@150 local]#

//修改目录/usr/local/mysql的属主属组

[root@150 local]# chown -R mysql.mysql mysql
[root@150 local]# chown -R mysql.mysql mysql-5.7.38-linux-glibc2.12-x86_64/
[root@150 local]# ll
total 0
drwxr-xr-x. 14 root  root  164 Jul 14 18:59 apache
drwxr-xr-x.  6 root  root   58 Jul 14 18:49 apr
drwxr-xr-x.  5 root  root   43 Jul 14 18:54 apr-until
drwxr-xr-x.  2 root  root    6 Jun 22  2021 bin
drwxr-xr-x.  2 root  root    6 Jun 22  2021 etc
drwxr-xr-x.  2 root  root    6 Jun 22  2021 games
drwxr-xr-x.  2 root  root    6 Jun 22  2021 include
drwxr-xr-x.  2 root  root    6 Jun 22  2021 lib
drwxr-xr-x.  3 root  root   17 Jun 27 14:18 lib64
drwxr-xr-x.  2 root  root    6 Jun 22  2021 libexec
lrwxrwxrwx.  1 mysql mysql  36 Jul 26 18:36 mysql -> mysql-5.7.38-linux-glibc2.12-x86_64/
drwxr-xr-x.  9 mysql mysql 129 Jul 26 18:33 mysql-5.7.38-linux-glibc2.12-x86_64
drwxr-xr-x. 11 root  root  164 Jul 14 16:31 nginx
drwxr-xr-x.  2 root  root    6 Jun 22  2021 sbin
drwxr-xr-x.  5 root  root   49 Jun 27 14:18 share
drwxr-xr-x.  2 root  root    6 Jun 22  2021 src
[root@150 local]#

//添加环境变量

[root@150 local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql  mysql-5.7.38-linux-glibc2.12-x86_64  sbin  share  src
[root@150 local]# echo 'export PATH=/usr/local/mysql/bin:$PATH'>/etc/profile.d/mysql.sh
[root@150 local]# . /etc/profile.d/mysql.sh
[root@150 local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/apache/bin/apache:/usr/local/nginx/sbin:/root/bin
[root@150 local]#
[root@150 local]# cd mysql
[root@150 mysql]# ln -s /usr/local/mysql/include/ /usr/include/mysql
[root@150 mysql]# ll /usr/include/
lrwxrwxrwx.  1 root root     25 Jul 26 18:53 mysql -> /usr/local/mysql/include/
[root@150 mysql]# ls
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@150 mysql]# vim /etc/ld.so.conf.d/mysql.conf
[root@150 mysql]# cat /etc/ld.so.conf.d/mysql.conf
/usr/local/mysql/lib
[root@150 mysql]# ldconfig
[root@150 mysql]# pwd
/usr/local/mysql
[root@150 mysql]# vim /etc/man_db.conf
 22 MANDATORY_MANPATH                       /usr/local/mysql/man
[root@150 mysql]#

//建立数据存放目录

[root@150 mysql]# mkdir /opt/data
[root@150 mysql]# chown -R mysql.mysql /opt/data/
[root@150 mysql]# ll -d /opt/data/
drwxr-xr-x. 2 mysql mysql 6 Jul 26 19:02 /opt/data/

//初始化数据库

[root@150 mysql]# mysqld --initialize --user=mysql --datadir=/opt/data
2022-07-26T11:14:32.501635Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2022-07-26T11:14:32.645781Z 0 [Warning] InnoDB: New log files created, LSN=45790
2022-07-26T11:14:32.666168Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2022-07-26T11:14:32.721561Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: 1ff6b580-0cd4-11ed-84b5-000c296b6021.
2022-07-26T11:14:32.722450Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2022-07-26T11:14:32.862202Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher.
2022-07-26T11:14:32.862224Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher.
2022-07-26T11:14:32.862626Z 0 [Warning] CA certificate ca.pem is self signed.
2022-07-26T11:14:32.961643Z 1 [Note] A temporary password is generated for root@localhost: ifuk%1Mt+eKc
[root@150 mysql]#

//这个命令的最后会生成一个临时密码,此处密码是ifuk%1Mt+eKc,密码是随机的。

//生成配置文件

[root@150 ~]# vim /etc/my.cnf
[root@150 ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[root@150 ~]#

//配置服务启动脚本

[root@150 ~]# cd /usr/local/mysql/support-files/
[root@150 support-files]#
[root@150 support-files]#
[root@150 support-files]# ls
magic  mysqld_multi.server  mysql-log-rotate  mysql.server
[root@150 support-files]# file mysql.server
mysql.server: POSIX shell script, ASCII text executable
[root@150 support-files]# cp -a mysql.server /etc/init.d/mysql
[root@150 support-files]#
[root@150 support-files]# vim /etc/init.d/mysql
46 basedir=/usr/local/mysql
47 datadir=/opt/data

//启动mysql

[root@150 ~]# service mysql start
Starting MySQL.Logging to '/opt/data/150.err'.
 SUCCESS!
[root@150 ~]# ps -ef |grep mysql
root        2415       1  0 19:35 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/opt/data --pid-file=/opt/data/mysql.pid
mysql       2615    2415  1 19:35 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/opt/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=150.err --pid-file=/opt/data/mysql.pid --socket=/tmp/mysql.sock --port=3306
root        2645    1729  0 19:35 pts/0    00:00:00 grep --color=auto mysql
[root@150 ~]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port       Peer Address:Port   Process
LISTEN   0        128              0.0.0.0:111             0.0.0.0:*
LISTEN   0        128              0.0.0.0:22              0.0.0.0:*
LISTEN   0        80                     *:3306                  *:*
LISTEN   0        128                 [::]:111                [::]:*
LISTEN   0        128                 [::]:22                 [::]:*
[root@150 ~]#

//修改密码
//使用临时密码登录

[root@150 ~]# yum -y install ncurses-compat-libs
appstream                                           1.1 MB/s | 4.4 kB     00:00
baseos                                              907 kB/s | 3.9 kB     00:00
Dependencies resolved.
====================================================================================
 Package                   Architecture Version                  Repository    Size
====================================================================================
Installing:
 ncurses-compat-libs       x86_64       6.1-9.20180224.el8       BaseOS       328 k

Transaction Summary
====================================================================================
Install  1 Package

Total size: 328 k
Installed size: 1.0 M
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                            1/1
  Installing       : ncurses-compat-libs-6.1-9.20180224.el8.x86_64              1/1
  Running scriptlet: ncurses-compat-libs-6.1-9.20180224.el8.x86_64              1/1
  Verifying        : ncurses-compat-libs-6.1-9.20180224.el8.x86_64              1/1

Installed:
  ncurses-compat-libs-6.1-9.20180224.el8.x86_64

Complete!
[root@150 ~]#
[root@150 ~]# mysql -uroot -pifuk%1Mt+eKc
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.38

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> exit
Bye
[root@150 ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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>

2. mysql配置文件


mysql的配置文件为/etc/my.cnf
配置文件查找次序:若在多个配置文件中均有设定,则最后找到的最终生效

/etc/my.cnf --> /etc/mysql/my.cnf --> --default-extra-file=/PATH/TO/CONF_FILE --> ~/.my.cnf

mysql常用配置文件参数:

参数 说明
port = 3306 设置监听端口
socket = /tmp/mysql.sock 指定套接字文件位置
basedir = /usr/local/mysql 指定MySQL的安装路径
datadir = /data/mysql 指定MySQL的数据存放路径
pid-file = /data/mysql/mysql.pid 指定进程ID文件存放路径
user = mysql 指定MySQL以什么用户的身份提供服务
skip-name-resolve 禁止MySQL对外部连接进行DNS解析,使用这一选项可以消除MySQL进行DNS解析的时间。若开启该选项,则所有远程主机连接授权都要使用IP地址方式,否则MySQL将无法正常处理连接请求

3. 数据库密码破解


3.1 编辑mysql配置文件,添加一行: skip-grant-tables

[root@150 ~]# vim /etc/my.cnf
[root@150 ~]# cat /etc/my.cnf
[mysqld]
basedir = /usr/local/mysql
datadir = /opt/data
socket = /tmp/mysql.sock
port = 3306
pid-file = /opt/data/mysql.pid
user = mysql
skip-name-resolve
skip-grant-tables
sql-mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
[root@150 ~]#

3.2 重启服务

[root@150 ~]# service mysqld restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@150 ~]#

3.3 验证登入

[root@150 ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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>

3.4 修改密码

mysql> select * from user\G
*************************** 1. row ***************************
                  Host: localhost
                  User: root
 authentication_string: *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>
mysql> update user set authentication_string = Password('123456') where Host = 'localhost' and User = 'root';
Query OK, 0 rows affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 0  Warnings: 1

mysql>

3.5 删除修改/etc/my.cnf中skip-grant-tables并重启服务

[root@150 ~]# vim /etc/my.cnf
[root@150 ~]# service mysql restart
Shutting down MySQL.. SUCCESS!
Starting MySQL. SUCCESS!
[root@150 ~]#

3.6 用新的密码去验证登入

[root@150 ~]# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.38 MySQL Community Server (GPL)

Copyright (c) 2000, 2022, Oracle and/or its affiliates.

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>

你可能感兴趣的:(mysql,linux,数据库)