MySQL环境搭建
'''进入本机源文件目录'''
cd /usr/local/src
'''wget下载yum源的rpm包'''
wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
'''安装rpm包'''
rpm -ivh mysql57-community-release-el7-11.noarch.rpm
'''使用yum安装mysql-server'''
yum install -y mysql-server
'''启动mysqld'''
systemctl start mysqld
'''查看启动mysqld'''
ps aux|grep mysqld
'''设置开机自动启动'''
systemctl enable mysqld
'''使用初始密码登陆'''
由于MySQL从5.7开始不允许首次安装后,使用空密码进行登录,系统会随机生成一个密码以供管理员首次登录使用,这个密码记录在/var/log/mysqld.log文件中
使用下面的命令可以查看此密码:
cat /var/log/mysqld.log|grep 'A temporary password'
2017-11-12T13:35:37.013617Z 1 [Note] A temporary password is generated for root@localhost: ,QZcc6_y&tDf
最后一行冒号后面的部分,QZcc6_y&tDf就是初始密码。
'''使用刚刚查看的密码登陆mysql'''
mysql -u root -p
'''更改密码'''
use mysql;
alter user 'root'@'localhost' identified by 'your_password';
MySQL 远程访问
'''将host设置为%表示任何ip都能连接mysql'''
mysql -u root -h localhost -p
use mysql;
update user set host='%' where user='root' and host='localhost';
'''当然也可以将host指定为某个ip'''
update user set host='106.39.178.131' where user='root' and host='localhost';
'''执行完以上语句,接着执行以下语句 ,刷新权限表,使配置生效'''
flush privileges;
'''最后开放阿里云3306端口就可以啦'''
常见错误
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing
'''解决方法'''
ALTER USER 'root'@'localhost' IDENTIFIED BY '密码' PASSWORD EXPIRE NEVER;
flush privileges;
ERROR 1820更多解决方案
ERROR 1130: host 'localhost' not allowed to connect to this MySQLserver
'''解决方法'''
mysql>use mysql; # 查询出数据库的用户名
mysql>select host,user,password from user; # 把用户表里面的host项改为“%”
mysql>update user set host = '%' where user ='root';
mysql>flush privileges; #刷新用户权限表
# 重启mysql即可
ERROR 1130更多解决方案