[Linux] CentOS 安装 MySQL

借鉴了两位博主的配置:

duo&duo  用 wget 安装 MySQL  https://www.cnblogs.com/duo-duo/p/10028219.html ;

石公子   远程连接MySQL服务器   https://www.cnblogs.com/zlbluesky/p/MySQL.html ;

在tx服务器上安装 MySQL 数据库步骤:

1、下载 yum 安装源:这个是安装 mysql 的源

# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm

2、安装该源

# rpm -ivh mysql-community-release-el7-5.noarch.rpm

3、用 yum 命令安装 mysql

# yum install mysql-server

4、重置密码、首先要登录

# mysql -u root

上面的命令很可能会报错:

ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock'

如果报错请使用下面的命令把 /var/lib/mysql 的拥有者改为当前用户:

# chown -R root:root /var/lib/mysql

5、重启服务

$ service mysqld restart

6、接下来登录重置密码:

$ mysql -u root
mysql> use mysql;
mysql> update user set password=password('此处填妳的密码') where user='root';
mysql> exit;
重复操作5

7、更改权限允许远程登陆:

# mysql -h localhost -uroot -p
Enter password: <=这里填妳刚刚更改的密码
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> use mysql; <=此DB存放MySQL的各种配置信息
Database changed
mysql> select host, user from user; <=查看用户的权限情况
mysql> select host, user, password from user;
+-----------+------+-------------------------------------------+
| host      | user | password                                  |
+-----------+------+-------------------------------------------+
| localhost | root | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
| 127.0.0.1 | root | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
| localhost |      |                                           |
+-----------+------+-------------------------------------------+
4 rows in set (0.01 sec)

8、允许其他人登陆,'%' 表示允许所有能够输入密码的用户登陆

mysql> Grant all privileges on *.* to 'root'@'%' identified by '远程登录密码' with grant option;
mysql> flush privileges;   <=运行此句才生效,或者重启MySQL
mysql> select host, user, password from user;
+-----------+------+-------------------------------------------+
| host     | user | password                                 |
+-----------+------+-------------------------------------------+
| localhost | root | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
| 127.0.0.1 | root | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
| localhost |      |                                          |
| %        | root | *4ACFE3202A5FF5CF467898FC58AAB1D615029441 |
+-----------+------+-------------------------------------------+
4 rows in set (0.01 sec)

到这里我们的 MySQL 就算是安装好了,然后我们来配置防火墙:

1、检查系统的 3306 端口是否打开:

$ netstat -lnp | grep 3306
tcp6    0  0:::3306    :::*    LISTEM      xxxx/mysql

2、然后看看防火墙的情况,查看 firewall 的服务状态(CentOS7中防火墙改成了 firewall ):

$ systemctl status firewalld

如果提示防火墙已关闭请使用以下命令来开启防火墙:

# systemctl start firewalld

3、用防火墙查看 3306 端口是否开放:

# firewall-cmd --query-port=3306/tcp

4、如果显示的是 no ,则输入下列命令来开放 3306 端口:

# firewall-cmd --permanent --add-port=3306/tcp

5、最后重启防火墙:

# firewall-cmd --reload

注:如果用的云服务器还要查看云服务器中的端口是否开放!=>传送门:tx云服务器怎么开放端口

posted @ 2019-09-28 15:02  绎丶江忆南  阅读(453)  评论(0编辑  收藏  举报