1. 程式人生 > 實用技巧 >mysql進階命令

mysql進階命令

1.下載二進位制的資料庫包,地址為:https://downloads.mysql.com/archives/get/p/23/file/mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz,再傳輸到虛擬機器。再建立系統組mysql,再建立使用者mysql,不要自動建立使用者的登入目錄,指定使用者所屬的組為mysql,並且沒有可以登陸的shell。

[root@localhost ~]# cd /usr/src/
[root@localhost src]# ls
debug  kernels  mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
[root@localhost src]# groupadd 
-r mysql groupadd: group 'mysql' already exists [root@localhost src]# useradd -M -s /sbin/nologin -g mysql mysql

2.講資料庫包解壓到/usr/local下,再給解壓後的目錄生成一個軟連結mysql,把/usr/local/mysql的屬主與屬組更改為mysql。

[root@localhost src]# tar xf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz -C /usr/local
[root@localhost src]# cd /usr/local
[root@localhost local]# ls
bin  games    lib    libexec                              sbin   src
etc  include  lib64  mysql
-5.7.31-linux-glibc2.12-x86_64 share [root@localhost local]# ln -sv mysql-5.7.31-linux-glibc2.12-x86_64/ mysql 'mysql' -> 'mysql-5.7.31-linux-glibc2.12-x86_64/' [root@localhost local]# ll total 0 drwxr-xr-x. 2 root root 6 Aug 12 2018 bin drwxr-xr-x. 2 root root 6 Aug 12 2018 etc drwxr-xr-x. 2
root root 6 Aug 12 2018 games drwxr-xr-x. 2 root root 6 Aug 12 2018 include drwxr-xr-x. 2 root root 6 Aug 12 2018 lib drwxr-xr-x. 2 root root 6 Aug 12 2018 lib64 drwxr-xr-x. 2 root root 6 Aug 12 2018 libexec lrwxrwxrwx. 1 root root 36 Dec 28 15:35 mysql -> mysql-5.7.31-linux-glibc2.12-x86_64/ drwxr-xr-x. 9 7161 31415 129 Jun 2 2020 mysql-5.7.31-linux-glibc2.12-x86_64 drwxr-xr-x. 2 root root 6 Aug 12 2018 sbin drwxr-xr-x. 5 root root 49 Nov 13 22:08 share drwxr-xr-x. 2 root root 6 Aug 12 2018 src [root@localhost local]# chown -R mysql:mysql /usr/local/mysql [root@localhost local]# ll /usr/local/mysql -d lrwxrwxrwx. 1 mysql mysql 36 Dec 28 15:35 /usr/local/mysql -> mysql-5.7.31-linux-glibc2.12-x86_64/

3.新增環境變數,再建立資料存放目錄/opt/data。

[root@localhost local]# ls /usr/local/mysql
bin  docs  include  lib  LICENSE  man  README  share  support-files
[root@localhost local]# echo 'export PATH=/usr/local/mysql/bin:$PATH' > /etc/profile.d/mysql.sh
[root@localhost local]# source /etc/profile.d/mysql.sh 
[root@localhost local]# echo $PATH
/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
[root@localhost local]# mkdir /opt/data
[root@localhost local]# chown -R mysql:mysql /opt/data/
[root@localhost local]# ll /opt/
total 0
drwxr-xr-x. 2 mysql mysql 6 Dec 28 15:44 data

4.初始化資料庫

[root@localhost local]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --datadir=/opt/data/
2020-12-28T07:52:02.545659Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2020-12-28T07:52:03.204103Z 0 [Warning] InnoDB: New log files created, LSN=45790
2020-12-28T07:52:03.303585Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2020-12-28T07:52:03.370771Z 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: 92dca673-48e1-11eb-a9f4-000c29b5300b.
2020-12-28T07:52:03.371455Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2020-12-28T07:52:03.628412Z 0 [Warning] CA certificate ca.pem is self signed.
2020-12-28T07:52:03.949543Z 1 [Note] A temporary password is generated for root@localhost: %qt+-kq9R8&Z

//請注意,這個命令的最後會生成一個臨時密碼,此處密碼是%qt+-kq9R8&Z
//再次注意,這個密碼是隨機的,你的不會跟我一樣,一定要記住這個密碼,因為一會登入時會用到

mysql的配置檔案為/etc/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將無法正常處理連線請求

5.生成配置檔案

[root@localhost local]# vi /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

6.配置服務啟動指令碼

[root@localhost local]# cp -a /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
[root@localhost local]# vi /etc/init.d/mysqld
# If you change base dir, you must also change datadir. These may get
# overwritten by settings in the MySQL configuration files.

basedir=/usr/local/mysql      #加上路徑
datadir=/opt/data             #加上路徑

# Default value, in seconds, afterwhich the script should timeout waiting
# for server start.
# Value here is overriden by value in my.cnf.

7.啟動mysql服務

[root@localhost local]# service mysqld start
Starting MySQL.Logging to '/opt/data/localhost.localdomain.err'.
... SUCCESS! 
[root@localhost local]# ss -antl
State    Recv-Q   Send-Q     Local Address:Port       Peer Address:Port   
LISTEN   0        128              0.0.0.0:22              0.0.0.0:*      
LISTEN   0        80                     *:3306                  *:*      
LISTEN   0        128                 [::]:22                 [::]:* 
root@localhost ~]# mysql -uroot -p'%qt+-kq9R8&Z'
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No such file or directory
[root@localhost ~]# yum whatprovides libncurses.so.5
Updating Subscription Management repositories.
Unable to read consumer identity
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Last metadata expiration check: 2:45:08 ago on Mon 28 Dec 2020 03:49:42 PM CST.
ncurses-compat-libs-6.1-7.20180224.el8.i686 : Ncurses compatibility
                                            : libraries
Repo        : BaseOS
Matched from:
Provide    : libncurses.so.5
[root@localhost ~]# yum -y install ncurses-compat-libs
[root@localhost ~]# mysql -uroot -p'%qt+-kq9R8&Z'

8.更改密碼為123456,並設定開機自動啟動(mysql是6以前的版本)

mysql> set password = password('123456');
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> quit
Bye
[root@localhost ~]# chkconfig --add mysqld
[root@localhost ~]# chkconfig mysqld on
[root@localhost ~]# 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]'.

mysqld             0:off    1:off    2:on    3:on    4:on    5:on    6:off

9.mysql配置檔案

[root@localhost ~]# vi .my.cnf

[client]
user=root
password=123456
[root@localhost ~]# mysql
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.31 MySQL Community Server (GPL)

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> 

10.破解mysql密碼,修改為csl123

[root@localhost ~]# vi /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@localhost ~]# service mysqld restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL. SUCCESS!
[root@localhost ~]# mysql
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> select * from user\G     #查詢user=root host=localhost裡內容
mysql> update user set authentication_string=password('csl123')where User='root' and Host='localhost';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1
mysql> quit
Bye
[root@localhost ~]# vi /etc/my.cnf    #刪除skip-grant-tables
[root@localhost ~]# service mysqld restart
Shutting down MySQL.... SUCCESS! 
Starting MySQL. SUCCESS! 
[root@localhost ~]# mysql -uroot -pcsl123

備份方案特點
全量備份 全量備份就是指對某一個時間點上的所有資料或應用進行的一個完全拷貝。
資料恢復快。
備份時間長
增量備份 增量備份是指在一次全備份或上一次增量備份後,以後每次的備份只需備份
與前一次相比增加和者被修改的檔案。這就意味著,第一次增量備份的物件
是進行全備後所產生的增加和修改的檔案;第二次增量備份的物件是進行第一次增量
備份後所產生的增加和修改的檔案,如此類推。

沒有重複的備份資料
備份時間短
恢復資料時必須按一定的順序進行
差異備份 備份上一次的完全備份後發生變化的所有檔案。
差異備份是指在一次全備份後到進行差異備份的這段時間內
對那些增加或者修改檔案的備份。在進行恢復時,我們只需對第一次全量備份和最後一次差異備份進行恢復。

11.資料庫備份與恢復,全量備份到檔案all.sql

[root@localhost ~]# mysql -uroot -pcsl123
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 4
Server version: 5.7.31 MySQL Community Server (GPL)

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> create database school;
Query OK, 1 row affected (0.00 sec)

mysql> use school;
Database changed
mysql> create table student(id int not null primary key auto_increment,name varchar(50),age tinyint);
Query OK, 0 rows affected (0.16 sec)

mysql> insert student(name,age)values('tom',12),('xiaoming',23),('xiaohong',18);
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0
mysql> quit
Bye
[root@localhost ~]# mysqldump -uroot -pcsl123 --all-databases > all.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# ls
all.sql  anaconda-ks.cfg  csl  passwd
[root@localhost ~]# file all.sql 
all.sql: UTF-8 Unicode text, with very long lines
[root@localhost ~]# mysql -uroot -pcsl123 -e 'drop database school;'
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -pcsl123 < all.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[root@localhost ~]# mysql -uroot -pcsl123 -e 'select * from school.student;'
mysql: [Warning] Using a password on the command line interface can be insecure.
+----+----------+------+
| id | name     | age  |
+----+----------+------+
|  1 | tom      |   12 |
|  2 | xiaoming |   23 |
|  3 | xiaohong |   18 |
+----+----------+------+