sudo apt-get update
sudo apt-get install mysql-server
sudo mysql_secure_installation
VALIDATE PASSWORD COMPONENT 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 component?
Press y|Y for Yes, any other key for No:
# 启用密码插件
y
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG:
# 密码等级
0
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
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) :
# 禁止root用户远程登录
no
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) :
# 移出测试数据库
y
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设置密码
sudo mysql
SHOW DATABASES;
SELECT user, host, plugin, authentication_string FROM user;
# 设置密码
# 8.0以下用这句
UPDATE user SET plugin="mysql_native_password", authentication_string=PASSWORD("123456") WHERE user="root";
# 8.0以上用这句
alter user 'root'@'localhost' identified with mysql_native_password by '123456';
FLUSH PRIVILEGES;
exit;
# 重启mysql
sudo service mysql restart
mysql -u root -p
123456 # 数据库密码
# 设置root账户支持远程登录,若不需要可忽略
UPDATE mysql.user SET host="%" WHERE user="root";
FLUSH PRIVILEGES;
exit;
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
[mysqld]
bind_address = 0.0.0.0 # 把绑定地址改为0.0.0.0允许任何ip连接
:wq! # 保存退出
sudo service mysql restart
# 至此mysql即刻远程登录了,可以使用数据库可视化工具测试连接是否成功
# 若10060 Unkonw error 记得看看端口是否放开3306
sudo ufw allow 3306/tcp # 防火墙开放3306端口
sudo iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
# 以上两种还不行就看看云服务商那边是否没开
# 最后再设置下字符集
sudo vi /etc/mysql/my.cnf
# 把以下内容添加进去:
[client]
default-character-set = utf8mb4
[mysql]
default-character-set = utf8mb4
[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci
init_connect='SET NAMES utf8mb4'
# 保存重启
:wq!
sudo service mysql restart
# 大功告成
Ubuntu 安装 Mysql
最新推荐文章于 2024-08-26 16:16:44 发布