检查是否已经安装Mysql,如未安装则执行下面命令为空。

rpm -qa |grep -i mysql

如果不为空,则使用以下命令,进行删除安装过的包,删除相关服务,找出残余文件并删除。

rpm -e 对应文件名
chkconfig --list | grep -i mysql
chkconfig --del mysql
find / -name mysql
rm -rf 对应文件路径

可能系统自带了Mariadb数据库,要先卸载。

查询是否自带Mariadb数据库。

rpm -qa |grep Mariadb
有的话执行rpm -e --nodeps 对应软件包名字
我这里是rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64

下载Mysql源到服务器(一般可从这里开始,以上可忽略。)

Mysql源地址:https://repo.mysql.com
wget https://repo.mysql.com//mysql80-community-release-el7-3.noarch.rpm

此处地址是由https://repo.mysql.com 加上//再接上对应rpm文件名得出。

安装Mysql的yum源

rpm -ivh mysql80-community-release-el7-3.noarch.rpm
执行yum makecache可查看到已安装的yum源

安装Mysql

这里的nogpgcheck是跳过检查公钥安装,否则可能安装不了。
yum install mysql-community-server --nogpgcheck -y

查看版本号,启动Mysql设置开机启动并查看状态

mysql -v
systemctl start mysqld
systemctl enable mysqld
systemctl status mysqld

查看随机生成的密码

grep "password" /var/log/mysqld.log

以root身份进入Mysql

mysql -uroot -p
输入刚刚获取到的密码即可进入

修改root的Mysql密码

密码要求大写字母+小写字母+数字+特殊字符,长度不能小于8位
alter user 'root'@'localhost' identified with mysql_native_password BY 'Lun5201314..';

可以将密码修改为简单模式,方便操作(非必要)

#设置为弱口令,密码最小长度为1
mysql> set global validate_password.policy=0;
mysql> set global validate_password.length=1;
mysql> flush privileges;
#查看密码策略
mysql> show variables like '%validate_password_policy%';
mysql> show variables like '%validate_password_length%';

可以设置免密登录(非必要)

修改/etc/my.cnf文件
把skip-grant-tables添加到配置文件里面,放在[mysqld]下面就可以了
重启服务systemctl restart mysqld

可以对Mysql进行安全性配置(非必要)

mysql_secure_installation
重置root用户的密码。

说明:在输入密码时,系统为了最大限度的保证数据安全,命令行将不做任何回显。您只需要输入正确的密码信息,然后按Enter键即可。
Enter password for user root: #输入已获取的root用户初始密码

The existing password for the user account root has expired. Please set a new password.

New password: #输入新的MySQL密码

Re-enter new password: #重复输入新的MySQL密码
The ‘validate_password’ component is installed on the server.
The subsequent steps will run with the existing configuration
of the component.
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) :Y #输入Y选择更新MySQL密码。您也可以输入N不再更新MySQL密码。

New password: #输入新的MySQL密码

Re-enter new password: #重复输入新的MySQL密码

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确认使用已设置的密码。
b. 删除匿名用户。
c. By default, a MySQL installation has an anonymous user,
d. allowing anyone to log into MySQL without having to have
e. a user account created for them. This is intended only for
f. testing, and to make the installation go a bit smoother.
g. You should remove them before moving into a production
h. environment.
i.
j. Remove anonymous users? (Press y|Y for Yes, any other key for No) :Y #输入Y删除MySQL默认的匿名用户。
Success.
k. 禁止root账号远程登录。
l. Normally, root should only be allowed to connect from
m. ‘localhost’. This ensures that someone cannot guess at
n. the root password from the network.
o.
p. Disallow root login remotely? (Press y|Y for Yes, any other key for No) :Y #输入Y禁止root远程登录。
Success.
q. 删除test库以及对test库的访问权限。
r. By default, MySQL comes with a database named ‘test’ that
s. anyone can access. This is also intended only for testing,
t. and should be removed before moving into a production
u. environment.
v.
w.
x. Remove test database and access to it? (Press y|Y for Yes, any other key for No) :Y #输入Y删除test库以及对test库的访问权限。
y. - Dropping test database…
z. Success.
aa.
bb. - Removing privileges on test database…
Success.
cc. 重新加载授权表。
dd. Reloading the privilege tables will ensure that all changes
ee. made so far will take effect immediately.
ff.
gg. Reload privilege tables now? (Press y|Y for Yes, any other key for No) :Y #输入Y重新加载授权表。
hh. Success.
ii.
All done!

开启远程连接

mysql> use mysql;
mysql> update user set host ='%' where user ='root'

给root用户授权

mysql> grant all privileges on *.* to root@'%' with grant option;
mysql> flush privileges;

创建远程登录MySQL的账号

#创建数据库用户2021232203322,并授予远程连接权限
mysql> create user '2021232203322'@'%' identified by 'Lun5201314..';
#为2021232203322用户授权数据库所有权限
mysql> grant all privileges on *.* to '2021232203322'@'%';
#刷新权限
mysql> flush privileges;

CMD 窗口连接使用

cmd命令行输入mysql -h ip地址 -P 3306 -u 用户名 -p 需要关闭防火墙:systemctl stop firewalld

如果提示’mysql’ 不是内部或外部命令,也不是可运行的程序,则复制mysql的安装路径bin目录
我这里是:C:\Program Files\MySQL\MySQL Server 5.5\bin
右击我的电脑->属性->高级系统设置->高级->环境变量->系统变量->双击Path->新建->把刚刚的目录复制进去->确定->外面还有一个确定

连接名随便写,主机:写ip地址,端口3306,用户名为root(前面如果设置了禁止root远程登录则会登陆失败),或者创建的用户,密码为自己设置的密码,同样需要关闭防火墙:systemctl stop firewalld