- 1. 二进制格式mysql安装
下载软件包
- /usr/src中下载二进制格式的 mysql 软件包
[root@10 ~]# cd /usr/src [root@10 src]# ls mysql-5.7.37-linux-glibc2.12-x86_64.tar.gz 创建 用户 组 [root@10 src]# useradd -r -M -s /sbin/nologin mysql [root@10 src]# id mysql uid=993(mysql) gid=991(mysql) groups=991(mysql) 改名 [root@10 local]# mv mysql-5.7.37-linux-glibc2.12-x86_64/ mysql 改属组 [root@10 local]# chown -R mysql.mysql mysql [root@10 local]# ll total 0 drwxr-xr-x. 14 root root 164 Apr 16 11:53 apache drwxr-xr-x. 6 root root 58 Apr 16 11:12 apr drwxr-xr-x. 5 root root 43 Apr 16 11:15 apr-until drwxr-xr-x. 5 root root 43 Apr 16 11:41 apr-util 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 Apr 12 16:09 lib64 drwxr-xr-x. 2 root root 6 Jun 22 2021 libexec drwxr-xr-x 9 mysql mysql 129 Jun 28 15:43 mysql drwxr-xr-x. 2 root root 6 Jun 22 2021 sbin drwxr-xr-x. 5 root root 49 Apr 12 16:09 share drwxr-xr-x. 2 root root 6 Jun 22 2021 src 添加环境变量 [root@10 local]# ls mysql/ bin docs include lib LICENSE man README share support-files [root@10 local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh [root@10 local]# source /etc/profile.d/mysql.sh 映射文件 [root@10 mysql]# ln -s /usr/local/mysql/include /usr/include/mysql 配置lib库 [root@10 mysql]# vi /etc/ld.so.conf.d/mysql.conf /usr/local/mysql/lib [root@10 mysql]# vi /etc/man_db.conf 建立数据存放目录 [root@10 ~]# mkdir -p /opt/data [root@10 ~]# chown -R mysql.mysql /opt/data drwxr-xr-x 2 mysql mysql 6 Apr 21 16:02 data 初始化数据库 [root@10 ~]# mysqld --initialize --user mysql --datadir /opt/data 2022-06-28T08:12:04.648395Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2022-06-28T08:12:04.992983Z 0 [Warning] InnoDB: New log files created, LSN=45790 2022-06-28T08:12:05.161258Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 2022-06-28T08:12:05.187877Z 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: ff28a2df-f6b9-11ec-8e58-000c29487efd. 2022-06-28T08:12:05.190286Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened. 2022-06-28T08:12:06.114324Z 0 [Warning] A deprecated TLS version TLSv1 is enabled. Please use TLSv1.2 or higher. 2022-06-28T08:12:06.114384Z 0 [Warning] A deprecated TLS version TLSv1.1 is enabled. Please use TLSv1.2 or higher. 2022-06-28T08:12:06.115126Z 0 [Warning] CA certificate ca.pem is self signed. 2022-06-28T08:12:06.249625Z 1 [Note] A temporary password is generated for root@localhost: 5yU?Y&2%dSJ2 保存密码 [root@10 ~]# echo '5yU?Y&2%dSJ2' > pass 修改配置文件(没有就创一个) [root@localhost ~]# cp /etc/my.cnf /etc/my.cnf.back ##备份配置文件 [root@localhost ~]# vim /etc/my.cnf [root@localhost ~]# 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@10 mysql]# cd support-files/
[root@10 support-files]# ls
magic mysqld_multi.server mysql-log-rotate mysql.server
[root@10 support-files]# file mysql.server
mysql.server: POSIX shell script, ASCII text executable
[root@10 support-files]# ll
total 24
-rw-r--r-- 1 mysql mysql 773 Nov 30 2021 magic
-rwxr-xr-x 1 mysql mysql 1061 Nov 30 2021 mysqld_multi.server
-rwxr-xr-x 1 mysql mysql 894 Nov 30 2021 mysql-log-rotate
-rwxr-xr-x 1 mysql mysql 10576 Nov 30 2021 mysql.server
[root@10 support-files]# cp mysql.server mysqld
[root@10 support-files]# ll
total 36
-rw-r--r-- 1 mysql mysql 773 Nov 30 2021 magic
-rwxr-xr-x 1 root root 10576 Jun 28 16:23 mysqld
-rwxr-xr-x 1 mysql mysql 1061 Nov 30 2021 mysqld_multi.server
-rwxr-xr-x 1 mysql mysql 894 Nov 30 2021 mysql-log-rotate
-rwxr-xr-x 1 mysql mysql 10576 Nov 30 2021 mysql.server
[root@10 support-files]# chown -R mysql.mysql mysqld
[root@10 support-files]# ll
total 36
-rw-r--r-- 1 mysql mysql 773 Nov 30 2021 magic
-rwxr-xr-x 1 mysql mysql 10576 Jun 28 16:23 mysqld
-rwxr-xr-x 1 mysql mysql 1061 Nov 30 2021 mysqld_multi.server
-rwxr-xr-x 1 mysql mysql 894 Nov 30 2021 mysql-log-rotate
-rwxr-xr-x 1 mysql mysql 10576 Nov 30 2021 mysql.serv
[root@10 support-files]# vi mysqld
basedir=/usr/local/mysql
datadir=/opt/data
启动mysql
[root@10 ~]# /usr/local/mysql/support-files/mysqld start
Starting MySQL.Logging to '/opt/data/10.10.10.135.err'.
SUCCESS!
[root@10 ~]# ss -antl
State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess
LISTEN 0 128 0.0.0.0:111 0.0.0.0:*
LISTEN 0 32 192.168.122.1:53 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@10 ~]# dnf -y install ncurses-compat-libs
改密码
[root@10 ~]# cat pass
5yU?Y&2%dSJ2
[root@10 ~]# mysql -uroot -p'5yU?Y&2%dSJ2'
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.37
mysql> set password = password('runtime123!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> quit
Bye
[root@10 ~]# mysql -uroot -p'runtime123!'
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.37 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.
设置开机自启
[root@10 ~]# vi mysqld.service
[Unit]
Description=mysql server daemon
After=network.target sshd-keygen.target
[Service]
Type=forking
ExecStart=/usr/local/support-files/mysqld start
ExecStop=/usr/local/support-files/mysqld stop
ExecReload=/bin/kill -HUP $MAINPID
[root@10 ~]# mv mysqld.service /usr/lib/systemd/system/
[Install]
WantedBy=multi-user.target
[root@10 ~]# reboot
[root@10~]# systemctl status mysqld
● mysqld.service - MySQL Server
Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enable>
Active: active (running) since Mon 2022-06-28 17:51:34 CST; 24>
Docs: man:mysqld(8)
http://dev.mysql.com/doc/refman/en/using-systemd.html
Process: 2513 ExecStart=/usr/sbin/mysqld --daemonize --pid-file>
Process: 2464 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=ex>
Main PID: 2516 (mysqld)
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 破解密码
1. 修改配置文件,加入以下内容
skip-grants-table
2 重启mysql
3 修改密码
4 恢复配置文件 。 删除 skip-grants-table
5重启mysql
6 验证新密码是否能登陆
[root@10 ~]# vim /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
[root@10 ~]# systemctl restart mysqld
[root@10 ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 3
Server version: 5.7.37 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.
mysql> set password = password('Kexuan0822!');
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> update user set authentication_string = password('123456') where User = 'root' and Host = 'localhost
[root@10 ~]# vim /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
[root@10 ~]# systemctl restart mysqld
[root@10 ~]# mysql -uroot -p123456
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 2
Server version: 5.7.37
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.



