腾讯云服务器centos安装mysql

先卸载自带的mysql或mariadb
然后下载mysql 建议安装在opt目录下
中间有部分冗余操作

[root@VM-8-4-centos ~]# rpm -qa|grep mysql
[root@VM-8-4-centos ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.68-1.el7.x86_64
# 使用yum卸载
[root@VM-8-4-centos ~]# yum remove mariadb
# 使用rpm卸载 查到几个卸几个
[root@VM-8-4-centos ~]# rpm -e --nodeps mariadb-libs-5.5.68-1.el7.x86_64
[root@VM-8-4-centos ~]# rm -rf /etc/my.cnf
[root@VM-8-4-centos ~]# whereis mysql
mysql: /usr/lib64/mysql
[root@VM-8-4-centos ~]# rm -rf /usr/lib64/mysql
mysql:[root@VM-8-4-centos ~]# find / -name mysql
/etc/selinux/targeted/active/modules/100/mysql
[root@VM-8-4-centos ~]# rm -rf /etc/selinux/targeted/active/modules/100/mysql
[root@VM-8-4-centos ~]# cat /etc/group | grep mysql
[root@VM-8-4-centos ~]# cat /etc/passwd |grep mysql
# 确认全部卸载完
# 创建mysql用户组
[root@VM-8-4-centos ~]# groupadd mysql
[root@VM-8-4-centos ~]# useradd -r -g mysql mysql
[root@VM-8-4-centos ~]# cd /opt
[root@VM-8-4-centos opt]# mkdir mysql
[root@VM-8-4-centos opt]# cd mysql/
# 下载
[root@VM-8-4-centos mysql]# wget https://cdn.mysql.com//Downloads/MySQL-8.0/mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz
# 解压
[root@VM-8-4-centos mysql]# tar -xvf mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz 
# 删除源文件(也可以不删留着下次安装)
[root@VM-8-4-centos mysql]# rm -rf ./mysql-8.0.32-linux-glibc2.12-x86_64.tar.xz 
# 重命名文件(此处有点强迫症了,可以直接将文件内容移动过来的,此处有冗余操作仅供参考)
[root@VM-8-4-centos mysql]# mv ./mysql-8.0.32-linux-glibc2.12-x86_64 /opt/mysql8
[root@VM-8-4-centos mysql]# cd ../
[root@VM-8-4-centos opt]# rm -rf ./mysql/
[root@VM-8-4-centos opt]# mv mysql8/ mysql/
[root@VM-8-4-centos opt]# cd mysql
# 以上操作自行参考
# 创建数据库文件夹
[root@VM-8-4-centos mysql]# mkdir data
# 授权用户组
[root@VM-8-4-centos mysql]# chown -R mysql.mysql /opt/mysql
[root@VM-8-4-centos mysql]# cd bin

# 初始化mysqld 记住生成的初始密码
[root@VM-8-4-centos bin]# ./mysqld --user=mysql --basedir=/opt/mysql --datadir=/opt/mysql/data/ --initialize
2023-02-02T07:11:47.072730Z 6 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: ******
# 记住上面密码
# 创建配置文件
[root@VM-8-4-centos bin]# vim /etc/my.cnf
[mysqld]
# skip-grant-tables
# 设置3306端口
port=3306
# 设置mysql的安装目录
basedir=/opt/mysql/
# 设置mysql数据库的数据的存放目录
datadir=/opt/mysql/data/
socket=/tmp/mysql.sock
# 允许最大连接数
max_connections=10000
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=10
# 服务端使用的字符集默认为UTF8
character-set-server=utf8mb4
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 默认使用“mysql_native_password”插件认证
default_authentication_plugin=mysql_native_password
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8mb4
[root@VM-8-4-centos bin]# cd ../
[root@VM-8-4-centos mysql]# cp -a ./support-files/mysql.server /etc/init.d/mysql
[root@VM-8-4-centos mysql]# chmod +x /etc/init.d/mysql
[root@VM-8-4-centos mysql]# chkconfig --add mysql
# 启动mysql
[root@VM-8-4-centos mysql]# service mysql start
Starting MySQL.Logging to '/opt/mysql/data/VM-8-4-centos.err'.
. SUCCESS! 
# 可以再次查看启动状态
[root@VM-8-4-centos mysql]# service mysql status
 SUCCESS! MySQL running (2054)
[root@VM-8-4-centos mysql]# 
# 将mysql命令添加到服务 
[root@VM-8-4-centos mysql]# ln -s /opt/mysql/bin/mysql /usr/bin
# 登录
[root@VM-8-4-centos mysql]# mysql -uroot -p
Enter password: 

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '****';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
mysql> use mysql;

mysql> update user set host='%' where user='root'; 
mysql> flush privileges;
mysql> quit;

# 启用端口发现报错
[root@VM-8-4-centos mysql]# firewall-cmd --add-port=3306/tcp --permanent
FirewallD is not running
# 启动防火墙
[root@VM-8-4-centos mysql]# systemctl start firewalld.service
# 设计开机启动防火墙
[root@VM-8-4-centos mysql]# systemctl enable firewalld.service
# 启用端口
[root@VM-8-4-centos mysql]# firewall-cmd --add-port=3306/tcp --permanent
# 重置防火墙 立即生效
[root@VM-8-4-centos mysql]#  firewall-cmd --reload
# 查看防火墙开放状态
[root@VM-8-4-centos mysql]# systemctl status firewalld
# 查看端口开放状态
[root@VM-8-4-centos mysql]# firewall-cmd --list-all


你可能感兴趣的:(linux,服务器,centos,腾讯云,mysql,linux)