mysql linux上安装使用

安装启动

安装之前可以看下系统中有没有已经安装。

查看所有软件:dpkg -l  

1、查看mysql安装的版本

mysql --version

2、mysql状态

service mysql status

3、打开数据库

service mysql start

4、关闭数据库:

service mysql stop

 不会有输出内容,继续输入  service mysql status

5、异常:control process exit,code=exited,status=1/failure

问题:配置mysql远程访问一开始可以启动,但后来关掉mysql服务一直重启失败

一开始以为是因为进程结束的方式错误,所以就重启了一下服务器;后来发现重启服务器后也没什么用,还是mysql.service failed because the control process exited with error code这个错误,一直以为错误出在mysql.service:main process exit,code=exited,status=1/failure,一直找不到解决方法!

后来想了一下,我在mysql服务没停的时候在/etc/mysql/my.cnf->/etc/alternatives/my.cnf文件里加了一行”bind-address=0.0.0.0”,之后启动mysql服务就一直失败。大概猜到了原因:出现此类错误可能是由配置文件错误造成的。
把新加的bind-address=0.0.0.0 删掉,然后停止数据库,再启动就可以了!

并且这行加的位置也不对,应该加在/etc/mysql/mysql.conf.d/mysqld.cnf 配置文件里. 

设置密码

默认密码为空,用户不用输入密码,直接回车登陆

mysql -uroot -p

password:(空)

选择数据库:use mysql; 
修改密码:update user set password=PASSWORD('123456') where user='root'; 

【可能报错,见下文】
刷新数据库权限:flush privileges; 
退出数据库:exit; 

这样就完成了。

报错

1、Unknown column'password' in fieldlist 错误

原因是mysql数据库下已经没有password这个字段了,password字段改成了 authentication_string

update user set authentication_string=PASSWORD('123456') where user='root';

2、ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

密码不符合当前的密码机制,可能是自己在什么时候设置过密码等级。查看一下有关的密码参数:

mysql> SHOW VARIABLES LIKE 'validate_password%';

在这里,我的mysql已经开启了中级密码验证,密码长度至少为8,包含大小写字母、数字和特殊字符。如果选用这种密码验证,那只要设置的密码包含以上所写的要求即可。

Linux上操作mysql

1、进入mysql数据库

root@test:/home# mysql -uroot -pPassword   <root是用户名,Password是密码>

2、查询所有的库

mysql> show databases; 

3、进入数据库“eduyun_2015_sp1”是库名

mysql> use eduyun_2015_sp1;

4、查询所有的表

mysql> show tables;

5、查询表,进行增删改查“ey_transcodesplit”是表

mysql> select * from ey_transcodesplit; 

6、"exit" + 回车;

远程登录

云服务的话 需要先在安全组中添加端口。

1、报错:Host XXX is not allowed to connect to this MySQL server

原因:这是由于Mysql配置了不支持远程连接引起的。

在安装Mysql数据库的主机上登录root用户: mysql -u root -p

依次执行如下命令:

use mysq;

select host from user where user='root';

可以看到当前主机配置信息为localhost.

修改:

再执行 update user set host = '%' where user ='root'  将Host设置为通配符%

Host设置了“%”后便可以允许远程访问。

执行flush privilegs使配置立即生效。

2、报错:1251 client does not support authentication ...

主机上登录root用户: mysql -u root -p

(1)查看用户信息

select host,user,plugin,authentication_string from mysql.user;

 

备注:host为 % 表示不限制ip   localhost表示本机使用    plugin非mysql_native_password 则需要修改密码

(2) 更新用户密码【尽管你之前已经设置了一次】

ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';      #更新一下用户的密码 root用户密码为123456 

#清除一下缓存
flush privileges;

原文地址:https://www.cnblogs.com/peterYong/p/11408588.html