下载安装MySQL
sudo apt-get update #更新软件源
sudo apt-get install mysql-server #安装mysql
 启动Mysql-servers
service mysql start

修改root密码
Ubuntu在安装MySQL的时候会自动生成一个账号密码,因此我们先获取mysql随机账号用户名和初始密码(user 、password )
sudo cat /etc/mysql/debian.cnf
 然后使用获取到的用户名和密码登录MySQL
mysql -u <user> -p
 接下来修改root密码,依次运行下面命令
use mysql;
update user set authentication_string='' where user='root'; --将字段置为空
alter user 'root'@'localhost' identified with mysql_native_password by '123456'; --修改密码为123456

然后quit 退出数据库尝试使用root 登录成功

修改编码方式为utf-8
(1)编辑配置文件。sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf (2)在[mysqld]下添加一行character_set_server=utf8 。如下图,保存并退出  (3)重启MySQL服务。service mysql restart (4)登陆MySQL,并查看MySQL目前设置的编码。show variables like "char%";

最后配置远程连接,进入配置文件
sudo vim /etc/mysql/mysql.conf.d/mysqld.cnf
将本地访问两条改掉,保存退出

root 账号登录数据库后依次运行下列代码
use mysql
update user set host = '%' where user = 'root';
flush privileges;
select user, host, plugin from user where user = 'root';

此时再查看属性就可以发现host 已经是% 了
 尝试远程连接,可以发现连接成功 至此,MySQL安装结束
转载请注明出处
|