绝大部分参考Linux实验:MySQL的安装和配置_怪人史哥的博客-CSDN博客
和
记一次Linux安装mysql报错问题:失败的软件包是:mysql-community-client-5.7.39-1.el7.x86_64 - huashui - 博客园
1、下载安装MySQL服务 root 用户
从MySQL官方的Yum Repository中下载rpm软件包
wget –c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
2、安装下载的rpm软件包
yum -y install mysql57-community-release-el7-10.noarch.rpm
3、安装MySQL社区版服务器
yum -y install mysql-community-server
从 file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql 检索密钥
导入 GPG key 0x5072E1F5:
用户ID : "MySQL Release Engineering <mysql-build@oss.oracle.com>"
指纹 : a4a9 4068 76fc bd3c 4567 70c8 8c71 8d3b 5072 e1f5
软件包 : mysql57-community-release-el7-10.noarch (installed)
来自 : /etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
mysql-community-client-5.7.40-1.el7.x86_64.rpm 的公钥尚未安装
失败的软件包是:mysql-community-client-5.7.40-1.el7.x86_64
GPG 密钥配置为:file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
4、安装mysql-community-server时报错解决(失败的软件包是:mysql-community-client-5.7.40-1.el7.x86_64)
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2022
5、重新执行第三步
# yum install -y mysql-community-server
已安装:
mysql-community-libs.x86_64 0:5.7.40-1.el7 mysql-community-libs-compat.x86_64 0:5.7.40-1.el7
mysql-community-server.x86_64 0:5.7.40-1.el7
作为依赖被安装:
mysql-community-client.x86_64 0:5.7.40-1.el7 mysql-community-common.x86_64 0:5.7.40-1.el7
作为依赖被升级:
postfix.x86_64 2:2.10.1-9.el7
替代:
mariadb-libs.x86_64 1:5.5.60-1.el7_5
完毕!
6、确定是否成功安装
[root@omsphere tmp]# mysql -v
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
[root@omsphere tmp]# mysql -V
mysql Ver 14.14 Distrib 5.7.40, for Linux (x86_64) using EditLine wrapper
7、开放MySQL的默认端口3306
# 重启防火墙,以保证防火墙一定是开启的
systemctl restart firewalld.service
# 在防火墙添加端口3306且设置永久开启
firewall-cmd --zone=public --add-port=3306/tcp --permanent
# 重新加载防火墙,使上一步操作生效
firewall-cmd --reload
# 查看3306端口是否开放
firewall-cmd --zone=public --query-port=3306/tcp
3、设置MySQL服务,添加开机自启动
# 重启MySQL服务,不用start用restart是为了避免读者之前启动过MySQL服务且没有关闭
systemctl restart mysqld
# 检查MySQL服务的运行状态,有running字样说明启动成功
systemctl status mysqld
# 设置MySQL服务开机自启动,此步为防止以后重启了Linux后使用MySQL前忘记启动服务,读者视自身情况选择是否执行本命令
systemctl enable mysqld
4、修改root用户默认密码
获取安装时MySQL自动对root设置的临时密码
使用root用户及其临时密码登录MySQL
# 用rpm包安装的mysql的临时密码默认在/var/log/mysqld.log里
#(1)清除MySQL带来的数据:
rm -rf /var/lib/mysql
#(2)重启MySQL服务:
systemctl restart mysqld
#(3)获得mysql的临时密码:
grep 'temporary password' /var/log/mysqld.log
#(4)进入mysql:
mysql -u root -p
#(5)输入上述第(3)步grep出来的密码
Enter password:
#(6)修改root密码,mysql要求新密码必须包含大小写字母、特殊符号和数字,并且长度大于8位。
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '你的新密码' WITH GRANT OPTION;;
Query OK, 0 rows affected (0.00 sec)
#(6)commit
mysql> commit;
5、在远程(非mysql所在的机器)验证root新密码登录MySQL,失败并解决
非mysql所在的机器指的是:例如你的mysql安装在vmware上,而你是用xshell在你的windows上登录。
[root@omsphere ~]# mysql -u root –p
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
解决方案:
vim /etc/my.cnf文件;
在[mysqld]后添加skip-grant-tables(登录时跳过权限检查)
# 1、进入MySQL配置文件
vi /etc/my.cnf
# 按下i键进入编辑模式,在[mysqld]所在行的下一行插入以下行内容:
skip-grant-tables
# 按下esc键退出编辑模式
# 保存并退出文件
:wq
# 2、重启MySQL服务,并用非密码方式登录,按如下操作进行
systemctl restart mysqld
[root@omsphere ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.40 MySQL Community Server (GPL)
Copyright (c) 2000, 2022, 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> 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> quit
# 3、进入MySQL配置文件
vi /etc/my.cnf
# 按下i键进入编辑模式,在skip-grant-tables前面加入一个#注释掉skip-grant-tables:
#skip-grant-tables
# 按下esc键退出编辑模式
# 保存并退出文件
:wq
#4、重启mysql
[root@omsphere ~]# systemctl restart mysqld
6、配置MySQL默认编码为utf-8
# 进入MySQL配置文件
vi /etc/my.cnf
# 按下i键进入编辑模式,在[mysqld]所在行的下一行插入以下两行内容:
character_set_server=utf8
init_connect='SET NAMES utf8'
# 按下esc键退出编辑模式
# 保存并退出文件
:wq
# 重启MySQL服务
systemctl restart mysqld
# 登陆MySQL查看默认编码,最终结果如下图
mysql -u root –p
mysql> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
7、创建数据
# 创建名为wdb的数据库,并查看数据库experiment是否存在
CREATE DATABASE wdb
-> ;
Query OK, 1 row affected (0.00 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wdb |
+--------------------+
5 rows in set (0.00 sec)
#修改当前的数据库
mysql> use wdb
Database changed
#创建名为students的数据表,并查看数据表students是否存在
mysql> CREATE TABLE students (id varchar(25) NOT NULL, name varchar(25) NOT NULL, PRIMARY KEY (id));
Query OK, 0 rows affected (0.01 sec)
mysql> DESC students;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | varchar(25) | NO | PRI | NULL | |
| name | varchar(25) | NO | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
# 插入一条数据,并查看该数据是否存在
mysql> INSERT INTO students VALUES ('1','张三');
Query OK, 1 row affected (0.01 sec)
mysql> SELECT * FROM students;
+----+--------+
| id | name |
+----+--------+
| 1 | 张三 |
+----+--------+
1 row in set (0.00 sec)
mysql> exit
Bye
MySQL语句不区分大小写,建议用大写,用户自定义的内容用小写,方便阅读。
如果文章内容出错或者您有更好的解决方法,欢迎到评论区指正和讨论!
谢谢!