参考 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
###下载安装包
wget https://dev.mysql.com/get/Downloads/MySQL-8.0/mysql-8.0.25-linux-glibc2.12-x86_64.tar.xz
### 对MySQL进行重命名
[root@localhost local]# mv mysql-8.0.25-linux-glibc2.12-x86_64/ mysql
[root@localhost local]# ll
total 0
drwxr-xr-x. 2 root root 6 Nov 3 2020 bin
drwxr-xr-x. 2 root root 6 Nov 3 2020 etc
drwxr-xr-x. 2 root root 6 Nov 3 2020 games
drwxr-xr-x. 2 root root 6 Nov 3 2020 include
drwxr-xr-x. 2 root root 6 Nov 3 2020 lib
drwxr-xr-x. 3 root root 17 Jun 16 02:59 lib64
drwxr-xr-x. 2 root root 6 Nov 3 2020 libexec
drwxr-xr-x. 9 root root 129 Jun 16 03:16 mysql
drwxr-xr-x. 2 root root 6 Nov 3 2020 sbin
drwxr-xr-x. 5 root root 49 Jun 16 02:59 share
drwxr-xr-x. 2 root root 6 Nov 3 2020 src
[root@localhost local]#
[root@localhost local]#
###创建用户组
[root@localhost local]# groupadd mysql
[root@localhost local]#
[root@localhost local]#
[root@localhost local]#
####添加MySQL用户
[root@localhost local]# useradd -r -g mysql -s /bin/false mysql
[root@localhost local]# cd mysql/
[root@localhost mysql]#
[root@localhost mysql]#
[root@localhost mysql]#
[root@localhost mysql]# mkdir mysql-files
[root@localhost mysql]#
[root@localhost mysql]#
[root@localhost mysql]#
[root@localhost mysql]# ll
total 284
drwxr-xr-x. 2 7161 31415 4096 Apr 23 14:56 bin
drwxr-xr-x. 2 7161 31415 55 Apr 23 14:56 docs
drwxr-xr-x. 3 7161 31415 282 Apr 23 14:56 include
drwxr-xr-x. 6 7161 31415 201 Apr 23 14:56 lib
-rw-r--r--. 1 7161 31415 274942 Apr 23 11:06 LICENSE
drwxr-xr-x. 4 7161 31415 30 Apr 23 14:56 man
drwxr-xr-x. 2 root root 6 Jun 16 03:17 mysql-files
-rw-r--r--. 1 7161 31415 666 Apr 23 11:06 README
drwxr-xr-x. 28 7161 31415 4096 Apr 23 14:56 share
drwxr-xr-x. 2 7161 31415 77 Apr 23 14:56 support-files
[root@localhost mysql]#
[root@localhost mysql]#
####赋予MySQL用户MySQL用户组 对mysql-files的权限
[root@localhost mysql]# chown mysql:mysql mysql-files
[root@localhost mysql]#
[root@localhost mysql]#
[root@localhost mysql]#
[root@localhost mysql]# ll
total 284
drwxr-xr-x. 2 7161 31415 4096 Apr 23 14:56 bin
drwxr-xr-x. 2 7161 31415 55 Apr 23 14:56 docs
drwxr-xr-x. 3 7161 31415 282 Apr 23 14:56 include
drwxr-xr-x. 6 7161 31415 201 Apr 23 14:56 lib
-rw-r--r--. 1 7161 31415 274942 Apr 23 11:06 LICENSE
drwxr-xr-x. 4 7161 31415 30 Apr 23 14:56 man
drwxr-xr-x. 2 mysql mysql 6 Jun 16 03:17 mysql-files
-rw-r--r--. 1 7161 31415 666 Apr 23 11:06 README
drwxr-xr-x. 28 7161 31415 4096 Apr 23 14:56 share
drwxr-xr-x. 2 7161 31415 77 Apr 23 14:56 support-files
[root@localhost mysql]#
[root@localhost mysql]#
###赋予MySQL用户的执行权限
[root@localhost mysql]# chmod 750 mysql-files
[root@localhost mysql]#
[root@localhost mysql]#
[root@localhost mysql]# ll
total 284
drwxr-xr-x. 2 7161 31415 4096 Apr 23 14:56 bin
drwxr-xr-x. 2 7161 31415 55 Apr 23 14:56 docs
drwxr-xr-x. 3 7161 31415 282 Apr 23 14:56 include
drwxr-xr-x. 6 7161 31415 201 Apr 23 14:56 lib
-rw-r--r--. 1 7161 31415 274942 Apr 23 11:06 LICENSE
drwxr-xr-x. 4 7161 31415 30 Apr 23 14:56 man
drwxr-x---. 2 mysql mysql 6 Jun 16 03:17 mysql-files
-rw-r--r--. 1 7161 31415 666 Apr 23 11:06 README
drwxr-xr-x. 28 7161 31415 4096 Apr 23 14:56 share
drwxr-xr-x. 2 7161 31415 77 Apr 23 14:56 support-files
[root@localhost mysql]#
[root@localhost mysql]#
###执行MySQL的初始化 将会生成一个data文件夹 ,最后一行生成一个temporary密码
[root@localhost mysql]# bin/mysqld --initialize --user=mysql
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
2021-06-16T07:18:33.521073Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-06-16T07:18:34.461153Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-06-16T07:18:35.564759Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 2M78VcFDrf+G
[root@localhost mysql]#
[root@localhost mysql]#
[root@localhost mysql]# cd bin
[root@localhost bin]#
[root@localhost bin]#
[root@localhost bin]# ps -ef | grep mysql
root 1734 1523 0 03:19 pts/0 00:00:00 grep --color=auto mysql
[root@localhost mysql]# ll
total 288
drwxr-xr-x. 2 7161 31415 4096 Apr 24 02:56 bin
drwxr-x---. 6 mysql mysql 4096 Jun 16 15:18 data
drwxr-xr-x. 2 7161 31415 55 Apr 24 02:56 docs
drwxr-xr-x. 3 7161 31415 282 Apr 24 02:56 include
drwxr-xr-x. 6 7161 31415 201 Apr 24 02:56 lib
-rw-r--r--. 1 7161 31415 274942 Apr 23 23:06 LICENSE
drwxr-xr-x. 4 7161 31415 30 Apr 24 02:56 man
drwxr-x---. 2 mysql mysql 6 Jun 16 15:17 mysql-files
-rw-r--r--. 1 7161 31415 666 Apr 23 23:06 README
drwxr-xr-x. 28 7161 31415 4096 Apr 24 02:56 share
drwxr-xr-x. 2 7161 31415 77 Apr 24 02:56 support-files
[root@localhost mysql]#
[root@localhost mysql]#
[root@localhost mysql]# cd data
[root@localhost data]# ll
total 176568
-rw-r-----. 1 mysql mysql 56 Jun 16 15:18 auto.cnf
-rw-------. 1 mysql mysql 1676 Jun 16 15:18 ca-key.pem
-rw-r--r--. 1 mysql mysql 1112 Jun 16 15:18 ca.pem
-rw-r--r--. 1 mysql mysql 1112 Jun 16 15:18 client-cert.pem
-rw-------. 1 mysql mysql 1676 Jun 16 15:18 client-key.pem
-rw-r-----. 1 mysql mysql 196608 Jun 16 15:18 '#ib_16384_0.dblwr'
-rw-r-----. 1 mysql mysql 8585216 Jun 16 15:18 '#ib_16384_1.dblwr'
-rw-r-----. 1 mysql mysql 5931 Jun 16 15:18 ib_buffer_pool
-rw-r-----. 1 mysql mysql 12582912 Jun 16 15:18 ibdata1
-rw-r-----. 1 mysql mysql 50331648 Jun 16 15:18 ib_logfile0
-rw-r-----. 1 mysql mysql 50331648 Jun 16 15:18 ib_logfile1
drwxr-x---. 2 mysql mysql 6 Jun 16 15:18 '#innodb_temp'
drwxr-x---. 2 mysql mysql 143 Jun 16 15:18 mysql
-rw-r-----. 1 mysql mysql 25165824 Jun 16 15:18 mysql.ibd
drwxr-x---. 2 mysql mysql 8192 Jun 16 15:18 performance_schema
-rw-------. 1 mysql mysql 1680 Jun 16 15:18 private_key.pem
-rw-r--r--. 1 mysql mysql 452 Jun 16 15:18 public_key.pem
-rw-r--r--. 1 mysql mysql 1112 Jun 16 15:18 server-cert.pem
-rw-------. 1 mysql mysql 1676 Jun 16 15:18 server-key.pem
drwxr-x---. 2 mysql mysql 28 Jun 16 15:18 sys
-rw-r-----. 1 mysql mysql 16777216 Jun 16 15:18 undo_001
-rw-r-----. 1 mysql mysql 16777216 Jun 16 15:18 undo_002
[root@localhost data]# cd ..
[root@localhost mysql]#
[root@localhost mysql]#
[root@localhost mysql]#
####无法采用这种方式启动
[root@localhost mysql]# systemctl start mysqld
Failed to start mysqld.service: Unit mysqld.service not found.
[root@localhost mysql]#
[root@localhost 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)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.00 sec)
mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
mysql>
mysql> select user,password,host from user;
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
mysql> select user,host from user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
| root | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
mysql> update user set host = '%' where user = 'root';
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select user,host from user;
+------------------+-----------+
| user | host |
+------------------+-----------+
| root | % |
| mysql.infoschema | localhost |
| mysql.session | localhost |
| mysql.sys | localhost |
+------------------+-----------+
4 rows in set (0.00 sec)
mysql>
mysql>
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.25 |
+-----------+
1 row in set (0.00 sec)
###刷新权限 就可以远程登陆了
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql>
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,验证配置文件
比如 大小写的问题