Mariadb

https://www.yuque.com/liweiming/linux/kca7w3

Linux 部署

https://dev.mysql.com/downloads/mysql/
rpm包标准安装包5个

mysql-community-server mysql-community-client mysql-community-libs mysql-community-common mysql-community-libs-compat

image.png

mysql-community-server-8.0.21-1.el7.x86_64.rpm
mysql-community-client-8.0.21-1.el7.x86_64.rpm
mysql-community-libs-8.0.21-1.el7.x86_64.rpm
mysql-community-common-8.0.21-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.21-1.el7.x86_64.rpm

  1. [root@riyimei tmp]# ll
  2. total 565996
  3. -rw-r--r-- 1 root root 49947596 Jul 25 09:10 mysql-community-client-8.0.21-1.el7.x86_64.rpm
  4. -rw-r--r-- 1 root root 631676 Jul 25 09:12 mysql-community-common-8.0.21-1.el7.x86_64.rpm
  5. -rw-r--r-- 1 root root 4765268 Jul 25 09:11 mysql-community-libs-8.0.21-1.el7.x86_64.rpm
  6. -rw-r--r-- 1 root root 1277020 Jul 25 09:12 mysql-community-libs-compat-8.0.21-1.el7.x86_64.rpm
  7. -rw-r--r-- 1 root root 522944772 Jul 25 09:06 mysql-community-server-8.0.21-1.el7.x86_64.rpm
  8. [root@riyimei tmp]# yum localinstall mysql-community*
  9. Loaded plugins: fastestmirror
  10. Examining mysql-community-client-8.0.21-1.el7.x86_64.rpm: mysql-community-client-8.0.21-1.el7.x86_64
  11. Marking mysql-community-client-8.0.21-1.el7.x86_64.rpm to be installed
  12. Examining mysql-community-common-8.0.21-1.el7.x86_64.rpm: mysql-community-common-8.0.21-1.el7.x86_64
  13. Marking mysql-community-common-8.0.21-1.el7.x86_64.rpm to be installed
  14. Examining mysql-community-libs-8.0.21-1.el7.x86_64.rpm: mysql-community-libs-8.0.21-1.el7.x86_64
  15. Marking mysql-community-libs-8.0.21-1.el7.x86_64.rpm to be installed
  16. Examining mysql-community-libs-compat-8.0.21-1.el7.x86_64.rpm: mysql-community-libs-compat-8.0.21-1.el7.x86_64
  17. Marking mysql-community-libs-compat-8.0.21-1.el7.x86_64.rpm to be installed
  18. Examining mysql-community-server-8.0.21-1.el7.x86_64.rpm: mysql-community-server-8.0.21-1.el7.x86_64
  19. Marking mysql-community-server-8.0.21-1.el7.x86_64.rpm to be installed
  20. Resolving Dependencies
  21. --> Running transaction check
  22. ---> Package mariadb.x86_64 1:5.5.65-1.el7 will be obsoleted
  23. ---> Package mariadb-libs.x86_64 1:5.5.65-1.el7 will be obsoleted
  24. ---> Package mariadb-server.x86_64 1:5.5.65-1.el7 will be obsoleted
  25. ---> Package mysql-community-client.x86_64 0:8.0.21-1.el7 will be obsoleting
  26. ---> Package mysql-community-common.x86_64 0:8.0.21-1.el7 will be installed
  27. ---> Package mysql-community-libs.x86_64 0:8.0.21-1.el7 will be obsoleting
  28. ---> Package mysql-community-libs-compat.x86_64 0:8.0.21-1.el7 will be obsoleting
  29. ---> Package mysql-community-server.x86_64 0:8.0.21-1.el7 will be obsoleting
  30. --> Finished Dependency Resolution
  31. Dependencies Resolved
  32. =======================================================================================================================================
  33. Package Arch Version Repository Size
  34. =======================================================================================================================================
  35. Installing:
  36. mysql-community-client x86_64 8.0.21-1.el7 /mysql-community-client-8.0.21-1.el7.x86_64 231 M
  37. replacing mariadb.x86_64 1:5.5.65-1.el7
  38. mysql-community-common x86_64 8.0.21-1.el7 /mysql-community-common-8.0.21-1.el7.x86_64 8.8 M
  39. mysql-community-libs x86_64 8.0.21-1.el7 /mysql-community-libs-8.0.21-1.el7.x86_64 22 M
  40. replacing mariadb-libs.x86_64 1:5.5.65-1.el7
  41. mysql-community-libs-compat x86_64 8.0.21-1.el7 /mysql-community-libs-compat-8.0.21-1.el7.x86_64 6.1 M
  42. replacing mariadb-libs.x86_64 1:5.5.65-1.el7
  43. mysql-community-server x86_64 8.0.21-1.el7 /mysql-community-server-8.0.21-1.el7.x86_64 2.3 G
  44. replacing mariadb-server.x86_64 1:5.5.65-1.el7
  45. Transaction Summary
  46. =======================================================================================================================================
  47. Install 5 Packages
  48. Total size: 2.6 G
  49. Is this ok [y/d/N]: y
  50. Downloading packages:
  51. Running transaction check
  52. Running transaction test
  53. Transaction test succeeded
  54. Running transaction
  55. Installing : mysql-community-common-8.0.21-1.el7.x86_64 1/8
  56. Installing : mysql-community-libs-8.0.21-1.el7.x86_64 2/8
  57. Installing : mysql-community-client-8.0.21-1.el7.x86_64 3/8
  58. Installing : mysql-community-server-8.0.21-1.el7.x86_64 4/8
  59. Installing : mysql-community-libs-compat-8.0.21-1.el7.x86_64 5/8
  60. Erasing : 1:mariadb-server-5.5.65-1.el7.x86_64 6/8
  61. warning: /var/log/mariadb/mariadb.log saved as /var/log/mariadb/mariadb.log.rpmsave
  62. Erasing : 1:mariadb-5.5.65-1.el7.x86_64 7/8
  63. Erasing : 1:mariadb-libs-5.5.65-1.el7.x86_64 8/8
  64. Verifying : mysql-community-server-8.0.21-1.el7.x86_64 1/8
  65. Verifying : mysql-community-libs-8.0.21-1.el7.x86_64 2/8
  66. Verifying : mysql-community-client-8.0.21-1.el7.x86_64 3/8
  67. Verifying : mysql-community-libs-compat-8.0.21-1.el7.x86_64 4/8
  68. Verifying : mysql-community-common-8.0.21-1.el7.x86_64 5/8
  69. Verifying : 1:mariadb-server-5.5.65-1.el7.x86_64 6/8
  70. Verifying : 1:mariadb-libs-5.5.65-1.el7.x86_64 7/8
  71. Verifying : 1:mariadb-5.5.65-1.el7.x86_64 8/8
  72. Installed:
  73. mysql-community-client.x86_64 0:8.0.21-1.el7 mysql-community-common.x86_64 0:8.0.21-1.el7
  74. mysql-community-libs.x86_64 0:8.0.21-1.el7 mysql-community-libs-compat.x86_64 0:8.0.21-1.el7
  75. mysql-community-server.x86_64 0:8.0.21-1.el7
  76. Replaced:
  77. mariadb.x86_64 1:5.5.65-1.el7 mariadb-libs.x86_64 1:5.5.65-1.el7 mariadb-server.x86_64 1:5.5.65-1.el7
  78. Complete!
  79. [root@riyimei tmp]#
  1. [root@riyimei mysql]# pwd
  2. /var/lib/mysql
  3. [root@riyimei mysql]# ls
  4. auto.cnf binlog.index client-key.pem ib_buffer_pool ibtmp1 mysql.sock public_key.pem test1
  5. babytun ca-key.pem hisystem ibdata1 #innodb_temp mysql.sock.lock server-cert.pem undo_001
  6. binlog.000001 ca.pem #ib_16384_0.dblwr ib_logfile0 mysql performance_schema server-key.pem undo_002
  7. binlog.000002 client-cert.pem #ib_16384_1.dblwr ib_logfile1 mysql.ibd private_key.pem sys
  8. [root@riyimei mysql]# cd mysql/
  9. [root@riyimei mysql]# ls
  10. general_log_207.sdi general_log.CSM general_log.CSV slow_log_208.sdi slow_log.CSM slow_log.CSV
  11. [root@riyimei mysql]# cd /var/run/mysqld/
  12. [root@riyimei mysqld]# ls
  13. mysqld.pid mysqlx.sock mysqlx.sock.lock
  14. [root@riyimei mysqld]#
  1. [root@riyimei ~]# netstat -lntup
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  4. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1061/sshd
  5. tcp6 0 0 :::33060 :::* LISTEN 1091/mysqld
  6. tcp6 0 0 :::3306 :::* LISTEN 1091/mysqld
  7. tcp6 0 0 :::22 :::* LISTEN 1061/sshd
  8. [root@riyimei ~]#
  9. [root@riyimei ~]# cat /etc/my.cnf
  10. # For advice on how to change settings please see
  11. # http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
  12. [mysqld]
  13. #
  14. # Remove leading # and set to the amount of RAM for the most important data
  15. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
  16. # innodb_buffer_pool_size = 128M
  17. #
  18. # Remove the leading "# " to disable binary logging
  19. # Binary logging captures changes between backups and is enabled by
  20. # default. It's default setting is log_bin=binlog
  21. # disable_log_bin
  22. #
  23. # Remove leading # to set options mainly useful for reporting servers.
  24. # The server defaults are faster for transactions and fast SELECTs.
  25. # Adjust sizes as needed, experiment to find the optimal values.
  26. # join_buffer_size = 128M
  27. # sort_buffer_size = 2M
  28. # read_rnd_buffer_size = 2M
  29. #
  30. # Remove leading # to revert to previous value for default_authentication_plugin,
  31. # this will increase compatibility with older clients. For background, see:
  32. # https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
  33. # default-authentication-plugin=mysql_native_password
  34. datadir=/var/lib/mysql
  35. socket=/var/lib/mysql/mysql.sock
  36. log-error=/var/log/mysqld.log
  37. pid-file=/var/run/mysqld/mysqld.pid
  38. [root@riyimei ~]#

yum源

https://dev.mysql.com/downloads/repo/yum/

image.png

wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm

  1. [root@n9e tmp]# ll
  2. total 28
  3. -rw-r--r-- 1 root root 26024 Feb 19 16:36 mysql80-community-release-el7-3.noarch.rpm
  4. [root@n9e tmp]# rpm -ivh mysql80-community-release-el7-3.noarch.rpm
  5. warning: mysql80-community-release-el7-3.noarch.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
  6. Preparing... ################################# [100%]
  7. Updating / installing...
  8. 1:mysql80-community-release-el7-3 ################################# [100%]
  9. [root@n9e tmp]#
  10. [root@n9e tmp]# cat /etc/yum.repos.d/m
  11. mariadb.repo.bak mariadb_repo_setup mysql-community.repo mysql-community-source.repo
  12. [root@n9e tmp]# cat /etc/yum.repos.d/mysql-community.repo
  13. # Enable to use MySQL 5.5
  14. [mysql55-community]
  15. name=MySQL 5.5 Community Server
  16. baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/
  17. enabled=0
  18. gpgcheck=1
  19. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  20. # Enable to use MySQL 5.6
  21. [mysql56-community]
  22. name=MySQL 5.6 Community Server
  23. baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/7/$basearch/
  24. enabled=0
  25. gpgcheck=1
  26. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  27. # Enable to use MySQL 5.7
  28. [mysql57-community]
  29. name=MySQL 5.7 Community Server
  30. baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
  31. enabled=0
  32. gpgcheck=1
  33. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  34. [mysql80-community]
  35. name=MySQL 8.0 Community Server
  36. baseurl=http://repo.mysql.com/yum/mysql-8.0-community/el/7/$basearch/
  37. enabled=1
  38. gpgcheck=1
  39. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  40. [mysql-connectors-community]
  41. name=MySQL Connectors Community
  42. baseurl=http://repo.mysql.com/yum/mysql-connectors-community/el/7/$basearch/
  43. enabled=1
  44. gpgcheck=1
  45. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  46. [mysql-tools-community]
  47. name=MySQL Tools Community
  48. baseurl=http://repo.mysql.com/yum/mysql-tools-community/el/7/$basearch/
  49. enabled=1
  50. gpgcheck=1
  51. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  52. [mysql-tools-preview]
  53. name=MySQL Tools Preview
  54. baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/$basearch/
  55. enabled=0
  56. gpgcheck=1
  57. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  58. [mysql-cluster-7.5-community]
  59. name=MySQL Cluster 7.5 Community
  60. baseurl=http://repo.mysql.com/yum/mysql-cluster-7.5-community/el/7/$basearch/
  61. enabled=0
  62. gpgcheck=1
  63. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  64. [mysql-cluster-7.6-community]
  65. name=MySQL Cluster 7.6 Community
  66. baseurl=http://repo.mysql.com/yum/mysql-cluster-7.6-community/el/7/$basearch/
  67. enabled=0
  68. gpgcheck=1
  69. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  70. [mysql-cluster-8.0-community]
  71. name=MySQL Cluster 8.0 Community
  72. baseurl=http://repo.mysql.com/yum/mysql-cluster-8.0-community/el/7/$basearch/
  73. enabled=0
  74. gpgcheck=1
  75. gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  76. [root@n9e tmp]#
  1. yum install mysql-community-client mysql-community-server
  1. [root@n9e ~]# yum repolist
  2. Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager
  3. This system is not registered with an entitlement server. You can use subscription-manager to register.
  4. Loading mirror speeds from cached hostfile
  5. repo id repo name status
  6. mysql-connectors-community/x86_64 MySQL Connectors Community 185
  7. mysql-tools-community/x86_64 MySQL Tools Community 123
  8. mysql80-community/x86_64 MySQL 8.0 Community Server 229
  9. repolist: 537
  10. [root@n9e ~]# yum install mysql-community-client mysql-community-server
  11. Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager
  12. This system is not registered with an entitlement server. You can use subscription-manager to register.
  13. Loading mirror speeds from cached hostfile
  14. Resolving Dependencies
  15. --> Running transaction check
  16. ---> Package mysql-community-client.x86_64 0:8.0.23-1.el7 will be installed
  17. --> Processing Dependency: mysql-community-client-plugins = 8.0.23-1.el7 for package: mysql-community-client-8.0.23-1.el7.x86_64
  18. --> Processing Dependency: mysql-community-libs(x86-64) >= 8.0.11 for package: mysql-community-client-8.0.23-1.el7.x86_64
  19. ---> Package mysql-community-server.x86_64 0:8.0.23-1.el7 will be installed
  20. --> Processing Dependency: mysql-community-common(x86-64) = 8.0.23-1.el7 for package: mysql-community-server-8.0.23-1.el7.x86_64
  21. --> Running transaction check
  22. ---> Package mysql-community-client-plugins.x86_64 0:8.0.23-1.el7 will be installed
  23. ---> Package mysql-community-common.x86_64 0:8.0.23-1.el7 will be installed
  24. ---> Package mysql-community-libs.x86_64 0:8.0.23-1.el7 will be installed
  25. --> Finished Dependency Resolution
  26. Dependencies Resolved
  27. ======================================================================================================================================================
  28. Package Arch Version Repository Size
  29. ======================================================================================================================================================
  30. Installing:
  31. mysql-community-client x86_64 8.0.23-1.el7 mysql80-community 48 M
  32. mysql-community-server x86_64 8.0.23-1.el7 mysql80-community 518 M
  33. Installing for dependencies:
  34. mysql-community-client-plugins x86_64 8.0.23-1.el7 mysql80-community 237 k
  35. mysql-community-common x86_64 8.0.23-1.el7 mysql80-community 621 k
  36. mysql-community-libs x86_64 8.0.23-1.el7 mysql80-community 4.6 M
  37. Transaction Summary
  38. ======================================================================================================================================================
  39. Install 2 Packages (+3 Dependent packages)
  40. Total download size: 572 M
  41. Installed size: 2.6 G
  42. Is this ok [y/d/N]:
  1. [root@n9e ~]# rm -rf /var/lib/mysql/*
  2. [root@n9e ~]#
  3. [root@n9e ~]# systemctl enable --now mysqld.service
  4. [root@n9e ~]# systemctl status mysqld.service
  5. mysqld.service - MySQL Server
  6. Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
  7. Active: active (running) since Fri 2021-02-19 16:57:25 CST; 6s ago
  8. Docs: man:mysqld(8)
  9. http://dev.mysql.com/doc/refman/en/using-systemd.html
  10. Process: 7856 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
  11. Main PID: 7932 (mysqld)
  12. Status: "Server is operational"
  13. CGroup: /system.slice/mysqld.service
  14. └─7932 /usr/sbin/mysqld
  15. Feb 19 16:57:21 n9e systemd[1]: Starting MySQL Server...
  16. Feb 19 16:57:25 n9e systemd[1]: Started MySQL Server.
  17. [root@n9e ~]#
  1. [root@n9e ~]# grep 'temporary password' /var/log/mysqld.log
  2. 2021-02-19T08:57:23.093478Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: wIgiWtyj-4)v
  3. [root@n9e ~]# mysql -uroot -p
  4. Enter password:
  5. Welcome to the MySQL monitor. Commands end with ; or \g.
  6. Your MySQL connection id is 9
  7. Server version: 8.0.23
  8. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  13. mysql>
  1. mysql> alter user 'root'@'localhost' identified by "!Q2w3e4r";
  2. Query OK, 0 rows affected (0.03 sec)
  3. mysql> exit
  4. Bye
  5. [root@n9e ~]# mysql -uroot -p
  6. Enter password:
  7. Welcome to the MySQL monitor. Commands end with ; or \g.
  8. Your MySQL connection id is 10
  9. Server version: 8.0.23 MySQL Community Server - GPL
  10. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  11. Oracle is a registered trademark of Oracle Corporation and/or its
  12. affiliates. Other names may be trademarks of their respective
  13. owners.
  14. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  15. mysql>

下载历史版本

https://downloads.mysql.com/archives/cluster/

经典版本 MySQL5.5 MySQL5.6 MySQL5.7 读写性能对比

Mysql 8.0部署和操作 - 图3

Mysql 8.0部署和操作 - 图4


  1. [root@riyimei tmp]# systemctl restart mysqld.service
  2. [root@riyimei tmp]#
  3. [root@riyimei tmp]#
  4. [root@riyimei tmp]#
  5. [root@riyimei tmp]# systemctl status mysqld.service
  6. mysqld.service - MySQL Server
  7. Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
  8. Active: active (running) since Sat 2020-07-25 12:16:13 CST; 9s ago
  9. Docs: man:mysqld(8)
  10. http://dev.mysql.com/doc/refman/en/using-systemd.html
  11. Process: 2312 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
  12. Main PID: 2387 (mysqld)
  13. Status: "Server is operational"
  14. CGroup: /system.slice/mysqld.service
  15. └─2387 /usr/sbin/mysqld
  16. Jul 25 12:16:09 riyimei systemd[1]: Starting MySQL Server...
  17. Jul 25 12:16:13 riyimei systemd[1]: Started MySQL Server.
  18. [root@riyimei tmp]# netstat -lntup
  19. Active Internet connections (only servers)
  20. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  21. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2157/sshd
  22. tcp6 0 0 :::33060 :::* LISTEN 2387/mysqld
  23. tcp6 0 0 :::3306 :::* LISTEN 2387/mysqld
  24. tcp6 0 0 :::22 :::* LISTEN 2157/sshd
  25. [root@riyimei tmp]#

查看安装默认密码

  1. [root@riyimei tmp]# grep 'temporary password' /var/log/mysqld.log
  2. 2020-07-25T04:16:10.579453Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ;<6%b1N5.tvm
  3. [root@riyimei tmp]#
  1. [root@riyimei tmp]# grep 'temporary password' /var/log/mysqld.log
  2. 2020-07-25T04:16:10.579453Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ;<6%b1N5.tvm
  3. [root@riyimei tmp]#
  4. [root@riyimei tmp]# mysql -uroot -p
  5. Enter password:
  6. Welcome to the MySQL monitor. Commands end with ; or \g.
  7. Your MySQL connection id is 9
  8. Server version: 8.0.21
  9. Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  10. Oracle is a registered trademark of Oracle Corporation and/or its
  11. affiliates. Other names may be trademarks of their respective
  12. owners.
  13. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  14. mysql>

修改密码 alter user ‘root’@’localhost’ identified by “!Q2w3e4r”;

数据库密码策略

  1. mysql> show variables like 'validate_password%';
  2. +--------------------------------------+--------+
  3. | Variable_name | Value |
  4. +--------------------------------------+--------+
  5. | validate_password.check_user_name | ON |
  6. | validate_password.dictionary_file | |
  7. | validate_password.length | 8 |
  8. | validate_password.mixed_case_count | 1 |
  9. | validate_password.number_count | 1 |
  10. | validate_password.policy | MEDIUM |
  11. | validate_password.special_char_count | 1 |
  12. +--------------------------------------+--------+
  13. 7 rows in set (0.00 sec)
  14. mysql>
  1. [root@riyimei tmp]# grep 'temporary password' /var/log/mysqld.log
  2. 2020-07-25T04:16:10.579453Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ;<6%b1N5.tvm
  3. [root@riyimei tmp]# mysql -uroot -p
  4. Enter password:
  5. Welcome to the MySQL monitor. Commands end with ; or \g.
  6. Your MySQL connection id is 12
  7. Server version: 8.0.21
  8. Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  13. mysql> alter user 'root'@'localhost' identified by "!Q2w3e4r";
  14. Query OK, 0 rows affected (0.00 sec)
  15. mysql>

跳过密码

skip-grant-tables

  1. [root@n9e ~]# vim /etc/my.cnf
  2. [root@n9e ~]#
  3. [root@n9e ~]# systemctl restart mysqld.service
  4. [root@n9e ~]# mysql -uroot
  5. Welcome to the MySQL monitor. Commands end with ; or \g.
  6. Your MySQL connection id is 7
  7. Server version: 8.0.23 MySQL Community Server - GPL
  8. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  9. Oracle is a registered trademark of Oracle Corporation and/or its
  10. affiliates. Other names may be trademarks of their respective
  11. owners.
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  13. mysql> exit
  14. Bye
  15. [root@n9e ~]# cat /etc/my.cnf
  16. # For advice on how to change settings please see
  17. # http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
  18. [mysqld]
  19. #
  20. # Remove leading # and set to the amount of RAM for the most important data
  21. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
  22. # innodb_buffer_pool_size = 128M
  23. #
  24. # Remove the leading "# " to disable binary logging
  25. # Binary logging captures changes between backups and is enabled by
  26. # default. It's default setting is log_bin=binlog
  27. # disable_log_bin
  28. #
  29. # Remove leading # to set options mainly useful for reporting servers.
  30. # The server defaults are faster for transactions and fast SELECTs.
  31. # Adjust sizes as needed, experiment to find the optimal values.
  32. # join_buffer_size = 128M
  33. # sort_buffer_size = 2M
  34. # read_rnd_buffer_size = 2M
  35. #
  36. # Remove leading # to revert to previous value for default_authentication_plugin,
  37. # this will increase compatibility with older clients. For background, see:
  38. # https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
  39. # default-authentication-plugin=mysql_native_password
  40. datadir=/var/lib/mysql
  41. socket=/var/lib/mysql/mysql.sock
  42. log-error=/var/log/mysqld.log
  43. pid-file=/var/run/mysqld/mysqld.pid
  44. skip-grant-tables
  45. [root@n9e ~]#

重置密码

skip-grant-tables

use mysql
select host, user, authentication_string, plugin from user;
update user set authentication_string=’’ where user=’root’;

sed -i ‘$d’ /etc/my.cnf
systemctl restart mysqld.service
alter user ‘root’@’localhost’ identified by “!Q2w3e4r”;

  1. [root@n9e ~]# cat /etc/my.cnf
  2. # For advice on how to change settings please see
  3. # http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
  4. [mysqld]
  5. #
  6. # Remove leading # and set to the amount of RAM for the most important data
  7. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
  8. # innodb_buffer_pool_size = 128M
  9. #
  10. # Remove the leading "# " to disable binary logging
  11. # Binary logging captures changes between backups and is enabled by
  12. # default. It's default setting is log_bin=binlog
  13. # disable_log_bin
  14. #
  15. # Remove leading # to set options mainly useful for reporting servers.
  16. # The server defaults are faster for transactions and fast SELECTs.
  17. # Adjust sizes as needed, experiment to find the optimal values.
  18. # join_buffer_size = 128M
  19. # sort_buffer_size = 2M
  20. # read_rnd_buffer_size = 2M
  21. #
  22. # Remove leading # to revert to previous value for default_authentication_plugin,
  23. # this will increase compatibility with older clients. For background, see:
  24. # https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
  25. # default-authentication-plugin=mysql_native_password
  26. datadir=/var/lib/mysql
  27. socket=/var/lib/mysql/mysql.sock
  28. log-error=/var/log/mysqld.log
  29. pid-file=/var/run/mysqld/mysqld.pid
  30. skip-grant-tables
  31. [root@n9e ~]#
  32. [root@n9e ~]# mysql -uroot
  33. Welcome to the MySQL monitor. Commands end with ; or \g.
  34. Your MySQL connection id is 8
  35. Server version: 8.0.23 MySQL Community Server - GPL
  36. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  37. Oracle is a registered trademark of Oracle Corporation and/or its
  38. affiliates. Other names may be trademarks of their respective
  39. owners.
  40. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  41. mysql> use mysql
  42. Reading table information for completion of table and column names
  43. You can turn off this feature to get a quicker startup with -A
  44. Database changed
  45. mysql> select host, user, authentication_string, plugin from user;
  46. +-----------+------------------+------------------------------------------------------------------------+-----------------------+
  47. | host | user | authentication_string | plugin |
  48. +-----------+------------------+------------------------------------------------------------------------+-----------------------+
  49. | localhost | mysql.infoschema | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
  50. | localhost | mysql.session | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
  51. | localhost | mysql.sys | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | caching_sha2_password |
  52. | localhost | root | $A$005$<5l!vq1Ov+[vt!OIfCFAoW.4ILpThA7TiD44PQEAbJ3GPuAq82TR4eBroC | caching_sha2_password |
  53. +-----------+------------------+------------------------------------------------------------------------+-----------------------+
  54. 4 rows in set (0.00 sec)
  55. mysql> update user set authentication_string='' where user='root';
  56. Query OK, 1 row affected (0.00 sec)
  57. Rows matched: 1 Changed: 1 Warnings: 0
  58. mysql> exit
  59. Bye
  60. [root@n9e ~]# sed -i '$d' /etc/my.cnf
  61. [root@n9e ~]# systemctl restart mysqld.service
  62. [root@n9e ~]# mysql -uroot
  63. Welcome to the MySQL monitor. Commands end with ; or \g.
  64. Your MySQL connection id is 8
  65. Server version: 8.0.23 MySQL Community Server - GPL
  66. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  67. Oracle is a registered trademark of Oracle Corporation and/or its
  68. affiliates. Other names may be trademarks of their respective
  69. owners.
  70. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  71. mysql> alter user 'root'@'localhost' identified by "!Q2w3e4r";
  72. Query OK, 0 rows affected (0.01 sec)
  73. mysql> exit
  74. Bye
  75. [root@n9e ~]# mysql -uroot -p
  76. Enter password:
  77. Welcome to the MySQL monitor. Commands end with ; or \g.
  78. Your MySQL connection id is 9
  79. Server version: 8.0.23 MySQL Community Server - GPL
  80. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  81. Oracle is a registered trademark of Oracle Corporation and/or its
  82. affiliates. Other names may be trademarks of their respective
  83. owners.
  84. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  85. mysql> exit
  86. Bye
  87. [root@n9e ~]#

安全初始化

mysql_secure_installation

  1. [root@riyimei tmp]# mysql_secure_installation
  2. Securing the MySQL server deployment.
  3. Enter password for user root:
  4. The 'validate_password' component is installed on the server.
  5. The subsequent steps will run with the existing configuration
  6. of the component.
  7. Using existing password for root.
  8. Estimated strength of the password: 100
  9. Change the password for root ? ((Press y|Y for Yes, any other key for No) :
  10. ... skipping.
  11. By default, a MySQL installation has an anonymous user,
  12. allowing anyone to log into MySQL without having to have
  13. a user account created for them. This is intended only for
  14. testing, and to make the installation go a bit smoother.
  15. You should remove them before moving into a production
  16. environment.
  17. Remove anonymous users? (Press y|Y for Yes, any other key for No) :
  18. ... skipping.
  19. Normally, root should only be allowed to connect from
  20. 'localhost'. This ensures that someone cannot guess at
  21. the root password from the network.
  22. Disallow root login remotely? (Press y|Y for Yes, any other key for No) :
  23. ... skipping.
  24. By default, MySQL comes with a database named 'test' that
  25. anyone can access. This is also intended only for testing,
  26. and should be removed before moving into a production
  27. environment.
  28. Remove test database and access to it? (Press y|Y for Yes, any other key for No) :
  29. ... skipping.
  30. Reloading the privilege tables will ensure that all changes
  31. made so far will take effect immediately.
  32. Reload privilege tables now? (Press y|Y for Yes, any other key for No) :
  33. ... skipping.
  34. All done!
  35. [root@riyimei tmp]#

数据类型

常见数据类型
字符串类型(CHAR(0-255固定长度),VARCHAR(0-255 可变长度) )
数值类型(INT (整数型)、FLOAT(浮点型))
日期和时间类型(DATA (年月日) 、TIME(时分秒))

8.0.1 7版本开始,TINYINT, SMALLINT, MEDIUMINT, INT, and BIGINT类型的显示宽度将失效

MySQL数据类型.pdf

约束类型

主键约束 primary key

外键约束 foreign key

唯一约束 unique

非空约束 not null 与 默认值 default

MySQL索引

MySQL锁

MySQL存储引擎

MYSAM
INNODB
MEMORY

数据库系统
表示层:数据库-数据表-记录(字段)
逻辑层:数据库的存储引擎
物理层:数据库文件(*.sql)

文件系统
表示层:文件名(文件类型 .txt…)
逻辑层:文件系统类型(ext4、NFS)
物理层:分区块(数据块 data block) 扇区

事务

SQL 分类 3个类别

DDL 语句
DML 语句
DCL 语句

DDL 语句:数据定义语言
定义了不同的数据段、数据库、表、列、索引等数据库对象(常用语句关键字:create、drop、alter)

DML 语句:数据操作语句
用于 添加、删除、更新和查询数据库记录、检查数据完整性(常用语句关键字:insert、delete、update、select)

DCL 语句:数据控制语句
定义了数据库、表、字段、用户的访问权限和安全级别(常用语句关键字:grant、revoke)

DDL 语句

MySQL 常用命令

创建数据库:test1
CREATE DATABASE test1;
查看数据库
SHOW DATABASES;
选择数据库
USE mysql;
查看数据的表
SHOW TABLES;
删除数据库
DROP DATABASE test1;

ADD\CHANGE\MODIFY

  1. mysql> CREATE DATABASE test1;
  2. Query OK, 1 row affected (0.01 sec)
  3. mysql> SHOW DATABASES;
  4. +--------------------+
  5. | Database |
  6. +--------------------+
  7. | information_schema |
  8. | mysql |
  9. | performance_schema |
  10. | sys |
  11. | test1 |
  12. +--------------------+
  13. 5 rows in set (0.01 sec)
  14. mysql>

系统库:

information_schema 用户表信息 mysql 存储系统用户权限信息 performance_schema 存储系统性能参数 sys 系统视图

  1. mysql>
  2. mysql> USE mysql;
  3. Reading table information for completion of table and column names
  4. You can turn off this feature to get a quicker startup with -A
  5. Database changed
  6. mysql> SHOW TABLES;
  7. +---------------------------+
  8. | Tables_in_mysql |
  9. +---------------------------+
  10. | columns_priv |
  11. | component |
  12. | db |
  13. | default_roles |
  14. | engine_cost |
  15. | func |
  16. | general_log |
  17. | global_grants |
  18. | gtid_executed |
  19. | help_category |
  20. | help_keyword |
  21. | help_relation |
  22. | help_topic |
  23. | innodb_index_stats |
  24. | innodb_table_stats |
  25. | password_history |
  26. | plugin |
  27. | procs_priv |
  28. | proxies_priv |
  29. | role_edges |
  30. | server_cost |
  31. | servers |
  32. | slave_master_info |
  33. | slave_relay_log_info |
  34. | slave_worker_info |
  35. | slow_log |
  36. | tables_priv |
  37. | time_zone |
  38. | time_zone_leap_second |
  39. | time_zone_name |
  40. | time_zone_transition |
  41. | time_zone_transition_type |
  42. | user |
  43. +---------------------------+
  44. 33 rows in set (0.00 sec)
  45. mysql>

删除数据库

  1. mysql> DROP DATABASE test1;
  2. Query OK, 0 rows affected (0.01 sec)
  3. mysql> SHOW DATABASES;
  4. +--------------------+
  5. | Database |
  6. +--------------------+
  7. | information_schema |
  8. | mysql |
  9. | performance_schema |
  10. | sys |
  11. +--------------------+
  12. 4 rows in set (0.00 sec)
  13. mysql>

创建表emp

  1. mysql> use test1;
  2. Database changed
  3. mysql>
  4. mysql>
  5. mysql> show tables;
  6. Empty set (0.00 sec)
  7. mysql>
  8. mysql> create table emp(ename varchar(10),hiredate date,sal decimal(10,2),deptno int(2));
  9. Query OK, 0 rows affected, 1 warning (0.09 sec)
  10. mysql> show tables;
  11. +-----------------+
  12. | Tables_in_test1 |
  13. +-----------------+
  14. | emp |
  15. +-----------------+
  16. 1 row in set (0.00 sec)
  17. mysql> desc emp;
  18. +----------+---------------+------+-----+---------+-------+
  19. | Field | Type | Null | Key | Default | Extra |
  20. +----------+---------------+------+-----+---------+-------+
  21. | ename | varchar(10) | YES | | NULL | |
  22. | hiredate | date | YES | | NULL | |
  23. | sal | decimal(10,2) | YES | | NULL | |
  24. | deptno | int | YES | | NULL | |
  25. +----------+---------------+------+-----+---------+-------+
  26. 4 rows in set (0.00 sec)
  27. mysql>
  1. mysql> create table emp(ename varchar(10),hiredate date,sal decimal(10,2),deptno int(2));
  2. Query OK, 0 rows affected, 1 warning (0.01 sec)
  3. mysql> show tables;
  4. +-----------------+
  5. | Tables_in_test1 |
  6. +-----------------+
  7. | emp |
  8. +-----------------+
  9. 1 row in set (0.00 sec)
  10. mysql> desc emp;
  11. +----------+---------------+------+-----+---------+-------+
  12. | Field | Type | Null | Key | Default | Extra |
  13. +----------+---------------+------+-----+---------+-------+
  14. | ename | varchar(10) | YES | | NULL | |
  15. | hiredate | date | YES | | NULL | |
  16. | sal | decimal(10,2) | YES | | NULL | |
  17. | deptno | int | YES | | NULL | |
  18. +----------+---------------+------+-----+---------+-------+
  19. 4 rows in set (0.00 sec)
  20. mysql> show create table emp \G;
  21. *************************** 1. row ***************************
  22. Table: emp
  23. Create Table: CREATE TABLE `emp` (
  24. `ename` varchar(10) DEFAULT NULL,
  25. `hiredate` date DEFAULT NULL,
  26. `sal` decimal(10,2) DEFAULT NULL,
  27. `deptno` int DEFAULT NULL
  28. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
  29. 1 row in set (0.02 sec)
  30. ERROR:
  31. No query specified
  32. mysql>

修改表字段

  1. mysql> desc emp;
  2. +----------+---------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +----------+---------------+------+-----+---------+-------+
  5. | ename | varchar(10) | YES | | NULL | |
  6. | hiredate | date | YES | | NULL | |
  7. | sal | decimal(10,2) | YES | | NULL | |
  8. | deptno | int | YES | | NULL | |
  9. +----------+---------------+------+-----+---------+-------+
  10. 4 rows in set (0.00 sec)
  11. mysql> alter table emp modify ename varchar(20);
  12. Query OK, 0 rows affected (0.02 sec)
  13. Records: 0 Duplicates: 0 Warnings: 0
  14. mysql> desc emp;
  15. +----------+---------------+------+-----+---------+-------+
  16. | Field | Type | Null | Key | Default | Extra |
  17. +----------+---------------+------+-----+---------+-------+
  18. | ename | varchar(20) | YES | | NULL | |
  19. | hiredate | date | YES | | NULL | |
  20. | sal | decimal(10,2) | YES | | NULL | |
  21. | deptno | int | YES | | NULL | |
  22. +----------+---------------+------+-----+---------+-------+
  23. 4 rows in set (0.00 sec)
  24. mysql>

增加表字段

  1. mysql> alter table emp add column age int(3);
  2. Query OK, 0 rows affected, 1 warning (0.02 sec)
  3. Records: 0 Duplicates: 0 Warnings: 1
  4. mysql> desc emp;
  5. +----------+---------------+------+-----+---------+-------+
  6. | Field | Type | Null | Key | Default | Extra |
  7. +----------+---------------+------+-----+---------+-------+
  8. | ename | varchar(20) | YES | | NULL | |
  9. | hiredate | date | YES | | NULL | |
  10. | sal | decimal(10,2) | YES | | NULL | |
  11. | deptno | int | YES | | NULL | |
  12. | age | int | YES | | NULL | |
  13. +----------+---------------+------+-----+---------+-------+
  14. 5 rows in set (0.00 sec)
  15. mysql>

字段改名

  1. mysql> desc emp;
  2. +----------+---------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +----------+---------------+------+-----+---------+-------+
  5. | ename | varchar(20) | YES | | NULL | |
  6. | hiredate | date | YES | | NULL | |
  7. | sal | decimal(10,2) | YES | | NULL | |
  8. | deptno | int | YES | | NULL | |
  9. | age | int | YES | | NULL | |
  10. +----------+---------------+------+-----+---------+-------+
  11. 5 rows in set (0.00 sec)
  12. mysql> alter table emp change age age1 int;
  13. Query OK, 0 rows affected (0.01 sec)
  14. Records: 0 Duplicates: 0 Warnings: 0
  15. mysql> desc emp;
  16. +----------+---------------+------+-----+---------+-------+
  17. | Field | Type | Null | Key | Default | Extra |
  18. +----------+---------------+------+-----+---------+-------+
  19. | ename | varchar(20) | YES | | NULL | |
  20. | hiredate | date | YES | | NULL | |
  21. | sal | decimal(10,2) | YES | | NULL | |
  22. | deptno | int | YES | | NULL | |
  23. | age1 | int | YES | | NULL | |
  24. +----------+---------------+------+-----+---------+-------+
  25. 5 rows in set (0.00 sec)
  26. mysql>

change和modify 都可以修改表的定义
change需要写两次列名 change可以修改列的名称、modify不可以

删除表字段

  1. mysql> desc emp;
  2. +----------+---------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +----------+---------------+------+-----+---------+-------+
  5. | ename | varchar(20) | YES | | NULL | |
  6. | hiredate | date | YES | | NULL | |
  7. | sal | decimal(10,2) | YES | | NULL | |
  8. | deptno | int | YES | | NULL | |
  9. | age | int | YES | | NULL | |
  10. +----------+---------------+------+-----+---------+-------+
  11. 5 rows in set (0.00 sec)
  12. mysql> alter table emp drop column age;
  13. Query OK, 0 rows affected (0.01 sec)
  14. Records: 0 Duplicates: 0 Warnings: 0
  15. mysql> desc emp;
  16. +----------+---------------+------+-----+---------+-------+
  17. | Field | Type | Null | Key | Default | Extra |
  18. +----------+---------------+------+-----+---------+-------+
  19. | ename | varchar(20) | YES | | NULL | |
  20. | hiredate | date | YES | | NULL | |
  21. | sal | decimal(10,2) | YES | | NULL | |
  22. | deptno | int | YES | | NULL | |
  23. +----------+---------------+------+-----+---------+-------+
  24. 4 rows in set (0.00 sec)
  25. mysql>

调整字段位置

选项 first|after column_name
ADD 增加的新字段默认加在表的最后位置,CHANGE/MODIFY默认都不会修改字段位置

  1. mysql> desc emp;
  2. +----------+---------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +----------+---------------+------+-----+---------+-------+
  5. | ename | varchar(20) | YES | | NULL | |
  6. | hiredate | date | YES | | NULL | |
  7. | sal | decimal(10,2) | YES | | NULL | |
  8. | deptno | int | YES | | NULL | |
  9. | age1 | int | YES | | NULL | |
  10. +----------+---------------+------+-----+---------+-------+
  11. 5 rows in set (0.00 sec)
  12. mysql> alter table emp add birth date after ename;
  13. Query OK, 0 rows affected (0.02 sec)
  14. Records: 0 Duplicates: 0 Warnings: 0
  15. mysql> desc emp;
  16. +----------+---------------+------+-----+---------+-------+
  17. | Field | Type | Null | Key | Default | Extra |
  18. +----------+---------------+------+-----+---------+-------+
  19. | ename | varchar(20) | YES | | NULL | |
  20. | birth | date | YES | | NULL | |
  21. | hiredate | date | YES | | NULL | |
  22. | sal | decimal(10,2) | YES | | NULL | |
  23. | deptno | int | YES | | NULL | |
  24. | age1 | int | YES | | NULL | |
  25. +----------+---------------+------+-----+---------+-------+
  26. 6 rows in set (0.01 sec)
  27. mysql>
  1. mysql> desc emp;
  2. +----------+---------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +----------+---------------+------+-----+---------+-------+
  5. | ename | varchar(20) | YES | | NULL | |
  6. | birth | date | YES | | NULL | |
  7. | hiredate | date | YES | | NULL | |
  8. | sal | decimal(10,2) | YES | | NULL | |
  9. | deptno | int | YES | | NULL | |
  10. | age | int | YES | | NULL | |
  11. +----------+---------------+------+-----+---------+-------+
  12. 6 rows in set (0.00 sec)
  13. mysql> alter table emp modify age int first;
  14. Query OK, 0 rows affected (0.01 sec)
  15. Records: 0 Duplicates: 0 Warnings: 0
  16. mysql> desc emp;
  17. +----------+---------------+------+-----+---------+-------+
  18. | Field | Type | Null | Key | Default | Extra |
  19. +----------+---------------+------+-----+---------+-------+
  20. | age | int | YES | | NULL | |
  21. | ename | varchar(20) | YES | | NULL | |
  22. | birth | date | YES | | NULL | |
  23. | hiredate | date | YES | | NULL | |
  24. | sal | decimal(10,2) | YES | | NULL | |
  25. | deptno | int | YES | | NULL | |
  26. +----------+---------------+------+-----+---------+-------+
  27. 6 rows in set (0.00 sec)
  28. mysql>

更改表名

  1. mysql> desc emp;
  2. +----------+---------------+------+-----+---------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +----------+---------------+------+-----+---------+-------+
  5. | age | int | YES | | NULL | |
  6. | ename | varchar(20) | YES | | NULL | |
  7. | birth | date | YES | | NULL | |
  8. | hiredate | date | YES | | NULL | |
  9. | sal | decimal(10,2) | YES | | NULL | |
  10. | deptno | int | YES | | NULL | |
  11. +----------+---------------+------+-----+---------+-------+
  12. 6 rows in set (0.00 sec)
  13. mysql> alter table emp rename emp1;
  14. Query OK, 0 rows affected (0.01 sec)
  15. mysql> desc emp;
  16. ERROR 1146 (42S02): Table 'test1.emp' doesn't exist
  17. mysql> desc emp1;
  18. +----------+---------------+------+-----+---------+-------+
  19. | Field | Type | Null | Key | Default | Extra |
  20. +----------+---------------+------+-----+---------+-------+
  21. | age | int | YES | | NULL | |
  22. | ename | varchar(20) | YES | | NULL | |
  23. | birth | date | YES | | NULL | |
  24. | hiredate | date | YES | | NULL | |
  25. | sal | decimal(10,2) | YES | | NULL | |
  26. | deptno | int | YES | | NULL | |
  27. +----------+---------------+------+-----+---------+-------+
  28. 6 rows in set (0.01 sec)
  29. mysql>

删除表emp1

  1. mysql> show tables;
  2. +-----------------+
  3. | Tables_in_test1 |
  4. +-----------------+
  5. | emp1 |
  6. +-----------------+
  7. 1 row in set (0.00 sec)
  8. mysql> drop table emp1;
  9. Query OK, 0 rows affected (0.01 sec)
  10. mysql> show tables;
  11. Empty set (0.00 sec)
  12. mysql>

查看在哪个库

select database();

  1. mysql> select database();
  2. +------------+
  3. | database() |
  4. +------------+
  5. | test1 |
  6. +------------+
  7. 1 row in set (0.00 sec)
  8. mysql>

创建用户

  1. mysql> use mysql;
  2. Database changed
  3. mysql> select host, user from user;
  4. +-----------+------------------+
  5. | host | user |
  6. +-----------+------------------+
  7. | % | root |
  8. | localhost | mysql.infoschema |
  9. | localhost | mysql.session |
  10. | localhost | mysql.sys |
  11. +-----------+------------------+
  12. 4 rows in set (0.00 sec)
  13. mysql> create user 'liwm'@'localhost' identified by '!Q2w3e4r';
  14. Query OK, 0 rows affected (0.00 sec)
  15. mysql> select host, user from user;
  16. +-----------+------------------+
  17. | host | user |
  18. +-----------+------------------+
  19. | % | root |
  20. | localhost | liwm |
  21. | localhost | mysql.infoschema |
  22. | localhost | mysql.session |
  23. | localhost | mysql.sys |
  24. +-----------+------------------+
  25. 5 rows in set (0.01 sec)
  26. mysql>

修改用户密码

  1. mysql> select host, user from user;
  2. +-----------+------------------+
  3. | host | user |
  4. +-----------+------------------+
  5. | % | root |
  6. | localhost | liwm |
  7. | localhost | mysql.infoschema |
  8. | localhost | mysql.session |
  9. | localhost | mysql.sys |
  10. +-----------+------------------+
  11. 9 rows in set (0.00 sec)
  12. mysql> alter user 'liwm'@'localhost' identified by '!Q2w3e4r';
  13. Query OK, 0 rows affected (0.00 sec)
  14. mysql> flush privileges;
  15. Query OK, 0 rows affected (0.01 sec)
  16. mysql>
  17. [root@riyimei mysqld]# mysql -uliwm -p
  18. Enter password:
  19. Welcome to the MySQL monitor. Commands end with ; or \g.
  20. Your MySQL connection id is 36
  21. Server version: 8.0.21 MySQL Community Server - GPL
  22. Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  23. Oracle is a registered trademark of Oracle Corporation and/or its
  24. affiliates. Other names may be trademarks of their respective
  25. owners.
  26. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  27. mysql> show databases;
  28. +--------------------+
  29. | Database |
  30. +--------------------+
  31. | information_schema |
  32. +--------------------+
  33. 1 row in set (0.00 sec)
  34. mysql>

查看用户权限

  1. mysql> SELECT user,host FROM mysql.user;
  2. +------------------+-----------+
  3. | user | host |
  4. +------------------+-----------+
  5. | root | % |
  6. | liwm | localhost |
  7. | mysql.infoschema | localhost |
  8. | mysql.session | localhost |
  9. | mysql.sys | localhost |
  10. +------------------+-----------+
  11. 5 rows in set (0.00 sec)
  12. mysql> SELECT * FROM mysql.user WHERE user='liwm'\G;
  13. *************************** 1. row ***************************
  14. Host: localhost
  15. User: liwm
  16. Select_priv: N
  17. Insert_priv: N
  18. Update_priv: N
  19. Delete_priv: N
  20. Create_priv: N
  21. Drop_priv: N
  22. Reload_priv: N
  23. Shutdown_priv: N
  24. Process_priv: N
  25. File_priv: N
  26. Grant_priv: N
  27. References_priv: N
  28. Index_priv: N
  29. Alter_priv: N
  30. Show_db_priv: N
  31. Super_priv: N
  32. Create_tmp_table_priv: N
  33. Lock_tables_priv: N
  34. Execute_priv: N
  35. Repl_slave_priv: N
  36. Repl_client_priv: N
  37. Create_view_priv: N
  38. Show_view_priv: N
  39. Create_routine_priv: N
  40. Alter_routine_priv: N
  41. Create_user_priv: N
  42. Event_priv: N
  43. Trigger_priv: N
  44. Create_tablespace_priv: N
  45. ssl_type:
  46. ssl_cipher: 0x
  47. x509_issuer: 0x
  48. x509_subject: 0x
  49. max_questions: 0
  50. max_updates: 0
  51. max_connections: 0
  52. max_user_connections: 0
  53. plugin: caching_sha2_password
  54. authentication_string: $A$005$YT%;JO ~5JxnA6nv=6e3/XsJh3e8692Jcwlu0hNG.NLgbUJM60SGdot3TBQA
  55. password_expired: N
  56. password_last_changed: 2020-07-26 18:30:35
  57. password_lifetime: NULL
  58. account_locked: N
  59. Create_role_priv: N
  60. Drop_role_priv: N
  61. Password_reuse_history: NULL
  62. Password_reuse_time: NULL
  63. Password_require_current: NULL
  64. User_attributes: NULL
  65. 1 row in set (0.00 sec)
  66. ERROR:
  67. No query specified
  68. mysql>

  1. mysql> desc user;
  2. +--------------------------+-----------------------------------+------+-----+-----------------------+-------+
  3. | Field | Type | Null | Key | Default | Extra |
  4. +--------------------------+-----------------------------------+------+-----+-----------------------+-------+
  5. | Host | char(255) | NO | PRI | | |
  6. | User | char(32) | NO | PRI | | |
  7. | Select_priv | enum('N','Y') | NO | | N | |
  8. | Insert_priv | enum('N','Y') | NO | | N | |
  9. | Update_priv | enum('N','Y') | NO | | N | |
  10. | Delete_priv | enum('N','Y') | NO | | N | |
  11. | Create_priv | enum('N','Y') | NO | | N | |
  12. | Drop_priv | enum('N','Y') | NO | | N | |
  13. | Reload_priv | enum('N','Y') | NO | | N | |
  14. | Shutdown_priv | enum('N','Y') | NO | | N | |
  15. | Process_priv | enum('N','Y') | NO | | N | |
  16. | File_priv | enum('N','Y') | NO | | N | |
  17. | Grant_priv | enum('N','Y') | NO | | N | |
  18. | References_priv | enum('N','Y') | NO | | N | |
  19. | Index_priv | enum('N','Y') | NO | | N | |
  20. | Alter_priv | enum('N','Y') | NO | | N | |
  21. | Show_db_priv | enum('N','Y') | NO | | N | |
  22. | Super_priv | enum('N','Y') | NO | | N | |
  23. | Create_tmp_table_priv | enum('N','Y') | NO | | N | |
  24. | Lock_tables_priv | enum('N','Y') | NO | | N | |
  25. | Execute_priv | enum('N','Y') | NO | | N | |
  26. | Repl_slave_priv | enum('N','Y') | NO | | N | |
  27. | Repl_client_priv | enum('N','Y') | NO | | N | |
  28. | Create_view_priv | enum('N','Y') | NO | | N | |
  29. | Show_view_priv | enum('N','Y') | NO | | N | |
  30. | Create_routine_priv | enum('N','Y') | NO | | N | |
  31. | Alter_routine_priv | enum('N','Y') | NO | | N | |
  32. | Create_user_priv | enum('N','Y') | NO | | N | |
  33. | Event_priv | enum('N','Y') | NO | | N | |
  34. | Trigger_priv | enum('N','Y') | NO | | N | |
  35. | Create_tablespace_priv | enum('N','Y') | NO | | N | |
  36. | ssl_type | enum('','ANY','X509','SPECIFIED') | NO | | | |
  37. | ssl_cipher | blob | NO | | NULL | |
  38. | x509_issuer | blob | NO | | NULL | |
  39. | x509_subject | blob | NO | | NULL | |
  40. | max_questions | int unsigned | NO | | 0 | |
  41. | max_updates | int unsigned | NO | | 0 | |
  42. | max_connections | int unsigned | NO | | 0 | |
  43. | max_user_connections | int unsigned | NO | | 0 | |
  44. | plugin | char(64) | NO | | caching_sha2_password | |
  45. | authentication_string | text | YES | | NULL | |
  46. | password_expired | enum('N','Y') | NO | | N | |
  47. | password_last_changed | timestamp | YES | | NULL | |
  48. | password_lifetime | smallint unsigned | YES | | NULL | |
  49. | account_locked | enum('N','Y') | NO | | N | |
  50. | Create_role_priv | enum('N','Y') | NO | | N | |
  51. | Drop_role_priv | enum('N','Y') | NO | | N | |
  52. | Password_reuse_history | smallint unsigned | YES | | NULL | |
  53. | Password_reuse_time | smallint unsigned | YES | | NULL | |
  54. | Password_require_current | enum('N','Y') | YES | | NULL | |
  55. | User_attributes | json | YES | | NULL | |
  56. +--------------------------+-----------------------------------+------+-----+-----------------------+-------+
  57. 51 rows in set (0.00 sec)

用户授权

  1. mysql> grant all on *.* to 'liwm'@'localhost';
  2. Query OK, 0 rows affected (0.02 sec)
  3. mysql> SELECT user,host FROM mysql.user;
  4. +------------------+-----------+
  5. | user | host |
  6. +------------------+-----------+
  7. | root | % |
  8. | liwm | localhost |
  9. | mysql.infoschema | localhost |
  10. | mysql.session | localhost |
  11. | mysql.sys | localhost |
  12. +------------------+-----------+
  13. 5 rows in set (0.00 sec)
  14. mysql> SELECT * FROM mysql.user WHERE user='liwm'\G;
  15. *************************** 1. row ***************************
  16. Host: localhost
  17. User: liwm
  18. Select_priv: Y
  19. Insert_priv: Y
  20. Update_priv: Y
  21. Delete_priv: Y
  22. Create_priv: Y
  23. Drop_priv: Y
  24. Reload_priv: Y
  25. Shutdown_priv: Y
  26. Process_priv: Y
  27. File_priv: Y
  28. Grant_priv: N
  29. References_priv: Y
  30. Index_priv: Y
  31. Alter_priv: Y
  32. Show_db_priv: Y
  33. Super_priv: Y
  34. Create_tmp_table_priv: Y
  35. Lock_tables_priv: Y
  36. Execute_priv: Y
  37. Repl_slave_priv: Y
  38. Repl_client_priv: Y
  39. Create_view_priv: Y
  40. Show_view_priv: Y
  41. Create_routine_priv: Y
  42. Alter_routine_priv: Y
  43. Create_user_priv: Y
  44. Event_priv: Y
  45. Trigger_priv: Y
  46. Create_tablespace_priv: Y
  47. ssl_type:
  48. ssl_cipher: 0x
  49. x509_issuer: 0x
  50. x509_subject: 0x
  51. max_questions: 0
  52. max_updates: 0
  53. max_connections: 0
  54. max_user_connections: 0
  55. plugin: caching_sha2_password
  56. authentication_string: $A$005$M#oi(
  57. xeeg 3fYqN8zHbU7.vv3EDBKqmY4pzKUN1dNYg4KN.j5k8dQ5
  58. password_expired: N
  59. password_last_changed: 2020-07-26 20:12:10
  60. password_lifetime: NULL
  61. account_locked: N
  62. Create_role_priv: Y
  63. Drop_role_priv: Y
  64. Password_reuse_history: NULL
  65. Password_reuse_time: NULL
  66. Password_require_current: NULL
  67. User_attributes: NULL
  68. 1 row in set (0.00 sec)
  69. ERROR:
  70. No query specified
  71. mysql>
  1. [root@riyimei mysqld]# mysql -uliwm -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 38
  5. Server version: 8.0.21 MySQL Community Server - GPL
  6. Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql> show databases;
  12. +--------------------+
  13. | Database |
  14. +--------------------+
  15. | babytun |
  16. | hisystem |
  17. | information_schema |
  18. | mysql |
  19. | performance_schema |
  20. | sys |
  21. | test1 |
  22. +--------------------+
  23. 7 rows in set (0.00 sec)
  24. mysql>

用户权限回收

用户角色权限管理

删除用户

  1. mysql> show tables;
  2. +---------------------------+
  3. | Tables_in_mysql |
  4. +---------------------------+
  5. | columns_priv |
  6. | component |
  7. | db |
  8. | default_roles |
  9. | engine_cost |
  10. | func |
  11. | general_log |
  12. | global_grants |
  13. | gtid_executed |
  14. | help_category |
  15. | help_keyword |
  16. | help_relation |
  17. | help_topic |
  18. | innodb_index_stats |
  19. | innodb_table_stats |
  20. | password_history |
  21. | plugin |
  22. | procs_priv |
  23. | proxies_priv |
  24. | role_edges |
  25. | server_cost |
  26. | servers |
  27. | slave_master_info |
  28. | slave_relay_log_info |
  29. | slave_worker_info |
  30. | slow_log |
  31. | tables_priv |
  32. | time_zone |
  33. | time_zone_leap_second |
  34. | time_zone_name |
  35. | time_zone_transition |
  36. | time_zone_transition_type |
  37. | user |
  38. +---------------------------+
  39. 33 rows in set (0.00 sec)
  40. mysql> SELECT user,host FROM mysql.user;
  41. +------------------+-----------+
  42. | user | host |
  43. +------------------+-----------+
  44. | root | % |
  45. | liwm | localhost |
  46. | mysql.infoschema | localhost |
  47. | mysql.session | localhost |
  48. | mysql.sys | localhost |
  49. +------------------+-----------+
  50. 5 rows in set (0.00 sec)
  51. mysql> drop user 'liwm'@'localhost';
  52. Query OK, 0 rows affected (0.00 sec)
  53. mysql>

DML 语句

指的是对数据库中表的操作记录、包括表记录的插入(inser)、更新(update)、删除(delete)、和查询(select)

1 插入记录

  1. mysql> show databases;
  2. +--------------------+
  3. | Database |
  4. +--------------------+
  5. | babytun |
  6. | information_schema |
  7. | mysql |
  8. | performance_schema |
  9. | sys |
  10. | test1 |
  11. +--------------------+
  12. 6 rows in set (0.00 sec)
  13. mysql> use babytun;
  14. Database changed
  15. mysql> show tables;
  16. +-------------------+
  17. | Tables_in_babytun |
  18. +-------------------+
  19. | t_category |
  20. | t_goods |
  21. | t_goods_cover |
  22. | t_goods_detail |
  23. | t_goods_param |
  24. +-------------------+
  25. 5 rows in set (0.00 sec)
  26. mysql> EXPLAIN SELECT * FROM t_goods_cover;
  27. +----+-------------+---------------+------------+------+---------------+------+---------+------+------+----------+-------+
  28. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  29. +----+-------------+---------------+------------+------+---------------+------+---------+------+------+----------+-------+
  30. | 1 | SIMPLE | t_goods_cover | NULL | ALL | NULL | NULL | NULL | NULL | 9469 | 100.00 | NULL |
  31. +----+-------------+---------------+------------+------+---------------+------+---------+------+------+----------+-------+
  32. 1 row in set, 1 warning (0.00 sec)
  33. mysql>