安装包
https://downloads.mysql.com/archives/community/
mysql-8.0.39-linux-glibc2.17-x86_64.tar.xz
1.卸载MariaDB
查看
bash
rpm -qa|grep mariadb
卸载
可能名称不一样,记得替换
bash
rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
rpm -qa|grep mariadb 执行验证有没有卸载成功
查看libaio 和 numactl
bash
[root@ncayu8847 ~]# rpm -qa|grep libaio
libaio-0.3.109-13.el7.x86_64
[root@ncayu8847 ~]#
[root@ncayu8847 ~]#
[root@ncayu8847 ~]# rpm -qa|grep numactl
numactl-libs-2.0.12-5.el7.x86_64
[root@ncayu8847 ~]#
如果没有则需要安装
bash
yum -y install libaio //安装libaio
yum -y install numactl //安装numactl
安装 MySQL
1.上传MySQL 安装包
mysql-8.0.39-linux-glibc2.17-x86_64.tar.xz
2.解压
注:本文安装包上传到了 /usr/local/ 目录下
进入安装包目录
bash
cd /usr/local/
解压文件到指定目录
bash
tar -pvxf mysql-8.0.39-linux-glibc2.17-x86_64.tar.xz -C /usr/local/
3.重命名文件夹
将解压后的文件夹重命名为mysql-8.0.39
bash
mv mysql-8.0.39-linux-glibc2.17-x86_64 mysql-8.0.39
创建软连接
bash
ln -s mysql-8.0.39 mysql
4.创建存储数据文件
创建 /usr/localysql/data文件夹
bash
mkdir mysql/data
5.设置用户组并赋权
创建用户组
bash
groupadd mysql
创建用户
-r:创建系统用户
-g:指定用户组
bash
useradd -r -g mysql mysql
更改文件属主 和权限
bash
chown -R mysql:mysql /usr/local/mysql/
bash
chmod -R 755 /usr/local/mysql/
6.初始化MySQL
进入MySQL的bin目录
bash
cd /usr/local/mysql/bin/
初始化数据库
bash
./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql
设置是否对sql语句大小写敏感,1表示不敏感
lower_case_table_names = 1
注意:#如果要设置lower_case_table_names可以在初始化里面设置 ./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql --lower_case_table_names=1
设置是否对sql语句大小写敏感执行这个:
bash
./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql --lower_case_table_names=1
初始化完成后会打印一个随机密码,登录的时候会用到
bash
[root@ncayu8847 bin]# ./mysqld --initialize --user=mysql --datadir=/usr/local/mysql/data --basedir=/usr/local/mysql --lower_case_table_names=1
2024-10-24T13:39:07.148956Z 0 [System] [MY-013169] [Server] /usr/local/mysql-8.0.39/bin/mysqld (mysqld 8.0.39) initializing of server in progress as process 13554
2024-10-24T13:39:07.160773Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2024-10-24T13:39:08.410704Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2024-10-24T13:39:10.989324Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: !fn?Puqa7-i+
随机密码:!fn?Puqa7-i+
7.配置参数文件 my.cnf
bash
vi /etc/my.cnf
配置文件修改为以下内容,也可以根据自己需要设置参数。
bash
[client]
port = 3306
socket = /usr/local/mysql/data/mysql.sock
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect = 'SET NAMES utf8mb4'
port = 3306
socket = /usr/local/mysql/data/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
datadir = /usr/local/mysql/data
lower_case_table_names=1
[mysqldump]
quick
max_allowed_packet = 16M
[mysql]
no-auto-rehash
[myisamchk]
key_buffer_size = 20M
sort_buffer_size = 20M
read_buffer = 2M
write_buffer = 2M
[mysqlhotcopy]
interactive-timeout
配置后修改 /etc/my.cnf 的权限为644
bash
chmod 644 /etc/my.cnf
启动 mysql 报错提示: my_print_defaults: [Warning] World-writable config file '/etc/my.cnf' is ignored.
操作数据库提示警告World-writable config file '/usr/local/mysql/my.cnf' is ignored
检查发现my.cnf权限过高【777】,将权限更改为644即可。
8.启动MySQL
bash
/usr/local/mysql/support-files/mysql.server start
[root@ncayu8847 mysql]# /usr/local/mysql/support-files/mysql.server start
Starting MySQL... SUCCESS!
[root@ncayu8847 mysql]#
9.设置软连接,并重启MySQL
bash
ln -s /usr/local/mysql/support-files/mysql.server /etc/init.d/mysql
ln -s /usr/local/mysql/bin/mysql /usr/bin/mysql
ln -s /usr/local/mysql/mysql.sock /var/mysql.sock
service mysql restart
10.登录并更改密码
bash
[root@ncayu8847 mysql]# mysql -uroot -p
Enter password: 输入初始化随机密码
我的mysql 随机密码: !fn?Puqa7-i+
第一次登录需要修改账号的密码
bash
alter user 'root'@'localhost' identified by 'admin@123';
11.开放远程连接
如果不开放远程连接,root 用户只能在本地连接,当然,你也可以创建新的数据库账号。
bash
use mysql;
update user set user.Host='%' where user.User='root';
flush privileges;
flush privileges; 表示刷新权限
MySQL 进程
12.连接工具测试连接MySQL
防火墙开放 3306 端口
bash
firewall-cmd --add-port=3306/tcp
firewall-cmd --add-port=3306/tcp --permanent
firewall-cmd --list-all
使用 DBeaver 工具连接数据库
连接成功
13.MySQL启动和停止
bash
# 启动
service mysql start
systemctl start mysql
#停止
service mysql stop
systemctl stop mysql
# 重启
service mysql restart
systemctl restart mysql
# 查看状态
service mysql status
systemctl status mysql
设置开机自启动
将服务文件拷贝到 /etc/init.d下,并重命名为mysqld
bash
cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld
赋予可执行权限
bash
# 赋予可执行权限
chmod +x /etc/init.d/mysqld
# 添加服务
chkconfig --add mysqld
# 显示服务列表
chkconfig --list
注:如果看到mysqld的服务,并且3,4,5都是on的话则成功,如果是off,则执行以下命令
bash
chkconfig --level 345 mysqld on
重启系统
bash
reboot
重启后查看mysql是否开机自启动
bash
ps -ef|grep mysql
可以开机自启动,验证通过。
报错处理:Public Key Retrieval is not allowed
连接mysql时报错:Public Key Retrieval is not allowed(不允许公钥检索)
解决方法:连接设置------驱动属性------allowPublicKeyRetrieval=false(这里的运输公钥检索是默认关闭的,需要把它开启),改为allowPublicKeyRetrieval=true即可。