1. 通过apt安装MySQL
1 #命令1
2 sudo apt-get update
3 #命令2
4 sudo apt-get install mysql-server
2. 配置mysql初始化信息
1 sudo mysql_secure_installation
配置说明:
ubuntu@VM-0-10-ubuntu:~$ sudo mysql_secure_installationSecuring 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?Press y|Y for Yes, any other key for No: N(选择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) : N(选择N,不删除匿名用户)... skipping.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(选择N,允许root远程连接)... 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.Remove test database and access to it? (Press y|Y for Yes, any other key for No) : N(选择N,不删除test数据库)... skipping.
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(选择Y,修改权限立即生效)
Success.All done!
3. 创建数据库
1 #1 创建数据库TestDB
2 CREATE DATABASE TestDB;
4. 配置访问权限
1 # 登陆mysql
2 sudo mysql -uroot -p
3 #切换数据库
4 use mysql;
5 #查询用户表命令:
6 select User,Host from user;
1 ubuntu@VM-0-10-ubuntu:~$ ubuntu@VM-0-10-ubuntu:~$ sudo mysql -u root -p
2 Enter password:
3 Welcome to the MySQL monitor. Commands end with ; or \g.
4 Your MySQL connection id is 29
5 Server version: 8.0.21-0ubuntu0.20.04.4 (Ubuntu)
6
7 Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
8
9 Oracle is a registered trademark of Oracle Corporation and/or its
10 affiliates. Other names may be trademarks of their respective
11 owners.
12
13 Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
14
15 mysql> use mysql;
16 Reading table information for completion of table and column names
17 You can turn off this feature to get a quicker startup with -A
18
19 Database changed
20 mysql> select User,Host from user;
21 +------------------+-----------+
22 | User | Host |
23 +------------------+-----------+
24 | Joseph | % |
25 | debian-sys-maint | localhost |
26 | mysql.infoschema | localhost |
27 | mysql.session | localhost |
28 | mysql.sys | localhost |
29 | root | localhost |
30 +------------------+-----------+
31 6 rows in set (0.00 sec)
32
33 mysql>
创建新用户并赋予权限
1 # 创建用户Joseph@'%',密码是‘123456’
2 create user Joseph@'%' identified by '123456';
3 # 赋予其TestDB数据库的远程连接权限
4 grant all privileges on Joseph.* to TestDB;
查看MySQL端口号
1 mysql> show global variables like 'port';
2 +---------------+-------+
3 | Variable_name | Value |
4 +---------------+-------+
5 | port | 3306 |
6 +---------------+-------+
7 1 row in set (0.00 sec)
退出MySQL查看3306端口是否正常
1 # 退出MySQL
2 mysql> exit
3 Bye
4 # 查看3306端口是否正常
5 ubuntu@VM-0-10-ubuntu:~$ netstat -an | grep 3306
6 tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN
7 ubuntu@VM-0-10-ubuntu:~$
注意:现在的3306端口绑定的IP地址是本地的127.0.0.1
修改Mysql配置文件(注意路径)
1 ubuntu@VM-0-10-ubuntu:~$ vim /etc/mysql/mysql.conf.d/mysqld.cnf
找到
bind-address = 127.0.0.1
前面加#注释掉
5. MySQL服务命令
1 # 检查服务状态
2 sudo service mysql status
3 # 停止 MySQL 服务
4 sudo service mysql stop
5 # 启动 MySQL 服务
6 sudo service mysql start
6. 腾讯云或者阿里云注意要配置安全组信息放开端口
7. 注意防火墙状态
1 # 查看防火墙状态
2 sudo ufw status
3 # 停止防火墙
4 sudo ufw disable
5 # 启动防火墙
6 sudo ufw enable