Linux下安装Mysql数据库

435 阅读2分钟

CentOS 7下yum成功安装 MySQL 5.7

  • 第一部分:CentOS 7安装MySQL 5.7

1.下载YUM库

shell > wget dev.mysql.com/get/mysql57…

2.安装YUM库

yum localinstall -y mysql57-community-release-el7-7.noarch.rpm

3.安装数据库

yum install -y mysql-community-server

4.启动MySQL服务

systemctl start mysqld.service

systemctl restart mysqld.service

5.默认空密码

shell > mysql -uroot -p

6.重置root密码后重启mysql服务

shell > update mysql.user set authentication_string=password("FengZe123") where user="root" and Host="localhost";

shell > flush privileges;

shell > quit;

systemctl restart mysqld;

如果手贱或者不知道啥原因出现如下问题:

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

请修改my.cnf,添加skip-grant-tables和skip-networking:

shell > vi /etc/my.cnf

[mysqld]

skip-grant-tables

skip-networking

重启mysql,然后重复以上修改密码步骤即可,记得修改完后,去掉my.cnf添加的两行。 便捷办法

  • 第二部分:配置

1、添加远程登录用户(登入Mysql)

use mysql;

GRANT ALL PRIVILEGES ON*.* TO 'root'@'%' IDENTIFIED BY 'FengZe123' WITH GRANT OPTION;

注意:

问题:ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

解决办法:

1.ALTER USER USER() IDENTIFIED BY 'FengZe123';

GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的密码' WITH GRANT OPTION;

2、创建用户时报错:

ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
报错原因:密码强度不够。
解决方法:(该账号为测试账号,所以采用降低密码策略强度)
mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)
mysql> set global validate_password_length=4;
Query OK, 0 rows affected (0.00 sec)
mysql> 再用1的方法;

2、检查用户表,刷新内存权限

select host, user from user;

FLUSH PRIVILEGES; 退出Mysql;

3、设置防火墙(CentOS7 不推荐)

vi /etc/sysconfig/iptables

在-A RH-Firewall-1-INPUT -j REJECT –reject-with icmp-host-prohibited之前,添加

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT

重启防火墙

service iptables restart

注:
centos7使用的是firewall防火墙
systemctl stop firewalld.service #停止
systemctl disable firewalld.service #禁用

4、设置字符编码集和区分大小写

4.1修改mysql配置文件(设置字符编码集)

默认位置:/etc/my.cnf

进入etc文件夹>>vim my.cnf

[mysqld]

character-set-server=utf8

collation-server=utf8_general_ci

创建数据库:create database 名字;

创建表格:create table(名字+类性);

查看数据库:show databases;