本文整理了下CentOS7下安装mysql的基本配置。

安装

wget https://dev.mysql.com/get/Downloads/MySQL-4.7/mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar

tar -xvf mysql-5.7.22-1.el7.x86_64.rpm-bundle.tar

rpm -ivh mysql-community-common-5.7.22-1.el7.x86_64.rpm \
  mysql-community-libs-5.7.22-1.el7.x86_64.rpm  \
  mysql-community-client-5.7.22-1.el7.x86_64.rpm \
  mysql-community-server-5.7.22-1.el7.x86_64.rpm \
  mysql-community-devel-5.7.22-1.el7.x86_64.rpm

注意:

  1. 如果已经安装mariadb,需要删除 yum -y remove mariadb mariadb-libs mariadb-server
  2. 需要安装perl yum -y install perl perl-JSON

使用mysql

启动与结束命令

service mysqld start
service mysqld stop

# 使用密码登录mysql
mysql -u root -p

找到并修改初始密码

参考:

  1. 通过cat /var/log/mysqld.log | grep password找到临时密码
  2. mysql -u root -p输入临时密码,连接到mysql
  3. 修改初始密码 SET PASSWORD FOR 'root'@'localhost' = PASSWORD('MysqlPasswordXXX');

注意:默认密码级别要求比较高,如果报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements,可以通过set global validate_password_policy=0;来设置降低密码级别

如果找不到临时的初始密码,可以新建密码

参考:https://www.jianshu.com/p/707190d13e5c

  1. 修改/etc/my.cnf文件,在[mysqld]下添加 skip-grant-tables, 再重新启动mysql
  2. 然后用空密码方式使用root用户登录MySQL: mysql -u root
  3. 修改root用户的密码
    mysql> update mysql.user set password=PASSWORD('new_password') where User='root';
    mysql> flush privileges;
    mysql> quit

远程访问的权限

默认mysql不支持远程访问,需要设置放开权限:

grant all privileges on *.* to 'root'@'%' identified by '[password]' with grant option;
flush privileges;

然后就能远程访问了:mysql -h xx.xx.xx.xx -P 3306 -u root -p

参考:https://blog.csdn.net/u010758410/article/details/76381632

mysql常用操作

参考mysql教程: http://www.runoob.com/mysql/mysql-create-tables.html

show databases;
use db_name;
show tables;
exit

其他参考