Ubuntu 安装 Mysql

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

# 大功告成
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值