在云服务器上(CentOS)上安装 Mysql


前言

说实话,对于数据库的应用,我还只停留在 curd 上,哈哈怪我太垃圾了。之所以要用到数据库,是因为我正在学习 nodejs 。

  • 安装 rpm 包
1
rpm -Uvh http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
  • 安装 mysql
1
yum -y install mysql-community-server
  • 加入开机启动(就是电脑启动的时候,自动启动MySQL服务)
1
systemctl enable mysqld
  • 对 mysql 初始化操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
mysql_secure_installation

Securing the MySQL server deployment.

Connecting to MySQL using a blank password.

VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
// 这个选yes的话密码长度就必须要设置为8位以上,但我只想要6位的
Press y|Y for Yes, any other key for No: N
Please set the password for root here.

New password: // 设置密码

Re-enter new password: // 再一次确认密码
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
// 移除不用密码的那个账户
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y
Success.


Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
//是否禁止远程登录
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n

... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.

// 是否删除test库
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y
- Dropping test database...
Success.

- Removing privileges on test database...
Success.

Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.

Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.

All done!
  • 尝试登陆
1
2
3
登陆: mysql -u root -p
启动: sudo service mysqld start
终止: sudo service mysqld stop
  • 修改密码
1
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '新密码';
  • 停止 mysql 服务,卸载 mysql
1
2
3
4
5
6
7
8
9
10
rpm -qa|grep -i mysql   // 查看当前安装 mysql 情况

rpm -ev --nodeps 包名 // delete

find / -name mysql // 查看对应 mysql 目录 再删掉

注意: 卸载后 etc/my.cnf 不会删除,需要手工删除
rm -rf /etc/my.cnf

最后再查看一遍机器是否安装 mysql

总结

在云服务器上安装和 mac 本地安装类似,mac 可以使用 homebrew 安装 mysql,brew install mysql@5.7,后面步骤类似。