CentOS 安装MySQL

961 阅读2分钟

安装 mysql

# wget安装
[root@hecs-x-medium-2-linux-20200403135700 /]# yum install wget  
# 下载mysql的repo源 
[root@hecs-x-medium-2-linux-20200403135700 /]# wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
# 安装rpm包
[root@hecs-x-medium-2-linux-20200403135700 /]# sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
# 安装mysql
[root@hecs-x-medium-2-linux-20200403135700 /]# sudo yum install mysql-server
# 把/var/lib/mysql的拥有者改为当前用户
[root@hecs-x-medium-2-linux-20200403135700 /]# sudo chown -R root:root /var/lib/mysql
# 重启mysql
[root@hecs-x-medium-2-linux-20200403135700 /]# service mysqld restart
# 直接会进入mysql命令行
[root@hecs-x-medium-2-linux-20200403135700 /]# mysql -u root -p
# 重置密码
mysql> 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
mysql> update user set password=password('密码') where user='用户名';
Query OK, 4 rows affected (0.00 sec)
Rows matched: 4  Changed: 4  Warnings: 0

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit;
Bye

配置远程连接

# 连接
[root@hecs-x-medium-2-linux-20200403135700 /]# mysql -uroot -p
# 切换
mysql> use mysql;

# 更改 "mysql" 数据库里的 "user" 表里的 "host" 字段 但是我这里报错了
mysql> update user set host = '%' where user = 'root';
ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'

# 查看数据库host信息如下 host已经有了%这个值
mysql>  select host from user where user = 'root';
+--------------------------------------+
| host                                 |
+--------------------------------------+
| %                                    |
| 127.0.0.1                            |
| ::1                                  |
| hecs-x-medium-2-linux-20200403135700 |
+--------------------------------------+
# 刷新系统权限相关
mysql> flush privileges;

提供一些刚装好数据库可能会用到的语句仅供参考

# 关闭mysql服务
[root@hecs-x-medium-2-linux-20200403135700 /]# service mysql stop
# 开启mysql服务
[root@hecs-x-medium-2-linux-20200403135700 /]# service mysql start
# 重启mysql服务
[root@hecs-x-medium-2-linux-20200403135700 /]# service mysqld restart

# 查看 host 表中指定的数据
mysql> select host, user from user;
+--------------------------------------+------+
| host                                 | user |
+--------------------------------------+------+
| %                                    | root |
| 127.0.0.1                            | root |
| ::1                                  | root |
| hecs-x-medium-2-linux-20200403135700 |      |
| hecs-x-medium-2-linux-20200403135700 | root |
| localhost                            |      |
+--------------------------------------+------+

# 查看 mysql 占用端口
mysql> show variables like 'port';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port          | 3306  |
+---------------+-------+

# 查看 mysql 版本
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.48    |
+-----------+

云服务器记得一定要打开安全组

参考1: segmentfault.com/a/119000001…

参考2: segmentfault.com/a/119000002…

参考3: www.jianshu.com/p/5c2e7cad9…