一、环境
CentOS 7.9
MySQL 8.0.32
二、下载地址
MySQL :: Download MySQL Community Server
https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
三、安装MySQL8.0
1.上传,解压,重命名
[root@bogon ~]# cd /opt
[root@bogon opt]# tar xvJf mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
[root@bogon opt]# mv mysql-8.0.32-linux-glibc2.12-x86_64 mysql-8.0.32
2.创建用户
-s /sbin/nologin 指定用户登入后所使用的shell。默认值为/bin/bash。/sbin/nologin,无法使用shell
-M:不要自动建立用户的登入目录。
[root@bogon opt]# useradd -s /sbin/nologin -M mysql
[root@bogon opt]# id mysql
uid=1001(mysql) gid=1001(mysql) 组=1001(mysql)
[root@bogon opt]# chown -R mysql.mysql /opt/mysql-8.0.32
3.防火墙打开端口
firewall-cmd --permanent --zone=public --add-port=3308/tcp
firewall-cmd --reload
4.创建配置文件my.cnf
[root@bogon opt]# cd mysql-8.0.32/
[root@bogon mysql-8.0.32]# vi my.cnf
[client]
port=3306
[mysql]
default-character-set=utf8mb4
[mysqld]
user=mysql
# 服务器侦听TCP/IP连接的端口号。
port=3306
# MySQL安装基础目录的路径。
basedir=/opt/mysql-8.0.32
# MySQL服务器数据目录的路径。最好将datadir值指定为绝对路径。
datadir=/opt/mysql-8.0.32/data
# 服务器默认字符集。如果设置此变量,还应设置collation_server以指定字符集的排序规则。
character-set-server=utf8mb4
# 服务器的默认排序规则。
collation_server=utf8mb4_0900_ai_ci
# 表的默认存储引擎。此变量仅为永久表设置存储引擎。
# 要为临时表设置存储引擎,请设置default_tmp_storage_engine系统变量。
default-storage-engine=InnoDB
# 允许的最大同时客户端连接数。(1-100000)
max_connections=151
# 在max_connect_errors之后,来自主机的连续连接请求在没有成功连接的情况下被中断,服务器将阻止该主机进行进一步的连接。
max_connect_errors=100
# 默认的身份验证插件。(mysql_native_password,sha256_password,caching_sha2_password)
default_authentication_plugin=caching_sha2_password
# soket文件,本地连接时使用
socket=/opt/mysql-8.0.32/data/mysql.sock
# 建议禁用符号链接以防止各种安全风险
symbolic-links=0
# 错误日志
log-error=/opt/mysql-8.0.32/data/mysqld.log
# pid文件
pid-file=/opt/mysql-8.0.32/data/mysqld.pid
# 表名不区分大小写(修改后需要重新初始化mysql)
lower_case_table_names=1
# 设置sql_mode(没有NO_AUTO_CREATE_USER了)
sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
# 一个数据包或任何生成/中间字符串的最大大小,默认值为4MB,最大为1024M
max_allowed_packet = 32M
# innodb缓冲池大小
innodb_buffer_pool_size=1G
# innodb缓冲池块大小
innodb_buffer_pool_chunk_size=128M
# innodb缓冲池实例数
innodb_buffer_pool_instances=8
5.初始化数据库
--initialize-insecure 密码为空
[root@bogon mysql-8.0.32]# /opt/mysql-8.0.32/bin/mysqld --initialize-insecure --user=mysql --basedir=/opt/mysql-8.0.32 --datadir=/opt/mysql-8.0.32/data --lower_case_table_names=1
2023-02-24T08:36:45.945347Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
2023-02-24T08:36:45.945711Z 0 [System] [MY-013169] [Server] /opt/mysql-8.0.32/bin/mysqld (mysqld 8.0.32) initializing of server in progress as process 27729
2023-02-24T08:36:45.960016Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2023-02-24T08:36:49.910026Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2023-02-24T08:36:52.185713Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
如果my.cnf配置了lower_case_table_names=1,初始化时也叫加上--lower_case_table_names=1
否则报错:
tail -f /opt/mysql-8.0.32/data/mysqld.log
[Server] Different lower_case_table_names settings for server ('1') and data dictionary ('0').
如果报错:
error while loading shared libraries: libaio.so.1: cannot open shared object file: No such file or directory
安装libiao
yum install libaio
6.创建service文件
[root@bogon mysql-8.0.32]# vi /usr/lib/systemd/system/mysqld80.service
[Unit]
Description=MySQL Server 8.0
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Service]
User=mysql
Group=mysql
LimitNOFILE=65536
ExecStart=/opt/mysql-8.0.32/bin/mysqld --defaults-file=/opt/mysql-8.0.32/my.cnf
[Install]
WantedBy=multi-user.target
7.启动服务
[root@bogon mysql-8.0.32]# systemctl start mysqld80
[root@dev1 mysql-8.0.20]# netstat -lntup|grep mysqld
tcp6 0 0 :::33060 :::* LISTEN 9129/mysqld
tcp6 0 0 :::3308 :::* LISTEN 9129/mysqld
8.设置自启动
[root@bogon mysql-8.0.32]# systemctl enable mysqld80
9.查看日志
tail -f /opt/mysql-8.0.32/data/mysqld.log
10.客户端登录
[root@bogon mysql-8.0.32]# /opt/mysql-8.0.32/bin/mysql -S /opt/mysql-8.0.32/data/mysql.sock
[root@bogon mysql-8.0.32]# /opt/mysql-8.0.32/bin/mysql -h 127.0.0.1 -P 3306 -u root -p
mysql> select @@version;
mysql> show variables like 'port';
11.修改密码,允许远程登录
[root@bogon mysql-8.0.32]# /opt/mysql-8.0.32/bin/mysql -h 127.0.0.1 -P 3306 -u root -p
mysql> use mysql
mysql> select user,authentication_string,host from user;
mysql> ALTER user 'root'@'localhost' IDENTIFIED BY 'root';
mysql> update user set host = '%' where user = 'root';
mysql> FLUSH PRIVILEGES;
12.停止mysql
[root@bogon ~]# /opt/mysql-8.0.32/bin/mysqladmin shutdown -h 127.0.0.1
[root@bogon ~]# /opt/mysql-8.0.32/bin/mysqladmin shutdown -h 127.0.0.1 -P 3306 -p
四、主从复制
五、集群
六、报错处理
如果客户端连接提示找不到mysql.sock,请使用localhost或27.0.0.1
/opt/mysql-8.0.32/bin/mysql -h 127.0.0.1 -u root -p
/opt/mysql-8.0.32/bin/mysql -h 127.0.0.1 -P3306 -u root -p