MySQL的安装
一、准备环境
//查看是否有MySQL相关依赖包
rpm -qa|grep -i mysql
//查看是否有MariaDB数据库管理系统,这个东西会影响MySQL的安装
rpm -qa|grep -i mariadb
//如果有上述相关依赖包,删除它
rpm -e 文件名
//如果删除失败,有相关使用
rpm -e xxxx --nodeps
//如果提示错误:error: %preun(xxxxxx) scriptlet failed, exit status 1
rpm -e --noscripts 文件名
//查找是否有MySQL安装历史遗留文件夹
find / -name mysql
//删掉他们
rm -rf xxxxx
//查看是否有MySQL用户组
cat /etc/group | grep mysql
//MySQL用户组密码
cat /etc/passwd |grep mysql
注1:rpm 类似npm yarn都是包管理工具,用于安装和管理依赖包
注2:/etc/group和/etc/passwd下的用户组是系统或服务正常运行所必需的用户,我们把这种用户称为系统用户或伪用户。系统用户是不能登录系统的,但是这些用户同样也不能被删除,因为一旦删除,依赖这些用户运行的服务或程序就不能正常执行,会导致系统问题。
二、安装MySQL
?本文安装的是MySQL5.7版本
配置文件:/etc/my.cnf
数据存储:/var/lib/mysql
命令文件:/usr/bin和/usr/sbin
数据库端口:3306
运行以下命令更新YUM源。
rpm -Uvh http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm
运行以下命令安装MySQL。
yum -y install mysql-community-server
注:这里由于我的之前操作有遗留的yum module 导致报错 找不到资源
yum module disable mysql
运行以下命令查看MySQL版本号。
mysql -V
返回结果如下,表示MySQL安装成功。
mysql Ver 14.14 Distrib 5.7.26, for Linux (x86_64) using EditLine wrapper
三、配置MySQL
运行以下命令启动MySQL服务。
systemctl start mysqld
运行以下命令设置MySQL服务开机自启动。
systemctl enable mysqld
运行以下命令查看/var/log/mysqld.log文件,获取并记录root用户的初始密码。
grep ‘temporary password‘ /var/log/mysqld.log
执行命令结果示例如下。
2020-04-08T08:12:07.893939Z 1 [Note] A temporary password is generated for [email protected]: xvlo1lZs7>uI
注:说明下一步对MySQL进行安全性配置时,会使用该初始密码。
运行下列命令对MySQL进行安全性配置。
mysql_secure_installation
重置root用户的密码。
Enter password for user root: #输入上一步获取的root用户初始密码
The ‘validate_password‘ plugin is installed on the server.
The subsequent steps will run with the existing configuration of the plugin.
Using existing password for root.
Estimated strength of the password: 100
Change the password for root ? ((Press y|Y for Yes, any other key for No) : Y #是否更改root用户密码,输入Y
New password: #输入新密码,长度为8至30个字符,必须同时包含大小写英文字母、数字和特殊符号。特殊符号可以是()` ~!@#$%^&*-+=|{}[]:;‘<>,.?/
Re-enter new password: #再次输入新密码
Estimated strength of the password: 100
Do you wish to continue with the password provided?(Press y|Y for Yes, any other key for No) : Y #是否继续操作,输入Y
删除匿名用户账号。
By default, a MySQL installation has an anonymous user, allowing anyone to log into MySQL without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment.
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y #是否删除匿名用户,输入Y
Success.
禁止root账号远程登录。
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : Y #禁止root远程登录,输入Y
Success.
删除test库以及对test库的访问权限。
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : Y #是否删除test库和对它的访问权限,输入Y
- Dropping test database...
Success.
重新加载授权表。
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : Y #是否重新加载授权表,输入Y
Success.
All done!
四、访问MySQL数据库
1.运行以下命令后,输入root用户的密码登录MySQL。
mysql -uroot -p
2.依次运行以下命令创建远程登录MySQL的账号。示例账号为dms、密码为123456。
mysql> grant all on *.* to [email protected]%‘IDENTIFIED BY ‘123456‘; #使用root替换dms,可设置为允许root账号远程登录。
mysql> flush privileges;
MySQL常用操作
登入
mysql -u root -p 88888888
登出
mysql> exit
创建数据库
CREATE DATABASE [IF NOT EXISTS]
-> DEFAULT CHARACTER SET gb2312 //
-> DEFAULT COLLATE gb2312_chinese_ci; //
显示所有数据库
SHOW DATABASES [LIKE ‘数据库名‘];
修改数据库
ALTER DATABASE [数据库名] {
[ DEFAULT ] CHARACTER SET |
[ DEFAULT ] COLLATE }
删除数据库
DROP DATABASE [ IF EXISTS ]
切换数据库
USE [数据库名]
下节进入常用SQL语句