一.liunx下安装mysql

1.使用wget指令下载mysql压缩包

  1. mysql压缩包下载:<br /> [https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.25-linux-glibc2.12-i686.tar.xz](https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.25-linux-glibc2.12-i686.tar.xz)

安装mysql


由于mysql8 在liunx下不会自动生成my.cnf配置文件所以在/etc/ 创建my.cnf创建该文件

配置My.cnf

https://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html 由于MySQL8 已不在安装包中包含配置文件,建议初始化MySQL之前去建立my.cnf Note: service mysqld stop

  1. [client]
  2. port = 3306
  3. #根据实际情况调整mysql.sock配置
  4. socket = /tmp/mysql.sock
  5. [mysqld]
  6. #Mysql服务的唯一编号 每个mysql服务Id需唯一
  7. server-id = 1
  8. #服务端口号 默认3306
  9. port = 3306
  10. #mysql安装根目录
  11. basedir = /usr/local/mysql
  12. #mysql数据文件所在位置
  13. datadir = /usr/local/mysql/data
  14. #pid
  15. pid-file = /usr/local/mysql/data/mysql.pid
  16. #设置socke文件所在目录
  17. socket = /tmp/mysql.sock
  18. #设置临时目录
  19. tmpdir = /tmp
  20. # 用户
  21. user = mysql
  22. # 允许访问的IP网段
  23. bind-address = 0.0.0.0
  24. # 跳过密码登录
  25. #skip-grant-tables
  26. #主要用于MyISAM存储引擎,如果多台服务器连接一个数据库则建议注释下面内容
  27. skip-external-locking
  28. #只能用IP地址检查客户端的登录,不用主机名
  29. skip_name_resolve = 1
  30. #事务隔离级别,默认为可重复读,mysql默认可重复读级别(此级别下可能参数很多间隙锁,影响性能)
  31. transaction_isolation = REPEATABLE-READ
  32. #数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节)
  33. character-set-server = utf8mb4
  34. #数据库字符集对应一些排序等规则,注意要和character-set-server对应
  35. collation-server = utf8mb4_general_ci
  36. #设置client连接mysql时的字符集,防止乱码
  37. init_connect='SET NAMES utf8mb4'
  38. #是否对sql语句大小写敏感,1表示不敏感
  39. lower_case_table_names = 1
  40. #最大连接数
  41. max_connections = 400
  42. #最大错误连接数
  43. max_connect_errors = 1000
  44. #TIMESTAMP如果没有显示声明NOT NULL,允许NULL值
  45. explicit_defaults_for_timestamp = true
  46. #SQL数据包发送的大小,如果有BLOB对象建议修改成1G
  47. max_allowed_packet = 128M
  48. #MySQL连接闲置超过一定时间后(单位:秒)将会被强行关闭
  49. #MySQL默认的wait_timeout 值为8个小时, interactive_timeout参数需要同时配置才能生效
  50. interactive_timeout = 1800
  51. wait_timeout = 1800
  52. #内部内存临时表的最大值 ,设置成128M。
  53. #比如大数据量的group by ,order by时可能用到临时表,
  54. #超过了这个值将写入磁盘,系统IO压力增大
  55. tmp_table_size = 134217728
  56. max_heap_table_size = 134217728
  57. #数据库错误日志文件
  58. log_error = error.log
  59. #慢查询sql日志设置
  60. slow_query_log = 1
  61. slow_query_log_file = slow.log
  62. #检查未使用到索引的sql
  63. log_queries_not_using_indexes = 1
  64. #针对log_queries_not_using_indexes开启后,记录慢sql的频次、每分钟记录的条数
  65. log_throttle_queries_not_using_indexes = 5
  66. #作为从库时生效,从库复制中如何有慢sql也将被记录
  67. log_slow_slave_statements = 1
  68. #慢查询执行的秒数,必须达到此值可被记录
  69. long_query_time = 8
  70. #检索的行数必须达到此值才可被记为慢查询
  71. min_examined_row_limit = 100
  72. #mysql binlog日志文件保存的过期时间,过期后自动删除
  73. expire_logs_days = 5

①:下载mysql压缩包并放在/use/local下

       **  **                 <br />            **  a:解压并修改文件名为mysql**
[root@localhost etc]# cd /usr/local/
[root@localhost local]# ll
总用量 875216
drwxr-xr-x. 2 root root         6 11月  3 2020 bin
drwxr-xr-x. 2 root root         6 11月  3 2020 etc
drwxr-xr-x. 2 root root         6 11月  3 2020 games
drwxr-xr-x. 2 root root         6 11月  3 2020 include
drwxr-xr-x. 2 root root         6 11月  3 2020 lib
drwxr-xr-x. 3 root root        17 7月  25 09:01 lib64
drwxr-xr-x. 2 root root         6 11月  3 2020 libexec
-rw-r--r--. 1 root root 896219776 7月  25 20:43 mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
drwxr-xr-x. 2 root root         6 11月  3 2020 sbin
drwxr-xr-x. 5 root root        49 7月  25 09:01 share
drwxr-xr-x. 2 root root         6 11月  3 2020 src
[root@localhost local]# xz -d mysql-8.0.26-linux-glibc2.12-x86_64.tar.xz
[root@localhost local]# ll
总用量 1460976
drwxr-xr-x. 2 root root          6 11月  3 2020 bin
drwxr-xr-x. 2 root root          6 11月  3 2020 etc
drwxr-xr-x. 2 root root          6 11月  3 2020 games
drwxr-xr-x. 2 root root          6 11月  3 2020 include
drwxr-xr-x. 2 root root          6 11月  3 2020 lib
drwxr-xr-x. 3 root root         17 7月  25 09:01 lib64
drwxr-xr-x. 2 root root          6 11月  3 2020 libexec
-rw-r--r--. 1 root root 1496254976 7月  25 20:43 mysql-8.0.26-linux-glibc2.12-x86_64.tar
drwxr-xr-x. 2 root root          6 11月  3 2020 sbin
drwxr-xr-x. 5 root root         49 7月  25 09:01 share
drwxr-xr-x. 2 root root          6 11月  3 2020 src
[root@localhost local]# tar  -xvf mysql-8.0.26-linux-glibc2.12-x86_64.tar
[root@localhost local]# ll
总用量 1460976
drwxr-xr-x. 2 root root          6 11月  3 2020 bin
drwxr-xr-x. 2 root root          6 11月  3 2020 etc
drwxr-xr-x. 2 root root          6 11月  3 2020 games
drwxr-xr-x. 2 root root          6 11月  3 2020 include
drwxr-xr-x. 2 root root          6 11月  3 2020 lib
drwxr-xr-x. 3 root root         17 7月  25 09:01 lib64
drwxr-xr-x. 2 root root          6 11月  3 2020 libexec
drwxr-xr-x. 9 root root        129 7月  25 20:44 mysql-8.0.25-linux-glibc2.12-x86_64
-rw-r--r--. 1 root root 1496254976 7月  25 20:43 mysql-8.0.26-linux-glibc2.12-x86_64.tar
drwxr-xr-x. 2 root root          6 11月  3 2020 sbin
drwxr-xr-x. 5 root root         49 7月  25 09:01 share
drwxr-xr-x. 2 root root          6 11月  3 2020 src
##修改解压后的文件名
[root@localhost local]# mv  mysql-8.0.25-linux-glibc2.12-x86_64 mysql
[root@localhost local]# ll
总用量 1460976
drwxr-xr-x. 2 root root          6 11月  3 2020 bin
drwxr-xr-x. 2 root root          6 11月  3 2020 etc
drwxr-xr-x. 2 root root          6 11月  3 2020 games
drwxr-xr-x. 2 root root          6 11月  3 2020 include
drwxr-xr-x. 2 root root          6 11月  3 2020 lib
drwxr-xr-x. 3 root root         17 7月  25 09:01 lib64
drwxr-xr-x. 2 root root          6 11月  3 2020 libexec
drwxr-xr-x. 9 root root        129 7月  25 20:44 mysql
-rw-r--r--. 1 root root 1496254976 7月  25 20:43 mysql-8.0.26-linux-glibc2.12-x86_64.tar
drwxr-xr-x. 2 root root          6 11月  3 2020 sbin
drwxr-xr-x. 5 root root         49 7月  25 09:01 share
drwxr-xr-x. 2 root root          6 11月  3 2020 src
[root@localhost local]# cd mysql/

[root@localhost mysql]# groupadd mysql

②:在liunx下添加配置mysql用户

  **     **
[root@localhost local]# ll
总用量 1460976
drwxr-xr-x. 2 root root          6 11月  3 2020 bin
drwxr-xr-x. 2 root root          6 11月  3 2020 etc
drwxr-xr-x. 2 root root          6 11月  3 2020 games
drwxr-xr-x. 2 root root          6 11月  3 2020 include
drwxr-xr-x. 2 root root          6 11月  3 2020 lib
drwxr-xr-x. 3 root root         17 7月  25 09:01 lib64
drwxr-xr-x. 2 root root          6 11月  3 2020 libexec
drwxr-xr-x. 9 root root        129 7月  25 20:44 mysql
-rw-r--r--. 1 root root 1496254976 7月  25 20:43 mysql-8.0.26-linux-glibc2.12-x86_64.tar
drwxr-xr-x. 2 root root          6 11月  3 2020 sbin
drwxr-xr-x. 5 root root         49 7月  25 09:01 share
drwxr-xr-x. 2 root root          6 11月  3 2020 src
[root@localhost local]# cd mysql/

 ##:添加mysql用户组
[root@localhost mysql]# groupadd mysql
[root@localhost mysql]# cd ..
[root@localhost local]# ll
总用量 1460976
drwxr-xr-x. 2 root root          6 11月  3 2020 bin
drwxr-xr-x. 2 root root          6 11月  3 2020 etc
drwxr-xr-x. 2 root root          6 11月  3 2020 games
drwxr-xr-x. 2 root root          6 11月  3 2020 include
drwxr-xr-x. 2 root root          6 11月  3 2020 lib
drwxr-xr-x. 3 root root         17 7月  25 09:01 lib64
drwxr-xr-x. 2 root root          6 11月  3 2020 libexec
drwxr-xr-x. 9 root root        129 7月  25 20:44 mysql
-rw-r--r--. 1 root root 1496254976 7月  25 20:43 mysql-8.0.26-linux-glibc2.12-x86_64.tar
drwxr-xr-x. 2 root root          6 11月  3 2020 sbin
drwxr-xr-x. 5 root root         49 7月  25 09:01 share
drwxr-xr-x. 2 root root          6 11月  3 2020 src

####添加MySQL用户
[root@localhost local]# useradd -r -g mysql -s /bin/false mysql
[root@localhost local]# cd mysql/
###生成mysql-files文件
[root@localhost mysql]# mkdir mysql-files
[root@localhost mysql]# ll
总用量 284
drwxr-xr-x.  2 7161 31415   4096 4月  23 14:56 bin
drwxr-xr-x.  2 7161 31415     55 4月  23 14:56 docs
drwxr-xr-x.  3 7161 31415    282 4月  23 14:56 include
drwxr-xr-x.  6 7161 31415    201 4月  23 14:56 lib
-rw-r--r--.  1 7161 31415 274942 4月  23 11:06 LICENSE
drwxr-xr-x.  4 7161 31415     30 4月  23 14:56 man
drwxr-xr-x.  2 root root       6 7月  25 20:46 mysql-files
-rw-r--r--.  1 7161 31415    666 4月  23 11:06 README
drwxr-xr-x. 28 7161 31415   4096 4月  23 14:56 share
drwxr-xr-x.  2 7161 31415     77 4月  23 14:56 support-files


####赋予MySQL用户MySQL用户组 对mysql-files的权限
[root@localhost mysql]# chown mysql:mysql mysql-files
[root@localhost mysql]# ll
总用量 284
drwxr-xr-x.  2  7161 31415   4096 4月  23 14:56 bin
drwxr-xr-x.  2  7161 31415     55 4月  23 14:56 docs
drwxr-xr-x.  3  7161 31415    282 4月  23 14:56 include
drwxr-xr-x.  6  7161 31415    201 4月  23 14:56 lib
-rw-r--r--.  1  7161 31415 274942 4月  23 11:06 LICENSE
drwxr-xr-x.  4  7161 31415     30 4月  23 14:56 man
drwxr-xr-x.  2 mysql mysql      6 7月  25 20:46 mysql-files
-rw-r--r--.  1  7161 31415    666 4月  23 11:06 README
drwxr-xr-x. 28  7161 31415   4096 4月  23 14:56 share
drwxr-xr-x.  2  7161 31415     77 4月  23 14:56 support-files
###赋予MySQL用户的执行权限
[root@localhost mysql]# chmod 750 mysql-files
[root@localhost mysql]# ll
总用量 284
drwxr-xr-x.  2  7161 31415   4096 4月  23 14:56 bin
drwxr-xr-x.  2  7161 31415     55 4月  23 14:56 docs
drwxr-xr-x.  3  7161 31415    282 4月  23 14:56 include
drwxr-xr-x.  6  7161 31415    201 4月  23 14:56 lib
-rw-r--r--.  1  7161 31415 274942 4月  23 11:06 LICENSE
drwxr-xr-x.  4  7161 31415     30 4月  23 14:56 man
drwxr-x---.  2 mysql mysql      6 7月  25 20:46 mysql-files
-rw-r--r--.  1  7161 31415    666 4月  23 11:06 README
drwxr-xr-x. 28  7161 31415   4096 4月  23 14:56 share
drwxr-xr-x.  2  7161 31415     77 4月  23 14:56 support-files

③执行MySQL的初始化 将会生成一个data文件夹 ,最后一行生成一个temporary密码

[root@localhost mysql]# bin/mysqld --initialize --user=mysql
 ①:如果实现适配了my.cnf则
  临时密码会在 usr/local/mysql/data/error.log 中
 ②:如果没有配置my.cnf<br />             执行完之后会显示临时密码

④:启动进入mysql

###启动MySQL
    [root@localhost mysql]# bin/mysqld_safe --user=mysql &
    [1] 1816
    [root@localhost mysql]# Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
    2021-06-16T07:22:46.224871Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data

    [root@localhost mysql]#
    [root@localhost mysql]#

    ###查看MySQL是否启动
    [root@localhost mysql]# ps -ef | grep mysql
    root        1816    1523  0 15:22 pts/0    00:00:00 /bin/sh bin/mysqld_safe --user=mysql
    mysql       1898    1816  9 15:22 pts/0    00:00:01 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=localhost.localdomain.err --pid-file=localhost.localdomain.pid
    root        1951    1523  0 15:22 pts/0    00:00:00 grep --color=auto mysql
    [root@localhost mysql]#
    [root@localhost mysql]#
    [root@localhost mysql]#
    [root@localhost mysql]#
    [root@localhost mysql]# cd bin


    ###进入MySQL发现错误
    [root@localhost bin]# ./mysql -uroot -p
    ./mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory
    [root@localhost bin]#
    [root@localhost bin]#


    ####创建符号链接
    [root@localhost bin]# sudo ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5
    [root@localhost bin]#
    [root@localhost bin]#
    [root@localhost bin]# ./mysql -uroot -p
    Enter password:
    ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)

    ###将产生的临时密码已下面方式进行登录
    [root@localhost bin]# ./mysql -uroot -p2M78VcFDrf+G
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 9
    Server version: 8.0.25

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

    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.

    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

    mysql> show databases;
    ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
    mysql>
    mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
    Query OK, 0 rows affected (0.00 sec)