安装
打开终端直接开始,编码配置方法在后面
#通过apt更新包索引
sudo apt update
#按照默认软件包安装
sudo apt install mysql-server
#运行安全脚本
sudo 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?
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(最低等级,将来需要设置长度不少于8个字符的密码)
在Ubuntu系统中MySQL 5.7及之后的版本,MySQL的root用户被默认设置成通过auth_socket插件进行认证,而不是通过密码。在很多情况下,这些配置可以使系统更加的安全和可靠,但如果允许外部程序(例如phpMyAdmin)访问时,这将是事情变得非常复杂。
为了能够以root用户通过密码的方式连接MySQL,需要将其认证方式从 auth_socket 方式变更为mysql_native_password。进行该设置,通过终端打开MySQL的提示符:
更换默认的认证方式 (auth_socket ===》 mysql_native_password)
#启动MySQL
sudo mysql
#查看每个用户的认证方式
SELECT user,authentication_string,plugin,host FROM mysql.user;
#输出结果:
mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| root | | auth_socket | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *0514040CAF65C120C8905B09219C0BA8010B2373 | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)
#更改root的认证方式,auth_socket ===》 mysql_native_password
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '{PASSWORD};
#让数据库系统重新加载授权表
FLUSH PRIVILEGES;
#重新查看授权方式
SELECT user,authentication_string,plugin,host FROM mysql.user;
#输出结果:
mysql> SELECT user,authentication_string,plugin,host FROM mysql.user;
+------------------+-------------------------------------------+-----------------------+-----------+
| user | authentication_string | plugin | host |
+------------------+-------------------------------------------+-----------------------+-----------+
| root | *84AAC12F54AB666ECFC2A83C676908C8BBC381B1 | mysql_native_password | localhost |
| mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE | mysql_native_password | localhost |
| debian-sys-maint | *0514040CAF65C120C8905B09219C0BA8010B2373 | mysql_native_password | localhost |
+------------------+-------------------------------------------+-----------------------+-----------+
4 rows in set (0.00 sec)
安装可视化工具 MySQL workbench
sudo apt-get install mysql-workbench
配置
Ubuntu默认语言是英语,安装MySQL后,默认建表插入语句时出现错误1336,这是默认编码与插入字符不符造成的。
修改方法如下:
#mysql默认配置文件夹
ls /etc/mysql/
conf.d debian-start my.cnf.fallback mysql.conf.d
debian.cnf my.cnf mysql.cnf
#修改mysql.conf.d文件夹下的配置文件
vim /mysql.conf.d/mysql.conf.d
#在[mysqld]块下追加一行字符设置 character-set-server=utf8
[mysqld]
#
# * Basic Settings
#
user = mysql
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
port = 3306
basedir = /usr
datadir = /var/lib/mysql
tmpdir = /tmp
lc-messages-dir = /usr/share/mysql
skip-external-locking
character-set-server=utf8
#还需要设置客户端的配置
cd /etc/mysql/conf.d
#修改该路径下的mysql.cnf,依然在[mysqld]后追加,内容稍有不同
default-character-set=utf8