首先执行安装命令(三句):
sudo apt-get install mysql-serversudo apt install mysql-clientsudo apt install libmysqlclient-dev查看是否安装成功
sudo netstat -tap | grep mysql显示下面这样就代表安装成功
尝试登录
mysql -uroot -p你的密码你有可能和我一样不知道 root 账户的密码是多少导致登录失败,没关系
/etc/mysql先进入这个目录 然后再执行下面一句
mysql -u debian-sys-maint -p出现
Enter password:直接回车
发现登录成功了,我们先来看一下root 角色信息
Welcome to the MySQL monitor. Commands end with ; or g.Your MySQL connection id is 26Server version: 5.7.23-0ubuntu0.18.04.1 (Ubuntu)Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.mysql> use mysqlReading table information for completion of table and column namesYou can turn off this feature to get a quicker startup with -ADatabase changed执行下面这句代码
select user,authentication_string from user;会看到
+------------------+-------------------------------------------+| user | authentication_string |+------------------+-------------------------------------------+| root | || mysql.session | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE || mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE || debian-sys-maint | *696F62E7F1B5F52AF683A21632A22815ABCB0845 |+------------------+-------------------------------------------+发现 root 的authentication_string 值为空的,我们需要改上自己的密码 ,执行下面这句代码
update user set authentication_string=PASSWORD("你的密码") where User='root'再执行下面三句 进行数据更新并保存退出
update user set plugin="mysql_native_password";flush privileges;quit;重启一下服务
sudo /etc/init.d/mysql stopsudo /etc/init.d/mysql start # reset mysqlOK 接下来就可以用 root 登录了
mysql -u root -p发现有出现
Enter password:没关系 直接输入密码,提醒一下 这里输入密码是不会显示的,输入完后直接回车就可以了
要想远程访问你还需要做下面的操作:首先编辑文件/etc/mysql/mysql.conf.d/mysqld.cnf
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf注释掉bind-address = 127.0.0.1:
保存退出,然后进入mysql服务,执行授权命令(这句是在登录MySQL状态下执行的):
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '你的密码' WITH GRANT OPTION;保存退出就可以了。
感谢 Alan_Xiang 分享的教程,我也是根据他的教程加上自己遇到的一些问题整理出来的 他的 原文地址 https://blog.csdn.net/xiangwanpeng/article/details/54562362