Centos7安装mysql数据库

一、查看当前Centos版本

[root@yl-web yl]# cat /etc/redhat-release 
CentOS Linux release 7.1.1503 (Core)

二、安装mysql数据库

一般网上的方法都是:
#yum install mysql
#yum install mysql-server
#yum install mysql-devel

结果:

安装mysql和mysql-devel都成功,但是安装mysql-server失败。

[root@yl-web yl]# yum install mysql-server
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.sina.cn
 * extras: mirrors.sina.cn
 * updates: mirrors.sina.cn
No package mysql-server available.
Error: Nothing to do

原因是:CentOS 7 版本将MySQL数据库软件从默认的程序列表中移除,用mariadb代替了。

解决方法 一:安装mariadb

MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可。开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源的潜在风险,因此社区采用分支的方式来避开这个风险。MariaDB的目的是完全兼容MySQL,包括API和命令行,使之能轻松成为MySQL的代替品。

yum install mariadb-server mariadb #安装mariadb数据库

mariadb数据库的相关命令是:
systemctl start mariadb  #启动MariaDB

systemctl stop mariadb  #停止MariaDB

systemctl restart mariadb  #重启MariaDB

systemctl enable mariadb  #设置开机启动

首先启动数据库进入再进入mysql:

 # systemctl start mariadb

 # mysql -u root -p

这是你又会发现,进不去,密码错误,按照网上在 /var/log/mysqld.log 是没有这个日志的。

正确方式:

1、进到数据库进行操作
[root@localhost etc]# mysql
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 7
Server version: 5.5.41-MariaDB MariaDB Server

Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]> mysql
-> ;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds
to your MariaDB server version for the right syntax to use near 'mysql' at line 1MariaDB [(none)]> update user set password=password("123456")where user='root';
ERROR 1046 (3D000): No database selected

2、选择数据库
MariaDB [(none)]> 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

3、用户root添加密码
MariaDB [mysql]> update user set password=password("123456")where user='root';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 4 Changed: 0 Warnings: 0

4、赋予权限
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)

5、退出,重新登录
MariaDB [mysql]> exit
Bye

6、尝试无密码是否可以登录
[root@localhost etc]# mysql

提示错误

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

7、输入密码登陆
[root@localhost etc]# mysql -u root -p123456
Welcome to the MariaDB monitor. Commands end with ; or g.
Your MariaDB connection id is 9
Server version: 5.5.41-MariaDB MariaDB Server

Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.

MariaDB [(none)]>

解决方法二:安装mysql

执行 yum remove mariadb.x86_64 移除.( remove之前请查看对应需要移除的版本 通过 yum serach mysql 查看内置的 mariadb 版本)

现在MySQL数据源 , 在MySQLyum 下载地址找到对应的链接和版本复制下载链接就行对应下载。 
eg:centOS mysql 8 下载链接https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm

复制对应的下载链接,进入 centos 进行下载。
执行 wget https://dev.mysql.com/get/mysql80-community-release-el7-1.noarch.rpm ;

安装时间依赖网速;一路 yes(y) 进行安装;

安装结束后 默认情况下会自动启动MySQL 服务; 

使用 ps -ef | grep 'mysql' 查看MySQL启动进程

命令:
service mysqld start/resart/stop (启动、重启、关闭 相关服务)

密码修改:

安装好数据库后,就是链接数据库。数据库在安装过程中会自动创建一个临时密码。 
请在/var /log/mysqld.lod 进行密码查看。
类似下面示例; 其中root@localhost: :9;0>yeDo?pW中的 :9;0>yeDo?pW 即为你的临时密码;
这里我操作是可以看见我这个密码很恶心; 我是用这个密码进行登录一直登录失败;

我曾使用 :9;0>yeDo?pW(:前有空格哦!) 、:9;0>yeDo?pW(:前无空格哦!) 、
9;0>yeDo?pW(没:没空格) 这上个密码进行参数均未成功; 

之后就这能想到修改密码进行操作。 在 /etc/my.cnf
文件中添加 skip-grant-tables 保存之后进行数据库重启service mysqld restart; 
然后就可以直接登录MySQL; 允许mysql 进入MySQL数据库; 接下来就是进行密码修改;

进入 mysql 数据库下的 user表进行密码修改 ;
执行 ALTER USER 'root'@'localhost' IDENTIFIED BY 'YourPassword@123;

远程连接:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'yourpassword' WITH GRANT OPTION; // 设置登录许可
// 修改权限许可后 需要更新权限 或 重启服务
flush privileges // 1更新权限 
service mysqld restart //2 退出MySQL 执行 服务重启 两者执行其一即可

如果数据库连接失败 提示1251 。
继续执行ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'yourpassword*123' 
后更新 flush privileges ;

还需要注意的是 防火墙是否关闭。执行关闭service firewalld stop

这段时间都没怎么跟新,之后全都得补上来了。

原文地址:https://www.cnblogs.com/wanchen-chen/p/12934110.html