MySQL 环境搭建

CentOS7 配置MySQL5.7

下载源,安装MySQL 
$ wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm
$ sudo yum localinstall mysql57-community-release-el7-8.noarch.rpm
$ sudo yum repolist enabled | grep "mysql.*-community.*"
$ sudo yum install mysql-community-server

启动MySQL服务,设置开机启动
$ sudo systemctl start mysqld
$ systemctl status mysqld
$ sudo systemctl enable mysqld
$ sudo systemctl daemon-reload

修改root密码
$ grep 'temporary password' /var/log/mysqld.log #查看临时密码:本次是R*)YnafK;2(_
$ mysql -uroot -p #输入临时密码:R*)YnafK;2(_
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'Baozi123!';
mysql> exit

禁用密码策略与默认编码
$ mkdir ~/backup/mysql
$ cp /etc/my.cnf ~/backup/mysql/ #备份一份my.cnf
$ sudo vim /etc/my.cnf
    [mysqld]
    validate_password=off
    character_set_server=utf8
    init_connect='SET NAMES utf8'
$ sudo systemctl restart mysqld #重启生效

重新修改root密码和创建新角色
$ mysql -uroot -p 输入密码:Baozi123!
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'root' WITH GRANT OPTION; #root添加远程连接
mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'root';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'baozi'@'%' IDENTIFIED BY 'ok' WITH GRANT OPTION; #创建新角色
mysql> flush privileges;
mysql> exit

重新登录测试,顺便查看字符编码和密码策略
$ mysql -uroot -p 输入密码:root
mysql> show variables like '%password%';
mysql> show variables like '%character%';

MacOS 配置MySQL5.7

方式一:官网下载
下载安装之后有一个提示,比如我这次是: rLPC3Yl5B_5;
点开系统偏好设置。最下面有MySQL,点击,然后启动MySQL
启动:$/usr/local/mysql/bin/mysql -uroot -p 登录:rLPC3Yl5B_5
修改密码:ALTER USER 'root'@'localhost' IDENTIFIED BY '新密码';

方式二:使用homebrew
\$ brew install mysql
\$ bash mysql.server start

你可能感兴趣的:(MySQL 环境搭建)