CentOS7中使用yum安装mysql

安装

参照 2.5.1 Installing MySQL on Linux Using the MySQL Yum Repository中的说明,使用yum安装mysql。

主要步骤如下:

  1. 下载centos7/redhat7对应的mysql的yum文件。或者点击MySQL Yum Repository,查看"Red Hat Enterprise Linux 7"对应的rpm为mysql84-community-release-el7-2.noarch.rpm,则使用命令wget http://dev.mysql.com/get/mysql84-community-release-el7-2.noarch.rpm直接下载rpm文件。

  2. 执行命令,安装yum源:

bash 复制代码
$> sudo yum install mysql80-community-release-el6-{version-number}.noarch.rpm
  1. 然后选择mysql版本,例如选择mysql8.0版本:
bash 复制代码
$> yum repolist all | grep mysql
$> sudo yum-config-manager --disable mysql57-community
$> sudo yum-config-manager --enable mysql80-community
  1. 安装MySQL:
bash 复制代码
$> sudo yum install mysql-community-server
  1. 启动MySQL:
bash 复制代码
$> systemctl start mysqld
$> systemctl status mysqld

设置用户名和密码并赋予权限

MySQL的用户名使用'user_name'@'host_name'格式进行设置,如果不设置@'host_name',则host_name默认使用%,代表任意IP均可以连接。localhost代表本地连接。关于用户名的详细规则,可以参考6.2.4 Specifying Account Names

检查临时密码,用root用户登录,修改root密码:

bash 复制代码
$> sudo grep 'temporary password' /var/log/mysqld.log
$> mysql -uroot -p
mysql> SHOW VARIABLES LIKE 'validate_password%';	# 查看密码规则,然后把密码规则都调低
mysql> set global validate_password.policy=0;
mysql> set global validate_password.special_char_count=0;
mysql> set global validate_password.number_count=0;
mysql> set global validate_password.mixed_case_count=0;
mysql> set global validate_password.length=1;
mysql> set global validate_password.check_user_name=OFF;
mysql> SHOW VARIABLES LIKE 'validate_password%';
+-------------------------------------------------+-------+
| Variable_name                                   | Value |
+-------------------------------------------------+-------+
| validate_password.changed_characters_percentage | 0     |
| validate_password.check_user_name               | OFF   |
| validate_password.dictionary_file               |       |
| validate_password.length                        | 1     |
| validate_password.mixed_case_count              | 0     |
| validate_password.number_count                  | 0     |
| validate_password.policy                        | LOW   |
| validate_password.special_char_count            | 0     |
+-------------------------------------------------+-------+
8 rows in set (0.00 sec)

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';

使用新密码登录:

bash 复制代码
$> mysql -uroot -proot # 注意-p后没有空格,-u后可以有空格。也可以-p后什么都不接,手动输入密码

创建新用户并设置密码(详情参看6.2.14 Assigning Account Passwords),并赋予权限(参看6.2.2 Privileges Provided by MySQL13.7.1.6 GRANT Statement):

bash 复制代码
mysql> create user 'eric'@'%' identified by 'eric';   # 创建用户
mysql> grant all privileges on *.* to 'eric'@'%' with grant option;   # 赋予用户权限,用户名可以直接写eric
mysql> flush privileges;   # 重新加载权限使其生效,有可能不需要这一步
mysql> quit # 退出,可以不加分号,或使用ctl+d退出

查询某个用户的权限:

bash 复制代码
mysql> show grants for eric;

Testing the Server

使用mysqladmin version查看MySQL版本:

bash 复制代码
$> mysqladmin -uroot -proot version # mysqladmin在/usr/bin/目录下。root/root是mysql的用户名密码。对于新安装的MySQL,可能不需要-u和-p选项
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
mysqladmin  Ver 8.0.25 for Linux on x86_64 (MySQL Community Server - GPL)
Copyright (c) 2000, 2021, 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.

Server version		8.0.25
Protocol version	10
Connection		Localhost via UNIX socket
UNIX socket		/var/lib/mysql/mysql.sock
Uptime:			1 hour 30 min 24 sec

Threads: 2  Questions: 14  Slow queries: 0  Opens: 152  Flush tables: 3  Open tables: 71  Queries per second avg: 0.002

使用mysqlshow列出数据库:

bash 复制代码
$> mysqlshow -uroot -proot
mysqlshow: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
|     Databases      |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| springsec          |
| sys                |
| vhr                |
+--------------------+

使用mysql -e执行查询语句:

bash 复制代码
$> mysql -uroot -proot -e "SELECT User, Host, plugin FROM mysql.user" mysql
mysql: [Warning] Using a password on the command line interface can be insecure.
+------------------+-----------+-----------------------+
| User             | Host      | plugin                |
+------------------+-----------+-----------------------+
| root             | %         | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | caching_sha2_password |
+------------------+-----------+-----------------------+

技巧

参看B.3.3.2 How to Reset the Root Password,重置root密码。

mysql的可执行文件在/usr/bin/mysql*/usr/sbin/mysql*中,如果找不到,可以用find -name "mysql"进行搜索。

mysql有个配置文件/etc/my.cnf,里面配置了mysql的datadir(数据文件)位置为/var/lib/mysql,并且配置了port=3306用作mysql启动端口(参看How to change the default port of mysql from 3306 to 3360)。

使用命令sudo ss -antp | grep mysqldps -ef | grep mysql(--port选项),可以检测现有MySQL使用的端口是哪个。

参考资料

相关推荐
不宕机的小马达2 小时前
【Mysql|第一篇】Mysql的安装与卸载、Navicat工具的使用
数据库·mysql
孔丘闻言2 小时前
python调用mysql
android·python·mysql
a_blue_ice5 小时前
JAVA 面试 MySQL
java·mysql·面试
Java水解8 小时前
【MySQL】数据库基础
后端·mysql
沃夫上校8 小时前
MySQL 中文拼音排序问题
java·mysql
要一起看日出8 小时前
MVCC-多版本并发控制
数据库·mysql·mvcc
Hx__8 小时前
MySQL InnoDB 的 MVCC 机制
数据库·mysql
chat2tomorrow10 小时前
数据采集平台的起源与演进:从ETL到数据复制
大数据·数据库·数据仓库·mysql·低代码·postgresql·etl
依稀i12310 小时前
Spring Boot + MySQL 创建超级管理员
spring boot·mysql
驾驭人生11 小时前
Asp .Net Core 系列:Asp .Net Core 集成 Hangfire+MySQL
数据库·mysql·.netcore