Mysql基本操作
解释 | 命令 |
---|---|
安装服务端 | yum install mysql-community-server |
启动 | service mysqld start/restart |
停止 | service mysqld stop |
centos7默认安装了Mariadb数据库,我们先移除该数据库。
输入yum remove mariadb-libs.x86_64
安装mysql
配置YUM源
你可以在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo/yum/
下载mysql源安装包
wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
安装mysql源
yum localinstall mysql57-community-release-el7-8.noarch.rpm
检查mysql源是否安装成功
yum repolist enabled | grep "mysql.*-community.*
如图所示,表示安装成功。
可以修改vim /etc/yum.repos.d/mysql-community.repo
源,改变默认安装的mysql版本。比如要安装5.6版本,将5.7源的enabled=1改成enabled=0。然后再将5.6源的enabled=0改成enabled=1即可。改完之后的效果如下所示:
安装mysql
yum install mysql-community-server
启动MySQL服务
service mysqld restart
启动mysql服务ps -ef | grep mysql
查看状态
修改密码
输入grep 'temporary password' /var/log/mysqld.log
登录到mysql mysql -uroot -p
并输入上图所示密码。
输入ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';
或者set password for 'root'@'localhost'=password('新密码');
注意:mysql5.7默认安装了密码安全检查插件(validate_password),默认密码检查策略要求密码必须包含:大小写字母、数字和特殊符号,并且长度不能少于8位。否则会提示ERROR 1819 (HY000): Your password does not satisfy the current policy requirements错误,如下图所示
通过msyql环境变量可以查看密码策略的相关信息:
validate_password_policy:密码策略,默认为MEDIUM策略
validate_password_dictionary_file:密码策略文件,策略为STRONG才需要
validate_password_length:密码最少长度
validate_password_mixed_case_count:大小写字符长度,至少1个
validate_password_number_count :数字至少1个
validate_password_special_char_count:特殊字符至少1个
上述参数是默认策略MEDIUM的密码检查规则。
共有以下几种密码策略:
策略 | 检查规则 |
---|---|
0 or LOW | Length |
1 or MEDIUM | Length; numeric, lowercase/uppercase, and special characters |
2 or STRONG | Length; numeric, lowercase/uppercase, and special characters; dictionary file |
修改密码策略
在/etc/my.cnf
文件添加validate_password_policy
配置,指定密码策略
选择0(LOW),1(MEDIUM),2(STRONG)其中一种,选择2需要提供密码字典文件validate_password_policy=0
如果不需要密码策略,添加my.cnf文件中添加如下配置禁用即可:validate_password = off
重新启动mysql服务使配置生效:systemctl restart mysqld
添加远程登录用户
默认只允许root帐户在本地登录,如果要在其它机器上连接mysql,必须修改root允许远程连接,或者添加一个允许远程连接的帐户,为了安全起见,我添加一个新的帐户:
mysql> GRANT ALL PRIVILEGES ON *.* TO 'geekerstar'@'%' IDENTIFIED BY 'Geekerstar1!' WITH GRANT OPTION;
配置默认编码为utf8
修改/etc/my.cnf
配置文件,在[mysqld]下添加编码配置,如下所示:
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
重新启动mysql服务,查看数据库默认编码如下所示:
默认配置文件路径:
配置文件:/etc/my.cnf
日志文件:/var/log//var/log/mysqld.log
服务启动脚本:/usr/lib/systemd/system/mysqld.service
socket文件:/var/run/mysqld/mysqld.pid
附加操作
设置root可以远程连接
update
user set Host = '%' where User = 'root' and Host = 'localhost';
flush privileges;
或者重启服务 sudo service mysqld restart
关闭防火墙
sudo service firewalld stop
MySQL开启general_log跟踪数据执行过程
设置general log保存路径
注意在Linux中只能设置到 /tmp
或 /var
文件夹下,设置其他路径出错
需要root用户才有访问此文件的权限mysql> set global general_log_file='/tmp/general.log';
general log模式mysql> set global general_log=on;
闭general log模式mysql>set global general_log=off;
在eneral log
式开启过程中,所有对数据库的操作都将被记录 eneral.log
件
新建用户
CREATE USER 'imooc'@'%' IDENTIFIED BY '123456';
赋予权限GRANT ALL PRIVILEGES ON *.* TO 'imooc'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
GRANT select,insert,update,delete ON *.* TO 'imooc'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
FLUSH PRIVILEGES;
收回权限REVOKE ALL PRIVILEGES ON *.* FROM imooc;
FLUSH PRIVILEGES;
忘记root 密码
在 /etc/my.cnf
加入 skip-grant-tables
use mysql;
update user set authentication_string=password('456789') where user='root';
参考资料
如果对我的文章有任何意见或建议,以及有疑问需要我提供帮助,欢迎在下面留言,只需输入
昵称
+邮箱
即可,网站或博客
可选填。对于所有留言内容我会及时回复,期待与大家的交流![/scode]版权声明:本文为原创文章,版权归 Geekerstar 所有。本文链接:http://www.geekerstar.com/technology/312.html
除了有特殊标注文章外欢迎转载,但请务必标明出处,格式如上,谢谢合作。