安装 MySQL
- 更新服务器上的包索引
sudo apt-get update
- 安装默认包
sudo apt-get install mysql-server
配置 MySQL
- 初始化配置
sudo mysql_secure_installation
- 配置项较多,如下所示:
VALIDATE PASSWORD PLUGIN can be used to test passwords...
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... Remove anonymous users?
(Press y|Y for Yes, any other key for No) : N (我的选项)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) : Y (我的选项)By default, MySQL comes with a database named 'test' that anyone can access... Remove test database and access to it?
(Press y|Y for Yes, any other key for No) : N (我的选项)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 (我的选项)
- 检查mysql服务状态
systemctl status mysql.service
显示如下结果说明mysql服务是正常的:
mysql.service - MySQL Community Server
Loaded: loaded (/lib/systemd/system/mysql.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2020-02-23 00:21:57 CST; 8min ago
Main PID: 1679 (mysqld)
Tasks: 28 (limit: 2339)
CGroup: /system.slice/mysql.service
└─1679 /usr/sbin/mysqld --daemonize --pid-file=/run/mysqld/mysqld.pid
Feb 23 00:21:57 xxxxxxxx systemd[1]: Starting MySQL Community Server...
Feb 23 00:21:57 xxxxxxxx systemd[1]: Started MySQL Community Server.
- 配置远程访问
在Ubuntu下MySQL缺省是只允许本地访问的,如果你要其他机器也能够访问的话,需要进行配置:
sudo mysql -uroot -p
输入
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY "123456";
or
GRANT ALL PRIVILEGES ON *.* TO root@localhost IDENTIFIED BY "123456";
其中root@localhost,localhost就是本地访问,配置成%就是所有主机都可连接;'123456'为你给新增权限用户设置的密码,%代表所有主机,也可以是具体的ip;
编辑配置文件
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
将 bind-address
的值修改为 0.0.0.0
- 重启MySQL
sudo /etc/init.d/mysql restart
网络安全组配置
点击==配置安全组==后==添加安全组规则==
测试
MySQL命令
启动MySQL:service mysql start
停止MySQL:service mysql stop
重启MySQL:sudo /etc/init.d/mysql restart
确认是否启动成功:sudo netstat -tap | grep mysql