当前位置:   article > 正文

Centos下使用yum安装Mysql8(Mysql5.7)以及常见的配置和使用

yum安装mysql

记录一下在centos7.x下面使用yum方式安装mysql8(Mysql5.7)关系型数据库

安装之前一般需要先确定centos7.x服务器里是否已经安装,未安装或者刚初始化的centos7.x服务器最好安装,原来已经有的要升级的话一定要对系统原有mysql 或mariadb卸载干净,以免影响后续的安装

安装之前查看下有没有mysql,mariadb相关的东西存在

  1. [root@myw ~]# whereis mysql
  2. mysql: /usr/lib64/mysql /usr/share/mysql
  3. [root@myw ~]# find / -name 'mysql*'
  4. /etc/selinux/targeted/active/modules/100/mysql
  5. /etc/my.cnf.d/mysql-clients.cnf
  6. /usr/lib/firewalld/services/mysql.xml
  7. /usr/share/qemu-kvm/qemu-ga/fsfreeze-hook.d/mysql-flush.sh.sample
  8. /usr/share/man/man5/mysql_table.5.gz
  9. /usr/share/mysql
  10. /usr/lib64/mysql
  11. /usr/lib64/mysql/plugin/mysql_clear_password.so
  12. [root@myw ~]#
  13. [root@myw ~]# rpm -qa|grep mariadb
  14. [root@myw ~]#

查看了这些查出来的目录和文件,不影响安装mysql8

mysql8的安装操作

使用yum方式安装mysql的最新版mysql8

首先在mysql的官网找到安装源,使用yum方式(https://dev.mysql.com/downloads/repo/yum/)找到对应的版本信息

个人使用的是常见的QQ浏览器,

方式一,可以先把rpm包下载下来,然后通过ftp等工具上传到centos7.x服务器的/root目录下面

方式二,复制链接地址,使用wget方式直接把rpm包下载到centos7.x里,默认下载目录就是/root里

方式二得到的下载链接如下

https://repo.mysql.com//mysql80-community-release-el7-7.noarch.rpm

既然得到了这个链接,那么可以单独访问Index of /232905

我们可以看到

妥妥的一大笔资源在这里,mysql老版本和新版本都在这儿

这里安装mysql8 那么可以使用

wget https://repo.mysql.com//mysql80-community-release-el7-7.noarch.rpm

如果运行发现不可用,可能没有wget的软件包 先使用yum安装wget

yum -y install wget
  1. [root@myw ~]# yum -y install wget
  2. Loaded plugins: fastestmirror
  3. Determining fastest mirrors
  4. * base: 100.125.0.40
  5. * extras: 100.125.0.40
  6. * updates: 100.125.0.40
  7. base | 3.6 kB 00:00:00
  8. extras | 2.9 kB 00:00:00
  9. updates | 2.9 kB 00:00:00
  10. (1/2): extras/7/x86_64/primary_db | 249 kB 00:00:00
  11. ******************************
  12. // 有很多信息,不太需要关注
  13. ******************************
  14. Total download size: 547 k
  15. Installed size: 2.0 M
  16. Downloading packages:
  17. wget-1.14-18.el7_6.1.x86_64.rpm | 547 kB 00:00:00
  18. Running transaction check
  19. Running transaction test
  20. Transaction test succeeded
  21. Running transaction
  22. Installing : wget-1.14-18.el7_6.1.x86_64 1/1
  23. Verifying : wget-1.14-18.el7_6.1.x86_64 1/1
  24. Installed:
  25. wget.x86_64 0:1.14-18.el7_6.1
  26. Complete!
  27. [root@myw ~]#
  1. [root@myw ~]# wget https://repo.mysql.com//mysql80-community-release-el7-7.noarch.rpm
  2. --2023-01-06 09:58:40-- https://repo.mysql.com//mysql80-community-release-el7-7.noarch.rpm
  3. Resolving repo.mysql.com (repo.mysql.com)... 184.30.152.230
  4. Connecting to repo.mysql.com (repo.mysql.com)|184.30.152.230|:443... connected.
  5. HTTP request sent, awaiting response... 200 OK
  6. Length: 11196 (11K) [application/x-redhat-package-manager]
  7. Saving to: ?.ysql80-community-release-el7-7.noarch.rpm?
  8. 100%[========================================================================================================================================================================================>] 11,196 --.-K/s in 0s
  9. 2023-01-06 09:58:41 (250 MB/s) - ?.ysql80-community-release-el7-7.noarch.rpm?.saved [11196/11196]
  10. [root@myw ~]#

然后执行rpm方式

rpm -ivh mysql80-community-release-el7-7.noarch.rpm
  1. [root@myw ~]# rpm -ivh mysql80-community-release-el7-7.noarch.rpm
  2. warning: mysql80-community-release-el7-7.noarch.rpm: Header V4 RSA/SHA256 Signature, key ID 3a79bd29: NOKEY
  3. Preparing... ################################# [100%]
  4. Updating / installing...
  5. 1:mysql80-community-release-el7-7 ################################# [100%]
  6. [root@myw ~]#

再执行

  1. yum -y install mysql mysql-server mysql-devel
  2. 或者
  3. yum -y install mysql-server
  1. [root@myw ~]# yum -y install mysql mysql-server mysql-devel
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. * base: 100.125.0.40
  5. * extras: 100.125.0.40
  6. * updates: 100.125.0.40
  7. mysql-connectors-community | 2.6 kB 00:00:00
  8. mysql-tools-community | 2.6 kB 00:00:00
  9. mysql80-community | 2.6 kB 00:00:00
  10. (1/3): mysql-connectors-community/x86_64/primary_db | 93 kB 00:00:01
  11. (2/3): mysql-tools-community/x86_64/primary_db | 89 kB 00:00:01
  12. (3/3): mysql80-community/x86_64/primary_db | 222 kB 00:00:01
  13. Resolving Dependencies
  14. --> Running transaction check
  15. ---> Package mysql-community-client.x86_64 0:8.0.31-1.el7 will be installed
  16. --> Processing Dependency: mysql-community-client-plugins = 8.0.31-1.el7 for package: mysql-community-client-8.0.31-1.el7.x86_64
  17. --> Processing Dependency: mysql-community-libs(x86-64) >= 8.0.11 for package: mysql-community-client-8.0.31-1.el7.x86_64
  18. ---> Package mysql-community-devel.x86_64 0:8.0.31-1.el7 will be installed
  19. ***********
  20. //省略
  21. ***********
  22. Installed:
  23. mysql-community-client.x86_64 0:8.0.31-1.el7 mysql-community-devel.x86_64 0:8.0.31-1.el7 mysql-community-libs.x86_64 0:8.0.31-1.el7 mysql-community-libs-compat.x86_64 0:8.0.31-1.el7
  24. mysql-community-server.x86_64 0:8.0.31-1.el7
  25. Dependency Installed:
  26. keyutils-libs-devel.x86_64 0:1.5.8-3.el7 krb5-devel.x86_64 0:1.15.1-51.el7_9 libaio.x86_64 0:0.3.109-13.el7 libcom_err-devel.x86_64 0:1.42.9-19.el7
  27. libkadm5.x86_64 0:1.15.1-51.el7_9 libselinux-devel.x86_64 0:2.5-15.el7 libsepol-devel.x86_64 0:2.5-10.el7 libverto-devel.x86_64 0:0.2.5-4.el7
  28. mysql-community-client-plugins.x86_64 0:8.0.31-1.el7 mysql-community-common.x86_64 0:8.0.31-1.el7 mysql-community-icu-data-files.x86_64 0:8.0.31-1.el7 openssl-devel.x86_64 1:1.0.2k-22.el7_9
  29. pcre-devel.x86_64 0:8.32-17.el7 zlib-devel.x86_64 0:1.2.7-19.el7_9
  30. Dependency Updated:
  31. krb5-libs.x86_64 0:1.15.1-51.el7_9 openssl.x86_64 1:1.0.2k-22.el7_9 openssl-libs.x86_64 1:1.0.2k-22.el7_9 zlib.x86_64 0:1.2.7-19.el7_9
  32. Replaced:
  33. mariadb-libs.x86_64 1:5.5.68-1.el7
  34. Complete!
  35. [root@myw ~]#

常见mysql服务的操作(启动,停止,重启,加入开机自启动)

  1. [root@myw ~]# systemctl daemon-reload
  2. [root@myw ~]# systemctl start mysqld.service
  3. [root@myw ~]# netstat -lnp|grep 3306
  4. tcp6 0 0 :::33060 :::* LISTEN 8203/mysqld
  5. tcp6 0 0 :::3306 :::* LISTEN 8203/mysqld
  6. unix 2 [ ACC ] STREAM LISTENING 28463 8203/mysqld /var/run/mysqld/mysqlx.sock
  7. unix 2 [ ACC ] STREAM LISTENING 28467 8203/mysqld /var/lib/mysql/mysql.sock
  8. [root@myw ~]# systemctl status mysqld.service
  9. ?.mysqld.service - MySQL Server
  10. Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
  11. Active: active (running) since Fri 2023-01-06 10:12:55 CST; 52s ago
  12. Docs: man:mysqld(8)
  13. http://dev.mysql.com/doc/refman/en/using-systemd.html
  14. Process: 8120 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
  15. Main PID: 8203 (mysqld)
  16. Status: "Server is operational"
  17. CGroup: /system.slice/mysqld.service
  18. ?..8203 /usr/sbin/mysqld
  19. Jan 06 10:12:37 myw systemd[1]: Starting MySQL Server...
  20. Jan 06 10:12:55 myw systemd[1]: Started MySQL Server.
  21. [root@myw ~]# systemctl restart mysqld.service
  22. [root@myw ~]# netstat -lnp|grep 3306
  23. tcp6 0 0 :::33060 :::* LISTEN 8299/mysqld
  24. tcp6 0 0 :::3306 :::* LISTEN 8299/mysqld
  25. [root@myw ~]# systemctl stop mysqld.service
  26. [root@myw ~]# netstat -lnp|grep 3306
  27. [root@myw ~]# systemctl start mysqld.service
  28. [root@myw ~]# systemctl enable mysqld.service
  29. [root@myw ~]#

端口:3306 用于 MySQL Classic 协议(服务器端口选项,非常常用)

端口:33060 用于 MySQL X 协议(服务器 mysqlx_port 选项,一般不用,不管他)

  1. // 重新加载
  2. systemctl daemon-reload
  3. // 启动
  4. systemctl start mysqld.service
  5. // 重启
  6. systemctl restart mysqld.service
  7. // 停止
  8. systemctl stop mysqld.service
  9. // 查看启动状态
  10. systemctl status mysqld.service
  11. // 加入开机启动
  12. systemctl enable mysqld.service
  13. // 取消开机启动
  14. systemctl disable mysqld.service

centos7.x已经安装好了mysql8 还需要对mysql8进行数据库root密码修改和常见的配置

这里安装的mysql8需要去日志目录找到mysqld.log里找临时密码

/var/log/mysqld.log

  1. [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.31) initializing of server in progress as process 8154
  2. [System] [MY-013576] [InnoDB] InnoDB initialization has started.
  3. [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
  4. [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: mq4UOM&fdenM
  5. [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.31) starting as process 8203
  6. [System] [MY-013576] [InnoDB] InnoDB initialization has started.
  7. [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
  8. [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
  9. [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
  10. [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
  11. [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.31' socket: '/var/lib/mysql/mysql.sock' port: 3306 MySQL Community Server - GPL.
  1. [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: mq4UOM&fdenM
  2. 临时密码:mq4UOM&fdenM

使用临时密码登录后改密码

mysql8的默认密码策略要求密码必须是大小写字母数字特殊字母的组合,至少8位,有些时候为了简单,可以修改密码策略,实际个人觉得不影响使用,密码复杂点儿好,字符集需要utf8mb4,这里发现默认就是utf8mb4 可以说省了配置

ALTER USER 'root'@'localhost' IDENTIFIED BY 'myW.MyyhTw147258';
  1. [root@jpmlg4syqgfe22ch ~]# mysql -uroot -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 8
  5. Server version: 8.0.31
  6. Copyright (c) 2000, 2022, Oracle and/or its affiliates.
  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> \s
  12. ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
  13. mysql>
  14. mysql> \s
  15. ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
  16. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'myW.MyyhTw147258';
  17. Query OK, 0 rows affected (0.00 sec)
  18. mysql> \s
  19. --------------
  20. mysql Ver 8.0.31 for Linux on x86_64 (MySQL Community Server - GPL)
  21. Connection id: 8
  22. Current database:
  23. Current user: root@localhost
  24. SSL: Not in use
  25. Current pager: stdout
  26. Using outfile: ''
  27. Using delimiter: ;
  28. Server version: 8.0.31
  29. Protocol version: 10
  30. Connection: Localhost via UNIX socket
  31. Server characterset: utf8mb4
  32. Db characterset: utf8mb4
  33. Client characterset: utf8mb4
  34. Conn. characterset: utf8mb4
  35. UNIX socket: /var/lib/mysql/mysql.sock
  36. Binary data as: Hexadecimal
  37. Uptime: 9 min 3 sec
  38. Threads: 2 Questions: 8 Slow queries: 0 Opens: 129 Flush tables: 3 Open tables: 48 Queries per second avg: 0.014
  39. --------------
  40. mysql>

我们在/etc/my.cnf里查看到的配置信息是如此的,可以根据需求调整

  1. # For advice on how to change settings please see
  2. # http://dev.mysql.com/doc/refman/8.0/en/server-configuration-defaults.html
  3. [mysqld]
  4. #
  5. # Remove leading # and set to the amount of RAM for the most important data
  6. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
  7. # innodb_buffer_pool_size = 128M
  8. #
  9. # Remove the leading "# " to disable binary logging
  10. # Binary logging captures changes between backups and is enabled by
  11. # default. It's default setting is log_bin=binlog
  12. # disable_log_bin
  13. #
  14. # Remove leading # to set options mainly useful for reporting servers.
  15. # The server defaults are faster for transactions and fast SELECTs.
  16. # Adjust sizes as needed, experiment to find the optimal values.
  17. # join_buffer_size = 128M
  18. # sort_buffer_size = 2M
  19. # read_rnd_buffer_size = 2M
  20. #
  21. # Remove leading # to revert to previous value for default_authentication_plugin,
  22. # this will increase compatibility with older clients. For background, see:
  23. # https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html#sysvar_default_authentication_plugin
  24. # default-authentication-plugin=mysql_native_password
  25. datadir=/var/lib/mysql
  26. socket=/var/lib/mysql/mysql.sock
  27. log-error=/var/log/mysqld.log
  28. pid-file=/var/run/mysqld/mysqld.pid

配置文件中,有一个 disable_log_bin参数 ,这个参数一般需要启用,这样可以减少磁盘的日志输入,降低使用的资源,这样使用查询指令

  1. mysql> SHOW VARIABLES LIKE 'log_bin';
  2. +---------------+-------+
  3. | Variable_name | Value |
  4. +---------------+-------+
  5. | log_bin | OFF |
  6. +---------------+-------+
  7. 1 row in set (0.02 sec)
  8. mysql>

可以看到log_bin的值是OFF

  1. mysql> show variables like "%bin%";
  2. +------------------------------------------------+----------------------+
  3. | Variable_name | Value |
  4. +------------------------------------------------+----------------------+
  5. | bind_address | * |
  6. | binlog_cache_size | 32768 |
  7. | binlog_checksum | CRC32 |
  8. | binlog_direct_non_transactional_updates | OFF |
  9. | binlog_encryption | OFF |
  10. | binlog_error_action | ABORT_SERVER |
  11. | binlog_expire_logs_auto_purge | ON |
  12. | binlog_expire_logs_seconds | 2592000 |
  13. | binlog_format | ROW |
  14. | binlog_group_commit_sync_delay | 0 |
  15. | binlog_group_commit_sync_no_delay_count | 0 |
  16. | binlog_gtid_simple_recovery | ON |
  17. | binlog_max_flush_queue_time | 0 |
  18. | binlog_order_commits | ON |
  19. | binlog_rotate_encryption_master_key_at_startup | OFF |
  20. | binlog_row_event_max_size | 8192 |
  21. | binlog_row_image | FULL |
  22. | binlog_row_metadata | MINIMAL |
  23. | binlog_row_value_options | |
  24. | binlog_rows_query_log_events | OFF |
  25. | binlog_stmt_cache_size | 32768 |
  26. | binlog_transaction_compression | OFF |
  27. | binlog_transaction_compression_level_zstd | 3 |
  28. | binlog_transaction_dependency_history_size | 25000 |
  29. | binlog_transaction_dependency_tracking | COMMIT_ORDER |
  30. | innodb_api_enable_binlog | OFF |
  31. | log_bin | OFF |
  32. | log_bin_basename | |
  33. | log_bin_index | |
  34. | log_bin_trust_function_creators | OFF |
  35. | log_bin_use_v1_row_events | OFF |
  36. | log_statements_unsafe_for_binlog | ON |
  37. | max_binlog_cache_size | 18446744073709547520 |
  38. | max_binlog_size | 1073741824 |
  39. | max_binlog_stmt_cache_size | 18446744073709547520 |
  40. | mysqlx_bind_address | * |
  41. | sql_log_bin | ON |
  42. | sync_binlog | 1 |
  43. +------------------------------------------------+----------------------+
  44. 38 rows in set (0.00 sec)
  45. mysql>

到这里mysql就安装完成可以使用了,于是回过头来查询下

  1. [root@myw ~]# whereis mysql
  2. mysql: /usr/bin/mysql /usr/lib64/mysql /usr/include/mysql /usr/share/man/man1/mysql.1.gz
  3. [root@myw ~]# find / -name 'mysql*'
  4. /etc/ld.so.conf.d/mysql-x86_64.conf
  5. /etc/systemd/system/multi-user.target.wants/mysqld.service
  6. /etc/selinux/targeted/active/modules/100/mysql
  7. /etc/logrotate.d/mysql
  8. /etc/yum.repos.d/mysql-community.repo
  9. /etc/yum.repos.d/mysql-community-debuginfo.repo
  10. /etc/yum.repos.d/mysql-community-source.repo
  11. /sys/fs/cgroup/systemd/system.slice/mysqld.service
  12. /root/mysql80-community-release-el7-7.noarch.rpm
  13. /var/lib/yum/repos/x86_64/7/mysql80-community
  14. /var/lib/yum/repos/x86_64/7/mysql-connectors-community
  15. /var/lib/yum/repos/x86_64/7/mysql-tools-community
  16. /var/lib/mysql-keyring
  17. /var/lib/mysql-files
  18. /var/lib/mysql
  19. /var/lib/mysql/mysql.sock.lock
  20. /var/lib/mysql/mysql.sock
  21. /var/lib/mysql/mysql.ibd
  22. /var/lib/mysql/mysql
  23. /var/log/mysqld.log
  24. /var/cache/yum/x86_64/7/mysql80-community
  25. /var/cache/yum/x86_64/7/mysql-connectors-community
  26. /var/cache/yum/x86_64/7/mysql-tools-community
  27. /run/mysqld
  28. /run/mysqld/mysqld.pid
  29. /run/mysqld/mysqlx.sock
  30. /run/mysqld/mysqlx.sock.lock
  31. /usr/lib/systemd/system/mysqld.service
  32. /usr/lib/systemd/system/mysqld@.service
  33. /usr/lib/tmpfiles.d/mysql.conf
  34. /usr/lib/firewalld/services/mysql.xml
  35. /usr/share/aclocal/mysql.m4
  36. /usr/share/qemu-kvm/qemu-ga/fsfreeze-hook.d/mysql-flush.sh.sample
  37. /usr/share/mysql-8.0
  38. /usr/share/mysql-8.0/mysql-log-rotate
  39. /usr/share/man/man1/mysqldump.1.gz
  40. /usr/share/man/man1/mysql_upgrade.1.gz
  41. /usr/share/man/man1/mysqladmin.1.gz
  42. /usr/share/man/man1/mysqlpump.1.gz
  43. /usr/share/man/man1/mysqlimport.1.gz
  44. /usr/share/man/man1/mysqlman.1.gz
  45. /usr/share/man/man1/mysqlshow.1.gz
  46. /usr/share/man/man1/mysqlcheck.1.gz
  47. /usr/share/man/man1/mysqlslap.1.gz
  48. /usr/share/man/man1/mysql_config.1.gz
  49. /usr/share/man/man1/mysql_config_editor.1.gz
  50. /usr/share/man/man1/mysqldumpslow.1.gz
  51. /usr/share/man/man1/mysql_ssl_rsa_setup.1.gz
  52. /usr/share/man/man1/mysql_tzinfo_to_sql.1.gz
  53. /usr/share/man/man1/mysqlbinlog.1.gz
  54. /usr/share/man/man1/mysql_secure_installation.1.gz
  55. /usr/share/man/man1/mysql.1.gz
  56. /usr/share/man/man8/mysqld.8.gz
  57. /usr/share/man/man5/mysql_table.5.gz
  58. /usr/share/doc/mysql-community-libs-compat-8.0.31
  59. /usr/share/doc/mysql-community-icu-data-files-8.0.31
  60. /usr/share/doc/mysql-community-common-8.0.31
  61. /usr/share/doc/mysql-community-client-plugins-8.0.31
  62. /usr/share/doc/mysql-community-server-8.0.31
  63. /usr/share/doc/mysql-community-client-8.0.31
  64. /usr/share/doc/mysql-community-devel-8.0.31
  65. /usr/share/doc/mysql-community-libs-8.0.31
  66. /usr/sbin/mysqld
  67. /usr/lib64/pkgconfig/mysqlclient.pc
  68. /usr/lib64/mysql
  69. /usr/lib64/mysql/plugin/mysql_no_login.so
  70. /usr/lib64/mysql/plugin/mysql_clone.so
  71. /usr/include/mysql
  72. /usr/include/mysql/mysql_com.h
  73. /usr/include/mysql/mysql_version.h
  74. /usr/include/mysql/mysqlx_version.h
  75. /usr/include/mysql/mysql_time.h
  76. /usr/include/mysql/mysqld_error.h
  77. /usr/include/mysql/mysqlx_error.h
  78. /usr/include/mysql/mysqlx_ername.h
  79. /usr/include/mysql/mysql.h
  80. /usr/include/mysql/mysql
  81. /usr/bin/mysql_migrate_keyring
  82. /usr/bin/mysql_upgrade
  83. /usr/bin/mysqlshow
  84. /usr/bin/mysql_config-64
  85. /usr/bin/mysqlpump
  86. /usr/bin/mysql_secure_installation
  87. /usr/bin/mysql_config
  88. /usr/bin/mysqlslap
  89. /usr/bin/mysqlimport
  90. /usr/bin/mysqlcheck
  91. /usr/bin/mysqlbinlog
  92. /usr/bin/mysql_tzinfo_to_sql
  93. /usr/bin/mysql_config_editor
  94. /usr/bin/mysql_ssl_rsa_setup
  95. /usr/bin/mysqld_pre_systemd
  96. /usr/bin/mysqldumpslow
  97. /usr/bin/mysqldump
  98. /usr/bin/mysql
  99. /usr/bin/mysqladmin
  100. [root@myw ~]#
  101. [root@myw ~]# rpm -qa|grep -i mysql
  102. mysql-community-libs-8.0.31-1.el7.x86_64
  103. mysql-community-devel-8.0.31-1.el7.x86_64
  104. mysql80-community-release-el7-7.noarch
  105. mysql-community-common-8.0.31-1.el7.x86_64
  106. mysql-community-client-8.0.31-1.el7.x86_64
  107. mysql-community-icu-data-files-8.0.31-1.el7.x86_64
  108. mysql-community-server-8.0.31-1.el7.x86_64
  109. mysql-community-libs-compat-8.0.31-1.el7.x86_64
  110. mysql-community-client-plugins-8.0.31-1.el7.x86_64
  111. [root@myw ~]#

可以对比看到增加了不少东西,如果要卸载mysql来重新安装升级版本,这样要移除的文件太多了,操作起来麻烦不说,还不一定能卸载干净,所以建议重新选择版本相近的系统,或者重装系统,以免卸载后重装出现未知的错误问题而无法解决,可是有些条件下必须卸载,那就只有走卸载的方式了。

查询mysql的版本

  1. [root@myw ~]# mysql -V
  2. mysql Ver 8.0.31 for Linux on x86_64 (MySQL Community Server - GPL)
  3. [root@myw ~]# mysql --version
  4. mysql Ver 8.0.31 for Linux on x86_64 (MySQL Community Server - GPL)
  5. [root@myw ~]#
create database myw_test
  1. [root@myw ~]# find / -name 'myw_test'
  2. /var/lib/mysql/myw_test
  3. [root@myw ~]#

通过创建一个数据库方式 再去找这个数据库的名字就能找到mysql默认存数据的位置

mysql8的卸载操作

卸载mysql8不一定能完全卸载干净的,只能说尝试卸载干净吧

  1. [root@myw ~]# rpm -qa|grep -i mysql
  2. mysql-community-libs-8.0.31-1.el7.x86_64
  3. mysql-community-devel-8.0.31-1.el7.x86_64
  4. mysql80-community-release-el7-7.noarch
  5. mysql-community-common-8.0.31-1.el7.x86_64
  6. mysql-community-client-8.0.31-1.el7.x86_64
  7. mysql-community-icu-data-files-8.0.31-1.el7.x86_64
  8. mysql-community-server-8.0.31-1.el7.x86_64
  9. mysql-community-libs-compat-8.0.31-1.el7.x86_64
  10. mysql-community-client-plugins-8.0.31-1.el7.x86_64
  11. [root@myw ~]# yum -y remove mysql mysql-server mysql-devel
  12. // 省略了
  13. Removed:
  14. mysql-community-client.x86_64 0:8.0.31-1.el7 mysql-community-devel.x86_64 0:8.0.31-1.el7 mysql-community-server.x86_64 0:8.0.31-1.el7
  15. Complete!
  16. [root@myw ~]# rpm -qa|grep -i mysql
  17. mysql-community-libs-8.0.31-1.el7.x86_64
  18. mysql80-community-release-el7-7.noarch
  19. mysql-community-common-8.0.31-1.el7.x86_64
  20. mysql-community-icu-data-files-8.0.31-1.el7.x86_64
  21. mysql-community-libs-compat-8.0.31-1.el7.x86_64
  22. mysql-community-client-plugins-8.0.31-1.el7.x86_64
  23. [root@myw ~]#

可以看到上面的操作只是卸载了3个主要的,可以使用2个指令来删除

  1. yum remove XXX
  2. rpm -ev --nodeps XXXXXXXXXXX
  1. [root@myw ~]# rpm -ev --nodeps mysql-community-libs-8.0.31-1.el7.x86_64
  2. Preparing packages...
  3. mysql-community-libs-8.0.31-1.el7.x86_64
  4. [root@myw ~]# rpm -qa | grep mysql
  5. [root@myw ~]# yum remove mysql-community-common-8.0.31-1.el7.x86_64
  6. //   省略,选择y
  7. Installed size: 10 M
  8. Is this ok [y/N]: y
  9. Downloading packages:
  10. Running transaction check
  11. Running transaction test
  12. Transaction test succeeded
  13. Running transaction
  14. Erasing : mysql-community-common-8.0.31-1.el7.x86_64 1/1
  15. Verifying : mysql-community-common-8.0.31-1.el7.x86_64 1/1
  16. Removed:
  17. mysql-community-common.x86_64 0:8.0.31-1.el7
  18. Complete!
  19. [root@myw ~]# rpm -qa | grep mysql
  20. mysql-community-icu-data-files-8.0.31-1.el7.x86_64
  21. mysql-community-libs-compat-8.0.31-1.el7.x86_64
  22. mysql-community-client-plugins-8.0.31-1.el7.x86_64
  23. [root@myw ~]#
  24. // 建议使用 rpm -ev --nodeps XXXXX

如此再执行查询

  1. [root@myw ~]# rpm -qa | grep mysql
  2. [root@myw ~]# whereis mysql
  3. mysql: /usr/lib64/mysql
  4. [root@myw ~]# find / -name 'mysql*'
  5. /etc/selinux/targeted/active/modules/100/mysql
  6. /root/mysql80-community-release-el7-7.noarch.rpm
  7. /var/lib/yum/repos/x86_64/7/mysql80-community
  8. /var/lib/yum/repos/x86_64/7/mysql-connectors-community
  9. /var/lib/yum/repos/x86_64/7/mysql-tools-community
  10. /var/lib/mysql
  11. /var/lib/mysql/mysql.ibd
  12. /var/lib/mysql/mysql
  13. /var/log/mysqld.log
  14. /var/cache/yum/x86_64/7/mysql80-community
  15. /var/cache/yum/x86_64/7/mysql-connectors-community
  16. /var/cache/yum/x86_64/7/mysql-tools-community
  17. /usr/lib/firewalld/services/mysql.xml
  18. /usr/share/qemu-kvm/qemu-ga/fsfreeze-hook.d/mysql-flush.sh.sample
  19. /usr/share/mysql-8.0
  20. /usr/share/man/man5/mysql_table.5.gz
  21. /usr/lib64/mysql
  22. [root@myw ~]# find / -name 'mysql'
  23. /etc/selinux/targeted/active/modules/100/mysql
  24. /var/lib/mysql
  25. /var/lib/mysql/mysql
  26. /usr/lib64/mysql
  27. [root@myw ~]#

可以看到查询的东西少了不少,但其中还是有些东西需要删除的

圈出来的都是需要删除的,其中数据存储的位置/var/lib/mysql需要备份慎重删除的,这里是放数据的

  1. [root@myw ~]# rm -rf /root/mysql80-community-release-el7-7.noarch.rpm
  2. [root@myw ~]# rm -rf /var/lib/yum/repos/x86_64/7/mysql*
  3. [root@myw ~]# rm -rf /var/lib/mysql
  4. [root@myw ~]# rm -rf /var/cache/yum/x86_64/7/mysql*
  5. [root@myw ~]# rm -rf /var/log/mysqld.log
  6. [root@myw ~]# rm -rf /usr/lib/firewalld/services/mysql.xml
  7. [root@myw ~]# rm -rf /usr/share/mysql-8.0
  8. [root@myw ~]# rm -rf /usr/share/man/man5/mysql_table.5.gz
  9. [root@myw ~]# find / -name 'mysql'
  10. /etc/selinux/targeted/active/modules/100/mysql
  11. /usr/lib64/mysql
  12. [root@myw ~]# find / -name 'mysql*'
  13. /etc/selinux/targeted/active/modules/100/mysql
  14. /usr/share/qemu-kvm/qemu-ga/fsfreeze-hook.d/mysql-flush.sh.sample
  15. /usr/lib64/mysql
  16. [root@myw ~]# whereis mysql
  17. mysql: /usr/lib64/mysql
  18. [root@myw ~]#

还有一个my.cnf配置文件,目前执行了上面的操作 my.cnf文件已经消失不见了(/etc/my.cnf),那么如此算是卸载干净了,如果文件还存在,需要删除,以免后面新安装mysql或者mariadb影响到

rm -rf /etc/my.cnf

mysql5.7的安装操作(简写,很少用了)

  1. wget https://repo.mysql.com//mysql57-community-release-el7-10.noarch.rpm
  2. rpm -ivh mysql57-community-release-el7-10.noarch.rpm
  3. yum -y install mysql-server mysql-server mysql-devel

在执行yum -y install mysql-server mysql-server mysql-devel时,报出密匙过期的错误

  1. [root@myw ~]# yum -y install mysql-server mysql-devel
  2. // 省略了
  3. Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  4. The GPG keys listed for the "MySQL 5.7 Community Server" repository are already installed but they are not correct for this package.
  5. Check that the correct key URLs are configured for this repository.
  6. Failing package is: mysql-community-client-5.7.40-1.el7.x86_64
  7. GPG Keys are configured as: file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
  8. [root@myw ~]#

那么需要更新密匙后重新安装

rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022

实际上这个更新密匙来自这里儿(下图片)

执行那条语句后,重新安装,就能完成安装

yum -y install mysql-server mysql-server mysql-devel

mysql5.7也是有临时密码的,在/var/log/mysqld.log里面

  1. [root@myw ~]# mysql -uroot -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 4
  5. Server version: 5.7.40
  6. Copyright (c) 2000, 2022, Oracle and/or its affiliates.
  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> ALTER USER 'root'@'localhost' IDENTIFIED BY 'myW.MyyhTw147258';
  12. Query OK, 0 rows affected (0.00 sec)
  13. mysql> select Host, User, plugin, authentication_string, password_lifetime, password_last_changed from user;
  14. ERROR 1046 (3D000): No database selected
  15. mysql> use mysql
  16. Reading table information for completion of table and column names
  17. You can turn off this feature to get a quicker startup with -A
  18. Database changed
  19. mysql> select Host, User, plugin, authentication_string, password_lifetime, password_last_changed from user;
  20. +-----------+---------------+-----------------------+-------------------------------------------+-------------------+-----------------------+
  21. | Host | User | plugin | authentication_string | password_lifetime | password_last_changed |
  22. +-----------+---------------+-----------------------+-------------------------------------------+-------------------+-----------------------+
  23. | localhost | root | mysql_native_password | *2F9E1755F3CDF48D3A31BFEB08C74DC824CD1EC4 | NULL | 2023-01-06 18:25:50 |
  24. | localhost | mysql.session | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | NULL | 2023-01-06 18:04:10 |
  25. | localhost | mysql.sys | mysql_native_password | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | NULL | 2023-01-06 18:04:10 |
  26. +-----------+---------------+-----------------------+-------------------------------------------+-------------------+-----------------------+
  27. 3 rows in set (0.00 sec)
  28. mysql> \s
  29. --------------
  30. mysql Ver 14.14 Distrib 5.7.40, for Linux (x86_64) using EditLine wrapper
  31. Connection id: 4
  32. Current database: mysql
  33. Current user: root@localhost
  34. SSL: Not in use
  35. Current pager: stdout
  36. Using outfile: ''
  37. Using delimiter: ;
  38. Server version: 5.7.40
  39. Protocol version: 10
  40. Connection: Localhost via UNIX socket
  41. Server characterset: latin1
  42. Db characterset: latin1
  43. Client characterset: utf8
  44. Conn. characterset: utf8
  45. UNIX socket: /var/lib/mysql/mysql.sock
  46. Uptime: 23 min 25 sec
  47. Threads: 1 Questions: 43 Slow queries: 0 Opens: 137 Flush tables: 1 Open tables: 130 Queries per second avg: 0.030
  48. --------------
  49. mysql>

可以看到,mysql5.7的默认plugin是mysql_native_password 这个和mysql8不一样,得注意,还有一点就是mysql5.7的编码是utf8和latin1,这在开发中是需要重新配置的,在/etc/my.cnf里面配置

  1. # For advice on how to change settings please see
  2. # http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
  3. [client]
  4. default-character-set=utf8mb4
  5. [mysql]
  6. default-character-set=utf8mb4
  7. [mysqld]
  8. #
  9. # Remove leading # and set to the amount of RAM for the most important data
  10. # cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
  11. # innodb_buffer_pool_size = 128M
  12. #
  13. # Remove leading # to turn on a very important data integrity option: logging
  14. # changes to the binary log between backups.
  15. # log_bin
  16. #
  17. # Remove leading # to set options mainly useful for reporting servers.
  18. # The server defaults are faster for transactions and fast SELECTs.
  19. # Adjust sizes as needed, experiment to find the optimal values.
  20. # join_buffer_size = 128M
  21. # sort_buffer_size = 2M
  22. # read_rnd_buffer_size = 2M
  23. datadir=/var/lib/mysql
  24. socket=/var/lib/mysql/mysql.sock
  25. character-set-client-handshake = FALSE
  26. character-set-server=utf8mb4
  27. collation-server = utf8mb4_unicode_ci
  28. init_connect='SET NAMES utf8mb4'
  29. # Disabling symbolic-links is recommended to prevent assorted security risks
  30. symbolic-links=0
  31. sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES
  32. tmp_table_size = 256M
  33. max_heap_table_size = 256M
  34. log-error=/var/log/mysqld.log
  35. pid-file=/var/run/mysqld/mysqld.pid

修改重启mysql5.7后(utf8mb4)

  1. [root@myw ~]# mysql -uroot -p
  2. Enter password:
  3. Welcome to the MySQL monitor. Commands end with ; or \g.
  4. Your MySQL connection id is 2
  5. Server version: 5.7.40 MySQL Community Server (GPL)
  6. Copyright (c) 2000, 2022, Oracle and/or its affiliates.
  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> \s
  12. --------------
  13. mysql Ver 14.14 Distrib 5.7.40, for Linux (x86_64) using EditLine wrapper
  14. Connection id: 2
  15. Current database:
  16. Current user: root@localhost
  17. SSL: Not in use
  18. Current pager: stdout
  19. Using outfile: ''
  20. Using delimiter: ;
  21. Server version: 5.7.40 MySQL Community Server (GPL)
  22. Protocol version: 10
  23. Connection: Localhost via UNIX socket
  24. Server characterset: utf8mb4
  25. Db characterset: utf8mb4
  26. Client characterset: utf8mb4
  27. Conn. characterset: utf8mb4
  28. UNIX socket: /var/lib/mysql/mysql.sock
  29. Uptime: 55 sec
  30. Threads: 1 Questions: 5 Slow queries: 0 Opens: 106 Flush tables: 1 Open tables: 99 Queries per second avg: 0.090
  31. --------------
  32. mysql>

mysql8(mysql5.7)的常见不常用sql

记录,mysql数据库都是需要远程访问的,特别是大家一起开发的时候(本地数据库的效果反而很差),那么我们需要配置远程访问

先看看mysql的user表

  1. mysql> use mysql;
  2. Database changed
  3. mysql> select Host, User, plugin, authentication_string, password_lifetime, password_last_changed from user;
  4. +-----------+------------------+-----------------------+------------------------------------------------------------------------+-------------------+-----------------------+
  5. | Host | User | plugin | authentication_string | password_lifetime | password_last_changed |
  6. +-----------+------------------+-----------------------+------------------------------------------------------------------------+-------------------+-----------------------+
  7. | localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | NULL | 2023-01-06 10:12:51 |
  8. | localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | NULL | 2023-01-06 10:12:51 |
  9. | localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | NULL | 2023-01-06 10:12:51 |
  10. | localhost | root | caching_sha2_password | $A$005$l 5{fEm?d?Y-jYFeJj.lUv3eAFlEstmd8AKtJW.foBc8b9OwGHRQXn/hi0 | NULL | 2023-01-06 10:32:46 |
  11. +-----------+------------------+-----------------------+------------------------------------------------------------------------+-------------------+-----------------------+
  12. 4 rows in set (0.00 sec)

root账号的host是localhost,那么仅仅支持本地访问,而且加密方式是caching_sha2_password(mysql8),一般情况下root账户在开发或者生产最好不要远程能够访问到,容易出事故,如果我就是需要远程访问而且还能有所有权限,那么可以新建一个账户来解决

  1. mysql> CREATE USER 'sysroot'@'%' IDENTIFIED BY 'myW.123456';
  2. Query OK, 0 rows affected (0.01 sec)
  3. mysql> GRANT ALL ON *.* TO 'sysroot'@'%';
  4. Query OK, 0 rows affected (0.00 sec)
  5. mysql> flush privileges;
  6. Query OK, 0 rows affected (0.00 sec)
  7. mysql> select Host, User, plugin, authentication_string, password_lifetime, password_last_changed from user;
  8. +-----------+------------------+-----------------------+------------------------------------------------------------------------+-------------------+-----------------------+
  9. | Host | User | plugin | authentication_string | password_lifetime | password_last_changed |
  10. +-----------+------------------+-----------------------+------------------------------------------------------------------------+-------------------+-----------------------+
  11. | % | sysroot | caching_sha2_password | $A$005$u3WO"ME1BpbhvvAQPHnrrhNnvi8NUQXUr0vi643HwtmS4PWWYfPB | NULL | 2023-01-06 10:55:12 |
  12. | localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | NULL | 2023-01-06 10:12:51 |
  13. | localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | NULL | 2023-01-06 10:12:51 |
  14. | localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | NULL | 2023-01-06 10:12:51 |
  15. | localhost | root | caching_sha2_password | $A$005$l 5{fEm?d?Y-jYFeJj.lUv3eAFlEstmd8AKtJW.foBc8b9OwGHRQXn/hi0 | NULL | 2023-01-06 10:32:46 |
  16. +-----------+------------------+-----------------------+------------------------------------------------------------------------+-------------------+-----------------------+
  17. 5 rows in set (0.00 sec)
  18. mysql>

可以看到新增的sysroot用户host是% 加密方式是caching_sha2_password(mysql5.7没有),这个时候用Navicat或者其他工具连接,如果说出现连接不成功,有可能是密码的加密模式不支持caching_sha2_password,还是使用的mysql_native_password方式,那么可以重新设置一下加密方式并且更新密码

  1. mysql> ALTER USER 'sysroot'@'%' IDENTIFIED WITH mysql_native_password BY 'myW.123456';
  2. Query OK, 0 rows affected (0.01 sec)
  3. mysql> flush privileges;
  4. Query OK, 0 rows affected (0.00 sec)
  5. mysql> select Host, User, plugin, authentication_string, password_lifetime, password_last_changed from user;
  6. +-----------+------------------+-----------------------+------------------------------------------------------------------------+-------------------+-----------------------+
  7. | Host | User | plugin | authentication_string | password_lifetime | password_last_changed |
  8. +-----------+------------------+-----------------------+------------------------------------------------------------------------+-------------------+-----------------------+
  9. | % | sysroot | mysql_native_password | *F44ED6676E63CA5264A14380FAAF279B31482776 | NULL | 2023-01-06 10:59:59 |
  10. | localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | NULL | 2023-01-06 10:12:51 |
  11. | localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | NULL | 2023-01-06 10:12:51 |
  12. | localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | NULL | 2023-01-06 10:12:51 |
  13. | localhost | root | caching_sha2_password | $A$005$l 5{fEm?d?Y-jYFeJj.lUv3eAFlEstmd8AKtJW.foBc8b9OwGHRQXn/hi0 | NULL | 2023-01-06 10:32:46 |
  14. +-----------+------------------+-----------------------+------------------------------------------------------------------------+-------------------+-----------------------+
  15. 5 rows in set (0.00 sec)
  16. mysql>

可以看到plugin的密码的加密方式变了。

假如我不需要这个用户了,那么可以把这个用户删掉

DROP USER 'XXXXX'@'XXX';

例子,创建一个把他删除

  1. mysql> CREATE USER 'myroot'@'%' IDENTIFIED BY 'myW.123456';
  2. Database changed
  3. Query OK, 0 rows affected (0.01 sec)
  4. mysql> GRANT ALL ON *.* TO 'myroot'@'%';
  5. Query OK, 0 rows affected (0.01 sec)
  6. mysql> select Host, User, plugin, authentication_string, password_lifetime, password_last_changed from user;
  7. +-----------+------------------+-----------------------+------------------------------------------------------------------------+-------------------+-----------------------+
  8. | Host | User | plugin | authentication_string | password_lifetime | password_last_changed |
  9. +-----------+------------------+-----------------------+------------------------------------------------------------------------+-------------------+-----------------------+
  10. | % | myroot | caching_sha2_password | $A$005$|XdSN%Rj^%v9&.aa7oKHsz1/WwvfhJnXM6l.WHnJqqCM9e0RANZCbx/aSsgD | NULL | 2023-01-06 11:05:23 |
  11. | % | sysroot | caching_sha2_password | $A$005$7]{|qn&%;NZ@%b,D^z9.mBn942HLnoa.Y30HmjsioHjT2jyGsXtMjMbq14D4 | NULL | 2023-01-06 11:03:07 |
  12. | localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | NULL | 2023-01-06 10:12:51 |
  13. | localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | NULL | 2023-01-06 10:12:51 |
  14. | localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | NULL | 2023-01-06 10:12:51 |
  15. | localhost | root | caching_sha2_password | $A$005$l 5{fEm?d?Y-jYFeJj.lUv3eAFlEstmd8AKtJW.foBc8b9OwGHRQXn/hi0 | NULL | 2023-01-06 10:32:46 |
  16. +-----------+------------------+-----------------------+------------------------------------------------------------------------+-------------------+-----------------------+
  17. 6 rows in set (0.00 sec)
  18. mysql> DROP USER 'myroot'@'%';
  19. Query OK, 0 rows affected (0.01 sec)
  20. mysql> select Host, User, plugin, authentication_string, password_lifetime, password_last_changed from user;
  21. +-----------+------------------+-----------------------+------------------------------------------------------------------------+-------------------+-----------------------+
  22. | Host | User | plugin | authentication_string | password_lifetime | password_last_changed |
  23. +-----------+------------------+-----------------------+------------------------------------------------------------------------+-------------------+-----------------------+
  24. | % | sysroot | caching_sha2_password | $A$005$7]{|qn&%;NZ@%b,D^z9.mBn942HLnoa.Y30HmjsioHjT2jyGsXtMjMbq14D4 | NULL | 2023-01-06 11:03:07 |
  25. | localhost | mysql.infoschema | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | NULL | 2023-01-06 10:12:51 |
  26. | localhost | mysql.session | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | NULL | 2023-01-06 10:12:51 |
  27. | localhost | mysql.sys | caching_sha2_password | $A$005$THISISACOMBINATIONOFINVALIDSALTANDPASSWORDTHATMUSTNEVERBRBEUSED | NULL | 2023-01-06 10:12:51 |
  28. | localhost | root | caching_sha2_password | $A$005$l 5{fEm?d?Y-jYFeJj.lUv3eAFlEstmd8AKtJW.foBc8b9OwGHRQXn/hi0 | NULL | 2023-01-06 10:32:46 |
  29. +-----------+------------------+-----------------------+------------------------------------------------------------------------+-------------------+-----------------------+
  30. 5 rows in set (0.00 sec)
  31. mysql>

可以看到mysql的存储数据的地址 在 var/lib/mysql下面(这个平时很少用,万一出问题很重要)

记录一下mysql账号的操作,创建用户 ,更改密码,授权用户, 取消授权,刷新权限,删除用户

创建用户

  1. CREATE USER 'username'@'host' IDENTIFIED BY 'password'
  2. username: 你要创建的用户名字(非中文)
  3. host: 指定该用户登录主机范围(本地用户localhost, 任意主机登陆,使用通配符%)
  4. password: 该用户的登陆密码,按照密码的策略,一般大小写和特殊字符长度大于8
  5. CREATE USER 'mywroot'@'localhost' IDENTIFIED BY 'myW.MyyhTw147258'
  6. CREATE USER 'mywroot'@'%' IDENTIFIED BY 'myW.MyyhTw147258'

更改密码

  1. ALTER USER 'username'@'host' IDENTIFIED WITH (plugin) BY 'password';
  2. plugin: 密码方式 caching_sha2_password mysql_native_password
  3. ALTER USER 'mywroot'@'%' IDENTIFIED WITH mysql_native_password BY 'myW.123456'
  4. ALTER USER 'mywroot'@'%' IDENTIFIED WITH caching_sha2_password BY 'myW.123456'(mysql8)

授权用户

  1. GRANT (privileges) ON (databasename.tablename) TO 'username'@'host'
  2. GRANT privileges ON databasename.tablename TO 'username'@'host' WITH GRANT OPTION(该方式创建的用户可以给用户授权)
  3. privileges: 用户的操作权限 SELECT, INSERT, UPDATE, 所的权限则使用ALL
  4. databasename: 数据库名
  5. tablename: 表名
  6. 所有数据库和表的相应操作权限则可用*表示 *.*
  7. GRANT SELECT, INSERT, UPDATE ON mywdata.account TO 'mywroot'@'%'
  8. GRANT ALL ON *.* TO 'mywroot'@'%'
  9. 查看授权信息
  10. SHOW GRANTS FOR 'mywroot'@'%'

取消授权

  1. REVOKE (privileges) ON (databasename.tablename) FROM 'username'@'host'
  2. REVOKE SELECT, INSERT ON *.* FROM 'mywroot'@'%'

刷新权限

  1. flush privileges;
  2. 更新或者修改或者删除用户权限后都刷新

删除用户

DROP USER 'username'@'host'

记录一下查询mysql表容量大小的查询sql(用的机会少,但每次用都去查还不如自己记录)

  1. 查询存在的数据库容量大小
  2. select
  3. table_schema as 'database_name', sum(table_rows) as 'record_number', sum(truncate(data_length/1024/1024, 2)) as 'data_size(MB)', sum(truncate(index_length/1024/1024, 2)) as 'index size(MB)'
  4. from
  5. information_schema.TABLES
  6. group by
  7. table_schema
  8. order by
  9. sum(data_length) desc, sum(index_length) desc
  10. 查询存在数据库的各表容量大小
  11. select
  12. table_schema as 'database_name', table_name as 'table_name', table_rows as 'record_number', truncate(data_length/1024/1024, 2) as 'data_size(MB)', truncate(index_length/1024/1024, 2) as 'index size(MB)'
  13. from
  14. information_schema.TABLES
  15. order by
  16. data_length desc, index_length desc
  17. 查询指定的数据库容量大小
  18. select
  19. table_schema as 'database_name', sum(table_rows) as 'record_number', sum(truncate(data_length/1024/1024, 2)) as 'data_size(MB)', sum(truncate(index_length/1024/1024, 2)) as 'index size(MB)'
  20. from
  21. information_schema.TABLES
  22. where
  23. table_schema = 'test'
  24. group by
  25. table_schema
  26. order by
  27. sum(data_length) desc, sum(index_length) desc
  28. 查询指定的数据库各表容量大小
  29. select
  30. table_schema as 'database_name', table_name as 'table_name', table_rows as 'record_number', truncate(data_length/1024/1024, 2) as 'data_size(MB)', truncate(index_length/1024/1024, 2) as 'index size(MB)'
  31. from
  32. information_schema.TABLES
  33. where
  34. table_schema = 'test'
  35. order by
  36. data_length desc, index_length desc
  37. 查询指定的数据库-指定表容量大小
  38. select
  39. table_schema as 'database_name', table_name as 'table_name', table_rows as 'record_number', truncate(data_length/1024/1024, 2) as 'data_size(MB)', truncate(index_length/1024/1024, 2) as 'index size(MB)'
  40. from
  41. information_schema.TABLES
  42. where
  43. table_schema = 'test' and table_name = 'test.account'
  44. order by
  45. data_length desc, index_length desc

记录一下查询mysql 在centos7.x下面备份

  1. 备份数据库 myw_test
  2. mysqldump -usysroot -p myw_test > /home/myw_test-20230101.sql
  3. 备份数据库 myw_test 表名 account
  4. mysqldump -usysroot -p myw_test account > /home/myw_test_account-20230101.sql
  5. 按照指定id范围备份数据库
  6. mysqldump -usysroot -p myw_test account -w "account.user_id < 1920" > /home/myw_test_account-20230101.sql
  7. mysqldump -usysroot -p myw_test account -w "account.user_id > 12 and account.user_id < 1920" > /home/myw_test_account-20230101.sql

记录一下查询正在运行的线程以及锁等待,死锁的情况

  1. SELECT * FROM information_schema.INNODB_TRX
  2. kill XXXXX
  3. XXXXX这个值是trx_mysql_thread_id

在执行结果中如果看到有等待的或者直接锁死成死锁了,直接杀掉

select * from information_schema.processlist;

显示用户正在运行的线程,这里特殊点儿(除了超级的 root 用户能看到所有正在运行的线程外,其他用户都只能看到自己正在运行的线程,看不到其它用户正在运行的线程)

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/955678
推荐阅读
相关标签
  

闽ICP备14008679号