参考 https://dev.mysql.com/doc/refman/8.0/en/binary-installation.html https://dev.mysql.com/doc/refman/8.0/en/starting-server.html https://www.cnblogs.com/kaishirenshi/p/12667004.html

安装MySQL

  1. ###下载安装包
  2. wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz
  3. ### 对MySQL进行重命名
  4. [root@localhost local]# mv mysql-8.0.25-linux-glibc2.12-x86_64/ mysql
  5. [root@localhost local]# ll
  6. total 0
  7. drwxr-xr-x. 2 root root 6 Nov 3 2020 bin
  8. drwxr-xr-x. 2 root root 6 Nov 3 2020 etc
  9. drwxr-xr-x. 2 root root 6 Nov 3 2020 games
  10. drwxr-xr-x. 2 root root 6 Nov 3 2020 include
  11. drwxr-xr-x. 2 root root 6 Nov 3 2020 lib
  12. drwxr-xr-x. 3 root root 17 Jun 16 02:59 lib64
  13. drwxr-xr-x. 2 root root 6 Nov 3 2020 libexec
  14. drwxr-xr-x. 9 root root 129 Jun 16 03:16 mysql
  15. drwxr-xr-x. 2 root root 6 Nov 3 2020 sbin
  16. drwxr-xr-x. 5 root root 49 Jun 16 02:59 share
  17. drwxr-xr-x. 2 root root 6 Nov 3 2020 src
  18. [root@localhost local]#
  19. [root@localhost local]#
  20. ###创建用户组
  21. [root@localhost local]# groupadd mysql
  22. [root@localhost local]#
  23. [root@localhost local]#
  24. [root@localhost local]#
  25. ####添加MySQL用户
  26. [root@localhost local]# useradd -r -g mysql -s /bin/false mysql
  27. [root@localhost local]# cd mysql/
  28. [root@localhost mysql]#
  29. [root@localhost mysql]#
  30. [root@localhost mysql]#
  31. [root@localhost mysql]# mkdir mysql-files
  32. [root@localhost mysql]#
  33. [root@localhost mysql]#
  34. [root@localhost mysql]#
  35. [root@localhost mysql]# ll
  36. total 284
  37. drwxr-xr-x. 2 7161 31415 4096 Apr 23 14:56 bin
  38. drwxr-xr-x. 2 7161 31415 55 Apr 23 14:56 docs
  39. drwxr-xr-x. 3 7161 31415 282 Apr 23 14:56 include
  40. drwxr-xr-x. 6 7161 31415 201 Apr 23 14:56 lib
  41. -rw-r--r--. 1 7161 31415 274942 Apr 23 11:06 LICENSE
  42. drwxr-xr-x. 4 7161 31415 30 Apr 23 14:56 man
  43. drwxr-xr-x. 2 root root 6 Jun 16 03:17 mysql-files
  44. -rw-r--r--. 1 7161 31415 666 Apr 23 11:06 README
  45. drwxr-xr-x. 28 7161 31415 4096 Apr 23 14:56 share
  46. drwxr-xr-x. 2 7161 31415 77 Apr 23 14:56 support-files
  47. [root@localhost mysql]#
  48. [root@localhost mysql]#
  49. ####赋予MySQL用户MySQL用户组 对mysql-files的权限
  50. [root@localhost mysql]# chown mysql:mysql mysql-files
  51. [root@localhost mysql]#
  52. [root@localhost mysql]#
  53. [root@localhost mysql]#
  54. [root@localhost mysql]# ll
  55. total 284
  56. drwxr-xr-x. 2 7161 31415 4096 Apr 23 14:56 bin
  57. drwxr-xr-x. 2 7161 31415 55 Apr 23 14:56 docs
  58. drwxr-xr-x. 3 7161 31415 282 Apr 23 14:56 include
  59. drwxr-xr-x. 6 7161 31415 201 Apr 23 14:56 lib
  60. -rw-r--r--. 1 7161 31415 274942 Apr 23 11:06 LICENSE
  61. drwxr-xr-x. 4 7161 31415 30 Apr 23 14:56 man
  62. drwxr-xr-x. 2 mysql mysql 6 Jun 16 03:17 mysql-files
  63. -rw-r--r--. 1 7161 31415 666 Apr 23 11:06 README
  64. drwxr-xr-x. 28 7161 31415 4096 Apr 23 14:56 share
  65. drwxr-xr-x. 2 7161 31415 77 Apr 23 14:56 support-files
  66. [root@localhost mysql]#
  67. [root@localhost mysql]#
  68. ###赋予MySQL用户的执行权限
  69. [root@localhost mysql]# chmod 750 mysql-files
  70. [root@localhost mysql]#
  71. [root@localhost mysql]#
  72. [root@localhost mysql]# ll
  73. total 284
  74. drwxr-xr-x. 2 7161 31415 4096 Apr 23 14:56 bin
  75. drwxr-xr-x. 2 7161 31415 55 Apr 23 14:56 docs
  76. drwxr-xr-x. 3 7161 31415 282 Apr 23 14:56 include
  77. drwxr-xr-x. 6 7161 31415 201 Apr 23 14:56 lib
  78. -rw-r--r--. 1 7161 31415 274942 Apr 23 11:06 LICENSE
  79. drwxr-xr-x. 4 7161 31415 30 Apr 23 14:56 man
  80. drwxr-x---. 2 mysql mysql 6 Jun 16 03:17 mysql-files
  81. -rw-r--r--. 1 7161 31415 666 Apr 23 11:06 README
  82. drwxr-xr-x. 28 7161 31415 4096 Apr 23 14:56 share
  83. drwxr-xr-x. 2 7161 31415 77 Apr 23 14:56 support-files
  84. [root@localhost mysql]#
  85. [root@localhost mysql]#
  86. ###执行MySQL的初始化 将会生成一个data文件夹 ,最后一行生成一个temporary密码
  87. [root@localhost mysql]# bin/mysqld --initialize --user=mysql
  88. 2021-06-16T07:18:33.509368Z 0 [System] [MY-013169] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.25) initializing of server in progress as process 1682
  89. 2021-06-16T07:18:33.521073Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
  90. 2021-06-16T07:18:34.461153Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
  91. 2021-06-16T07:18:35.564759Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 2M78VcFDrf+G
  92. [root@localhost mysql]#
  93. [root@localhost mysql]#
  94. [root@localhost mysql]# cd bin
  95. [root@localhost bin]#
  96. [root@localhost bin]#
  97. [root@localhost bin]# ps -ef | grep mysql
  98. root 1734 1523 0 03:19 pts/0 00:00:00 grep --color=auto mysql
  99. [root@localhost mysql]# ll
  100. total 288
  101. drwxr-xr-x. 2 7161 31415 4096 Apr 24 02:56 bin
  102. drwxr-x---. 6 mysql mysql 4096 Jun 16 15:18 data
  103. drwxr-xr-x. 2 7161 31415 55 Apr 24 02:56 docs
  104. drwxr-xr-x. 3 7161 31415 282 Apr 24 02:56 include
  105. drwxr-xr-x. 6 7161 31415 201 Apr 24 02:56 lib
  106. -rw-r--r--. 1 7161 31415 274942 Apr 23 23:06 LICENSE
  107. drwxr-xr-x. 4 7161 31415 30 Apr 24 02:56 man
  108. drwxr-x---. 2 mysql mysql 6 Jun 16 15:17 mysql-files
  109. -rw-r--r--. 1 7161 31415 666 Apr 23 23:06 README
  110. drwxr-xr-x. 28 7161 31415 4096 Apr 24 02:56 share
  111. drwxr-xr-x. 2 7161 31415 77 Apr 24 02:56 support-files
  112. [root@localhost mysql]#
  113. [root@localhost mysql]#
  114. [root@localhost mysql]# cd data
  115. [root@localhost data]# ll
  116. total 176568
  117. -rw-r-----. 1 mysql mysql 56 Jun 16 15:18 auto.cnf
  118. -rw-------. 1 mysql mysql 1676 Jun 16 15:18 ca-key.pem
  119. -rw-r--r--. 1 mysql mysql 1112 Jun 16 15:18 ca.pem
  120. -rw-r--r--. 1 mysql mysql 1112 Jun 16 15:18 client-cert.pem
  121. -rw-------. 1 mysql mysql 1676 Jun 16 15:18 client-key.pem
  122. -rw-r-----. 1 mysql mysql 196608 Jun 16 15:18 '#ib_16384_0.dblwr'
  123. -rw-r-----. 1 mysql mysql 8585216 Jun 16 15:18 '#ib_16384_1.dblwr'
  124. -rw-r-----. 1 mysql mysql 5931 Jun 16 15:18 ib_buffer_pool
  125. -rw-r-----. 1 mysql mysql 12582912 Jun 16 15:18 ibdata1
  126. -rw-r-----. 1 mysql mysql 50331648 Jun 16 15:18 ib_logfile0
  127. -rw-r-----. 1 mysql mysql 50331648 Jun 16 15:18 ib_logfile1
  128. drwxr-x---. 2 mysql mysql 6 Jun 16 15:18 '#innodb_temp'
  129. drwxr-x---. 2 mysql mysql 143 Jun 16 15:18 mysql
  130. -rw-r-----. 1 mysql mysql 25165824 Jun 16 15:18 mysql.ibd
  131. drwxr-x---. 2 mysql mysql 8192 Jun 16 15:18 performance_schema
  132. -rw-------. 1 mysql mysql 1680 Jun 16 15:18 private_key.pem
  133. -rw-r--r--. 1 mysql mysql 452 Jun 16 15:18 public_key.pem
  134. -rw-r--r--. 1 mysql mysql 1112 Jun 16 15:18 server-cert.pem
  135. -rw-------. 1 mysql mysql 1676 Jun 16 15:18 server-key.pem
  136. drwxr-x---. 2 mysql mysql 28 Jun 16 15:18 sys
  137. -rw-r-----. 1 mysql mysql 16777216 Jun 16 15:18 undo_001
  138. -rw-r-----. 1 mysql mysql 16777216 Jun 16 15:18 undo_002
  139. [root@localhost data]# cd ..
  140. [root@localhost mysql]#
  141. [root@localhost mysql]#
  142. [root@localhost mysql]#
  143. ####无法采用这种方式启动
  144. [root@localhost mysql]# systemctl start mysqld
  145. Failed to start mysqld.service: Unit mysqld.service not found.
  146. [root@localhost mysql]#
  147. [root@localhost mysql]#
  148. ###启动MySQL
  149. [root@localhost mysql]# bin/mysqld_safe --user=mysql &
  150. [1] 1816
  151. [root@localhost mysql]# Logging to '/usr/local/mysql/data/localhost.localdomain.err'.
  152. 2021-06-16T07:22:46.224871Z mysqld_safe Starting mysqld daemon with databases from /usr/local/mysql/data
  153. [root@localhost mysql]#
  154. [root@localhost mysql]#
  155. ###查看MySQL是否启动
  156. [root@localhost mysql]# ps -ef | grep mysql
  157. root 1816 1523 0 15:22 pts/0 00:00:00 /bin/sh bin/mysqld_safe --user=mysql
  158. 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
  159. root 1951 1523 0 15:22 pts/0 00:00:00 grep --color=auto mysql
  160. [root@localhost mysql]#
  161. [root@localhost mysql]#
  162. [root@localhost mysql]#
  163. [root@localhost mysql]#
  164. [root@localhost mysql]# cd bin
  165. ###进入MySQL发现错误
  166. [root@localhost bin]# ./mysql -uroot -p
  167. ./mysql: error while loading shared libraries: libtinfo.so.5: cannot open shared object file: No such file or directory
  168. [root@localhost bin]#
  169. [root@localhost bin]#
  170. ####创建符号链接
  171. [root@localhost bin]# sudo ln -s /usr/lib64/libtinfo.so.6.1 /usr/lib64/libtinfo.so.5
  172. [root@localhost bin]#
  173. [root@localhost bin]#
  174. [root@localhost bin]# ./mysql -uroot -p
  175. Enter password:
  176. ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
  177. ###将产生的临时密码已下面方式进行登录
  178. [root@localhost bin]# ./mysql -uroot -p2M78VcFDrf+G
  179. mysql: [Warning] Using a password on the command line interface can be insecure.
  180. Welcome to the MySQL monitor. Commands end with ; or \g.
  181. Your MySQL connection id is 9
  182. Server version: 8.0.25
  183. Copyright (c) 2000, 2021, Oracle and/or its affiliates.
  184. Oracle is a registered trademark of Oracle Corporation and/or its
  185. affiliates. Other names may be trademarks of their respective
  186. owners.
  187. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  188. mysql> show databases;
  189. ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
  190. mysql>
  191. mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
  192. Query OK, 0 rows affected (0.00 sec)
  193. mysql> show databases;
  194. +--------------------+
  195. | Database |
  196. +--------------------+
  197. | information_schema |
  198. | mysql |
  199. | performance_schema |
  200. | sys |
  201. +--------------------+
  202. 4 rows in set (0.00 sec)
  203. mysql> use mysql
  204. Reading table information for completion of table and column names
  205. You can turn off this feature to get a quicker startup with -A
  206. Database changed
  207. mysql>
  208. mysql>
  209. mysql> select user,password,host from user;
  210. ERROR 1054 (42S22): Unknown column 'password' in 'field list'
  211. mysql> select user,host from user;
  212. +------------------+-----------+
  213. | user | host |
  214. +------------------+-----------+
  215. | mysql.infoschema | localhost |
  216. | mysql.session | localhost |
  217. | mysql.sys | localhost |
  218. | root | localhost |
  219. +------------------+-----------+
  220. 4 rows in set (0.00 sec)
  221. mysql> update user set host = '%' where user = 'root';
  222. Query OK, 1 row affected (0.00 sec)
  223. Rows matched: 1 Changed: 1 Warnings: 0
  224. mysql> select user,host from user;
  225. +------------------+-----------+
  226. | user | host |
  227. +------------------+-----------+
  228. | root | % |
  229. | mysql.infoschema | localhost |
  230. | mysql.session | localhost |
  231. | mysql.sys | localhost |
  232. +------------------+-----------+
  233. 4 rows in set (0.00 sec)
  234. mysql>
  235. mysql>
  236. mysql> select version();
  237. +-----------+
  238. | version() |
  239. +-----------+
  240. | 8.0.25 |
  241. +-----------+
  242. 1 row in set (0.00 sec)
  243. ###刷新权限 就可以远程登陆了
  244. mysql> flush privileges;
  245. Query OK, 0 rows affected (0.00 sec)
  246. mysql>
  247. mysql>

配置防火墙


    //查看防火墙的状态
    [root@localhost ~]#  firewall-cmd --state
    running
    //关闭防火墙
    [root@localhost ~]# systemctl stop firewalld.service
    [root@localhost ~]#  firewall-cmd --state
    not running


    //查看3306端口是否开启
    firewall-cmd --query-port=3306/tcp

    ###开启防火墙设置端口
    systemctl start firewalld.service

    #### zone --作用域
    #### add-port=80/tcp--添加端口,格式为:端口/通讯协议
    #### permanent--永久生效,没有此参数重启后失效
    firewall-cmd --zone=public --add-port=3306/tcp --permanent
    ####开启3306端口后,workbench或naivcat 就能连接到MySQL数据库了

    ####重启防火墙
    firewall-cmd --reload

设置开机自启动


        //配置环境变量和系统自启动
    配置mysql环境变量:将mysql的bin目录加入PATH环境变量,编辑 /etc/profile文件:

    vim /etc/profile

    #最后添加下面一行
    ###PATH与=不能有空格
    export PATH=$PATH:/usr/local/mysql/bin

    #执行命令使其生效:
    source /etc/profile

    ###用 export 命令查看PATH值:

    echo $PATH

    ####设置开机自启动:

    ####复制启动脚本到资源目录:

    [root@localhost mysql]# cp ./support-files/mysql.server /etc/rc.d/init.d/mysqld

    ####增加 mysqld 服务控制脚本执行权限:

    [root@localhost mysql]# chmod +x /etc/rc.d/init.d/mysqld

    ####将 mysqld 服务加入到系统服务:

    [root@localhost mysql]# chkconfig --add mysqld

    ####检查mysqld服务是否已经生效:

    [root@localhost mysql]# chkconfig --list mysqld

    ####命令输出类似下面的结果:

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

    表明mysqld服务已经生效,在2、3、4、5运行级别随系统启动而自动启动,以后可以使用 service 命令控制 mysql 的启动和停止。

    ####启动指令
    service mysqld stop

查看MySQL配置文件

#### MySQL配置文件加载顺序
[root@localhost ~]# mysql --help|grep 'my.cnf'
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf /usr/local/mysql/etc/my.cnf ~/.my.cnf



####查看是否使用了MySQL配置文件 好像没用
[root@localhost ~]# ps aux|grep mysql|grep 'my.cnf'
[root@localhost ~]#

配置My.cnf

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

[client]
port = 3306
#根据实际情况调整mysql.sock配置
socket = /tmp/mysql.sock

[mysqld]
#Mysql服务的唯一编号 每个mysql服务Id需唯一
server-id = 1

#服务端口号 默认3306
port = 3306

#mysql安装根目录
basedir = /usr/local/mysql

#mysql数据文件所在位置
datadir = /usr/local/mysql/data

#pid
pid-file = /usr/local/mysql/data/mysql.pid

#设置socke文件所在目录
socket = /tmp/mysql.sock

#设置临时目录
tmpdir = /tmp

# 用户
user = mysql

# 允许访问的IP网段
bind-address = 0.0.0.0

# 跳过密码登录
#skip-grant-tables

#主要用于MyISAM存储引擎,如果多台服务器连接一个数据库则建议注释下面内容
skip-external-locking

#只能用IP地址检查客户端的登录,不用主机名
skip_name_resolve = 1

#事务隔离级别,默认为可重复读,mysql默认可重复读级别(此级别下可能参数很多间隙锁,影响性能)
transaction_isolation = REPEATABLE-READ

#数据库默认字符集,主流字符集支持一些特殊表情符号(特殊表情符占用4个字节)
character-set-server = utf8mb4

#数据库字符集对应一些排序等规则,注意要和character-set-server对应
collation-server = utf8mb4_general_ci

#设置client连接mysql时的字符集,防止乱码
init_connect='SET NAMES utf8mb4'

#是否对sql语句大小写敏感,1表示不敏感
lower_case_table_names = 1

#最大连接数
max_connections = 400

#最大错误连接数
max_connect_errors = 1000

#TIMESTAMP如果没有显示声明NOT NULL,允许NULL值
explicit_defaults_for_timestamp = true

#SQL数据包发送的大小,如果有BLOB对象建议修改成1G
max_allowed_packet = 128M


#MySQL连接闲置超过一定时间后(单位:秒)将会被强行关闭
#MySQL默认的wait_timeout  值为8个小时, interactive_timeout参数需要同时配置才能生效
interactive_timeout = 1800
wait_timeout = 1800

#内部内存临时表的最大值 ,设置成128M。
#比如大数据量的group by ,order by时可能用到临时表,
#超过了这个值将写入磁盘,系统IO压力增大
tmp_table_size = 134217728
max_heap_table_size = 134217728



#数据库错误日志文件
log_error = error.log

#慢查询sql日志设置
slow_query_log = 1
slow_query_log_file = slow.log

#检查未使用到索引的sql
log_queries_not_using_indexes = 1

#针对log_queries_not_using_indexes开启后,记录慢sql的频次、每分钟记录的条数
log_throttle_queries_not_using_indexes = 5

#作为从库时生效,从库复制中如何有慢sql也将被记录
log_slow_slave_statements = 1

#慢查询执行的秒数,必须达到此值可被记录
long_query_time = 8

#检索的行数必须达到此值才可被记为慢查询
min_examined_row_limit = 100

#mysql binlog日志文件保存的过期时间,过期后自动删除
expire_logs_days = 5

1,删除data目录

cd /usr/local/mysql
rm -rf data

2,在/etc/ 创建my.cnf

3,初始化MySQL

bin/mysqld --initialize --user=mysql

临时密码会在 usr/local/mysql/data/error.log 中

4,启动MySQL

bin/mysqld_safe --user=mysql &

5,进入MySQL

./mysql -uroot -p

6,验证配置文件

比如 大小写的问题