查看是否安装Mysql
sudo netstat -tap | grep mysql
如果为空则没有安装,进行安装:
sudo apt-get install mysql-server mysql-client
根据提示输入密码,然后确认..
Mysql 连接测试
mysql -u root -h localhost -p
启动,停止,重启 Mysql命令
sudo service mysql start
sudo service mysql stop
sudo service mysql restart
Ubuntu 安装 mysql 忘记初始密码解决方法
//打开这个文件 /etc/mysql/debian.cnf
//查看默认分配的密码
[client]
host = localhost
user = debian-sys-maint
password = eyPDN7kavhmjCZUn (记住这个密码)
socket = /var/run/mysqld/mysqld.sock
输入命令进入mysql修改用户密码
// 输入命令后把上面的密码粘贴进去
mysql -udebian-sys-maint -p
//进入到mysql界面厚修改密码
update mysql.user set authentication_string=password('newpassword') where user='root'
//都要使用刷新权限列表
flush privileges;
重新启动Mysql服务即可
如果出现下列错误请参考解决方式
ERROR 1698 (28000): Access denied for user ‘root’@’localhost’
https://stackoverflow.com/questions/39281594/error-1698-28000-access-denied-for-user-rootlocalhost
============================================================================================
============================================================================================
============================================================================================
The reason is that recent Ubuntu installation (maybe others also), mysql is using by default the UNIX auth_socket plugin.
Basically means that: db_users using it, will be "auth" by the system user credentias. You can see if your root
user is set up like this by doing the following:
$ sudo mysql -u root # I had to use "sudo" since is new installation
mysql> USE mysql;
mysql> SELECT User, Host, plugin FROM mysql.user;
+------------------+-----------------------+
| User | plugin |
+------------------+-----------------------+
| root | auth_socket |
| mysql.sys | mysql_native_password |
| debian-sys-maint | mysql_native_password |
+------------------+-----------------------+
As you can see in the query, the root
user is using the auth_socket
plugin
There are 2 ways to solve this:
- You can set the root user to use the
mysql_native_password
plugin - You can create a new
db_user
with yousystem_user
(recommended)
Option 1:
$ sudo mysql -u root # I had to use "sudo" since is new installation
mysql> USE mysql;
mysql> UPDATE user SET plugin='mysql_native_password' WHERE User='root';
mysql> FLUSH PRIVILEGES;
mysql> exit;
$ service mysql restart
Option 2: (replace YOUR_SYSTEM_USER with the username you have)
$ sudo mysql -u root # I had to use "sudo" since is new installation
mysql> USE mysql;
mysql> CREATE USER 'YOUR_SYSTEM_USER'@'localhost' IDENTIFIED BY '';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'YOUR_SYSTEM_USER'@'localhost';
mysql> UPDATE user SET plugin='auth_socket' WHERE User='YOUR_SYSTEM_USER';
mysql> FLUSH PRIVILEGES;
mysql> exit;
$ service mysql restart
=======================================================
=======================================================
=======================================================
可以通过如下命令进入MySQL服务:
mysql -uroot -p你的密码
现在设置mysql允许远程访问,首先编辑文件/etc/mysql/mysql.conf.d/mysqld.cnf:
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
注释掉bind-address = 127.0.0.1:
保存退出,然后进入mysql服务,执行授权命令:
grant all on *.* to root@'%' identified by '你的密码' with grant option;
flush privileges;
然后执行quit命令退出mysql服务,执行如下命令重启mysql:
service mysql restart