CentOS安装mysql

1、安装准备

1、查看是否安装了mysql服务(有则自行卸载删除)。

yum list installed mysql*
 
rpm –qa|grep mysql*

2、确定服务器的基础框架是arm架构还是x86架构。

uname -m

[root@dbserver mysql]# uname -m
x86_64

3、确定出你需要的系统版本。

以我的为例,是CentOS 7系统。(CentOS 版本是基于Red Hat 版本开发的)

cat /etc/redhat-release

[root@dbserver mysql]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) 

1、官网下载压缩文件

2、创建mysql安装目录

mkdir /usr/local/mysql

3、进入目录解压

cd /usr/local/mysql

tar -zxvf 安装包(我这里报错了)

解决办法:
tar -xvf 安装包

4、删除原有的mariadb,否则可能会报异常。

1、查看

rpm -qa|grep mariadb


2、卸载

rpm -e --nodeps mariadb-libs

5、安装rmp包

逐个安装,命令如下:

【必须安装】

rpm -ivh mysql-community-common-8.0.26-1.el8.x86_64.rpm

rpm -ivh mysql-community-libs-8.0.26-1.el8.x86_64.rpm

rpm -ivh mysql-community-client-8.0.26-1.el8.x86_64.rpm

rpm -ivh mysql-community-server-8.0.26-1.el8.x86_64.rpm

注意点:出现下面错误,则直接先将icu-date前者依赖者先下载,下载完成后,再下载后者

6、启动mysql

1、使用“systemctl”检测mysqld服务状态

systemctl status mysqld

2、启动mysqld服务

systemctl start mysql

7、后续工作

1、查看密码(密码后面会用到)

less /var/log/mysqld.log

2、登录mysql

这里笔者使用mysql -u root -p登录到mysql进行修改,当然也可以选择其他方式

[root@dbserver mysql]# mysql -u root -p
//这里密码就填写步骤一查看到的密码
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 8.0.33

Copyright (c) 2000, 2023, 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> 

3、重置密码('root'@'localhost'建议加上引号)

说明:MySQL 8.0及以上版本默认启用了密码策略插件,要求用户设置更强的密码以提高安全性。

您可以尝试使用更复杂的密码,包括数字、大小写字母和特殊字符,并确保密码长度至少为8个字符。

//密码不够安全
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'DFq159001';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

//密码包含数字、大小写字母和特殊字符
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '@DFq159001';
Query OK, 0 rows affected (0.04 sec)

mysql> 

8、参考文章

Linux CentOS 7 安装mysql的两种方式_centos7安装mysql_Escorts的博客-CSDN博客

linux解压时报错: gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error exit delayed_linux gzip: stdin: not in gzip format tar: child r_时光留夏的博客-CSDN博客

9、附录

Last login: Thu May  4 15:32:48 2023
[root@dbserver ~]# mkdir /usr/local/mysql
[root@dbserver ~]# cd /usr/local/mysql
[root@dbserver mysql]# cd ../
[root@dbserver local]# ls
bin  etc  games  include  lib  lib64  libexec  mysql  sbin  share  src
[root@dbserver local]# cd mysql/
[root@dbserver mysql]# ls
[root@dbserver mysql]# rpm -qa|grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
[root@dbserver mysql]# rpm -e --nodeps mariadb-libs
[root@dbserver mysql]# rpm -qa|grep mariadb
[root@dbserver mysql]# ls
[root@dbserver mysql]# ls
mysql-8.0.33-1.el7.x86_64.rpm-bundle.tar
[root@dbserver mysql]# tar -zxvf mysql-8.0.33-1.el7.x86_64.rpm-bundle.tar 

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now
[root@dbserver mysql]# tar -zxf mysql-8.0.33-1.el7.x86_64.rpm-bundle.tar 

gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now
[root@dbserver mysql]# tar -xvf mysql-8.0.33-1.el7.x86_64.rpm-bundle.tar 
mysql-community-client-8.0.33-1.el7.x86_64.rpm
mysql-community-client-plugins-8.0.33-1.el7.x86_64.rpm
mysql-community-common-8.0.33-1.el7.x86_64.rpm
mysql-community-debuginfo-8.0.33-1.el7.x86_64.rpm
mysql-community-devel-8.0.33-1.el7.x86_64.rpm
mysql-community-embedded-compat-8.0.33-1.el7.x86_64.rpm
mysql-community-icu-data-files-8.0.33-1.el7.x86_64.rpm
mysql-community-libs-8.0.33-1.el7.x86_64.rpm
mysql-community-libs-compat-8.0.33-1.el7.x86_64.rpm
mysql-community-server-8.0.33-1.el7.x86_64.rpm
mysql-community-server-debug-8.0.33-1.el7.x86_64.rpm
mysql-community-test-8.0.33-1.el7.x86_64.rpm
[root@dbserver mysql]# rpm -ivh mysql-community-common-8.0.33-1.el7.x86_64.rpm 
警告:mysql-community-common-8.0.33-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-common-8.0.33-1.e################################# [100%]
[root@dbserver mysql]# rpm -ivh mysql-community-libs-8.0.33-1.el7.x86_64.rpm 
警告:mysql-community-libs-8.0.33-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
错误:依赖检测失败:
        mysql-community-client-plugins = 8.0.33-1.el7 被 mysql-community-libs-8.0.33-1.el7.x86_64 需要
[root@dbserver mysql]# rpm -ivh mysql-community-client-8.0.33-1.el7.x86_64.rpm 
警告:mysql-community-client-8.0.33-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
错误:依赖检测失败:
        mysql-community-client-plugins = 8.0.33-1.el7 被 mysql-community-client-8.0.33-1.el7.x86_64 需要
        mysql-community-libs(x86-64) >= 8.0.11 被 mysql-community-client-8.0.33-1.el7.x86_64 需要
[root@dbserver mysql]# rpm -ivh mysql-community-client-plugins-8.0.33-1.el7.x86_64.rpm 
警告:mysql-community-client-plugins-8.0.33-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-client-plugins-8.################################# [100%]
[root@dbserver mysql]# rpm -ivh mysql-community-client-8.0.33-1.el7.x86_64.rpm 
警告:mysql-community-client-8.0.33-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
错误:依赖检测失败:
        mysql-community-libs(x86-64) >= 8.0.11 被 mysql-community-client-8.0.33-1.el7.x86_64 需要
[root@dbserver mysql]# rpm -ivh mysql-community-libs-8.0.33-1.el7.x86_64.rpm 
警告:mysql-community-libs-8.0.33-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-libs-8.0.33-1.el7################################# [100%]
[root@dbserver mysql]# rpm -ivh mysql-community-client-8.0.33-1.el7.x86_64.rpm 
警告:mysql-community-client-8.0.33-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-client-8.0.33-1.e################################# [100%]
[root@dbserver mysql]# rpm -ivh mysql-community-server-8.0.33-1.el7.x86_64.rpm 
警告:mysql-community-server-8.0.33-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
错误:依赖检测失败:
        mysql-community-icu-data-files = 8.0.33-1.el7 被 mysql-community-server-8.0.33-1.el7.x86_64 需要
[root@dbserver mysql]# rpm -ivh mysql-community-icu-data-files-8.0.33-1.el7.x86_64.rpm 
警告:mysql-community-icu-data-files-8.0.33-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-icu-data-files-8.################################# [100%]
[root@dbserver mysql]# rpm -ivh mysql-community-server-8.0.33-1.el7.x86_64.rpm 
警告:mysql-community-server-8.0.33-1.el7.x86_64.rpm: 头V4 RSA/SHA256 Signature, 密钥 ID 3a79bd29: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-server-8.0.33-1.e################################# [100%]
   
   
// active (running)看到这个状态表明mysql未启动
[root@dbserver mysql]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
[root@dbserver mysql]# systemctl start mysqld

// active (running)看到这个状态表明mysql启动了
[root@dbserver mysql]# systemctl status mysqld
● mysqld.service - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/mysqld.service; enabled; vendor preset: disabled)
   Active: active (running) since 四 2023-05-04 15:53:31 CST; 3s ago
     Docs: man:mysqld(8)
           http://dev.mysql.com/doc/refman/en/using-systemd.html
  Process: 9346 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 9430 (mysqld)
   Status: "Server is operational"
    Tasks: 38
   CGroup: /system.slice/mysqld.service
           └─9430 /usr/sbin/mysqld

5月 04 15:53:03 dbserver systemd[1]: Starting MySQL Server...
5月 04 15:53:31 dbserver systemd[1]: Started MySQL Server.

[root@dbserver mysql]# mysqladmin -u root -p password "root"
Enter password: 
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'
[root@dbserver mysql]# mysqladmin -u root -p password
Enter password: 
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'
[root@dbserver mysql]# mysqladmin -u root -p password
Enter password: 
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: NO)'
[root@dbserver mysql]# mysqladmin -u root -p password
Enter password: 
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'
[root@dbserver mysql]# mysqladmin -u root -p password
Enter password: 
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'
[root@dbserver mysql]# mysql -u root -p 
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
[root@dbserver mysql]# mysql -u root -p 
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@dbserver mysql]# mysql -u root -p 
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@dbserver mysql]# sudo mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@dbserver mysql]# mysql -u root
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
[root@dbserver mysql]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 20
Server version: 8.0.33

Copyright (c) 2000, 2023, 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> alter user root@localhost identified by 1234
    -> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '1234' at line 1
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '1234';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> SET GLOBAL validate_password.policy=LOW;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.
mysql> alter user root@localhost identified by @DFq1591001;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@Sq15922441001' at line 1
mysql> alter user root@localhost identified by DFq1591001;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DFq1591001' at line 1

//建议加上引号'root'@'localhost'
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'DFq1591001';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '@DFq1591001';
Query OK, 0 rows affected (0.04 sec)

mysql> exit
Bye

//重新用新密码登录
[root@dbserver mysql]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 21
Server version: 8.0.33 MySQL Community Server - GPL

Copyright (c) 2000, 2023, 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> create database my_db;
Query OK, 1 row affected (0.13 sec)

mysql> use my_db;
Database changed
mysql> cretae table first(id int ,name varchar(10),age int,primary key(`id`))engine=innodb;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'cretae table first(id int ,name varchar(10),age int,primary key(`id`))engine=inn' at line 1
mysql> create table message1(
    -> `id` int comment '主键',
    -> `content` varchar(255) comment '内容',
    -> primary key(`id`)
    -> )ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='短信表';
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO my_db.message1
    -> (id, content)
    -> VALUES(1, 'dzfzcessf');
Query OK, 1 row affected (0.01 sec)

mysql> select * from message1;
+----+-----------+
| id | content   |
+----+-----------+
|  1 | dzfzcessf |
+----+-----------+
1 row in set (0.00 sec)

mysql> 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值