linux下mysql的基本使用

安装mysql

CentOS7默认安装mariadb数据库,在安装mysql之前先把mariadb移除

yum remove mariadb-libs.x86_64

下载Mysql源

https://dev.mysql.com/downloads/repo/yum/

安装源

yum localinstall mysql57-community-release-el7-11.noarch.rpm

安装社区版mysql

yum install mysql-community-server

启动mysql

service mysqld start

查看默认密码

cat /var/log/mysqld.log | grep "password"

第一次使用默认密码连接mysql

mysql -uroot -p

如果不修改默认密码的话是使用不了mysql的一些功能的,所以我们来修改一下默认密码吧。mysql设置密码默认是要求是字母+数字+字符混合的,如果你要设置简单的密码比如:123456会报Your password does not satisfy the current policy requirements的错误。如果我们一定要设置简单的密码就必须修改一下mysql 的两个全局参数.

set global validate_password_policy=0;
set global validate_password_length=1;

设置新密码

set password = password('123456');

ps:只有第一次修改密码才能用此函数。


开启远程链接

选择mysql数据库,修改user表中user='root'的Host的字段,从原来的localhost改为% 表示允许所有ip用root账号登陆。

use mysql;
update user set Host='%' where Host = 'localhost' and user='root';

重启mysql的服务

service mysqld restart

开启genelog服务

  • 设置log的文件存储地址
set global general_log_file="/tmp/general.log";
  • 开启genelog
set global general_log=on;
  • 关闭genelog
set global general_log=off;

新建用户与授权

新建用户

create user 'test'@'%' identified by '123456';

%表示所有ip都能远程连接,如果报错Your password does not satisfy the current policy requirements则执行**set global validate_password_policy=0;set global validate_password_length=1;**两个命令

授权

grant all privileges on *.* to 'test'@'%' identified by '123456' with grant option

对用户test授予所有库所有表的所有权限。

grant select privileges on mysql.user to 'test'@'%' identified by '123456' with grant option;

对用户test授予mysql库user表的查询权限(其他的还有update,insert,delete权限)

revoke all privileges on *.* from test;

删除用户test的所有库所有表的所有权限,有些权限如果是追加的,则也要追加删除。例如 revoke select on mysql.user from test;

忘记密码

有时候我们会忘记mysql的root的密码,这时候就采用以下这种方法就可以轻松重置密码了而不用重装mysql。

进入mysql的配置文件

vim /etc/my.cnf

在最后面添加一行

skip-grant-tables

保存配置文件并且重启mysql服务

service mysqld restart

这时候就可以不用密码登陆mysql了

mysql -uroot -p

进入mysql库执行设置密码的sql

use mysql
update user set authentication_string = password('456789') where user = 'root';

最后将配置文件的skip-grant-tables去掉,重启mysql服务即可。

猜你喜欢

转载自my.oschina.net/u/3628952/blog/1791325
今日推荐