P1-Linux下安装MySQL及登录用户配置
Linux下安装MySQL及登录用户配置
环境:Centos7.4 Mysql5.6
1. 查询是否安装 MySQL和MariaDB
rpm -qa | grep -i mysql
rpm -qa | grep -i maria
2. 卸载已有的 MySQL 和 MariaDB
将1中查询结果一一删除
yum remove mysql-community mysql-community-server mysql-community-libs mysql-community-common
yum remove maria** (不可用*,须改为确切名称)
3. 确认是否完全卸载
rpm -qa | grep -i mysql
4. 下载yum源安装(Centos7.4中已经不包含 MySQL 的源)
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
5. 安装 MySQL
yum install mysql-community-server
6. 修改配置文件
vim /etc/mysqld
在 [mysqld] 下添加如下行:
character-set-server=utf8
default-character-set=utf8
修改此行(没有则添加)
datadir=/opt/data #安装时未指定,则缺省路径为/opt/data,默认的配置文件不是此路径,会导致服务无法启动
7. 启动MySQL
systemctl restart mysqld.service
8. 查看运行状态
systemctl status mysqld.service
注:如果启动失败,可以去 /var/log/mysqld.log 查看日志,以确认错误原因
9. 本机登录
mysql -u root -p #首次登陆默认密码为空
# 删除所有用户名为空的账户
delete from mysql.user where user = '';
# 给 root 账户设置密码
set password from root@localhost = password("123456");
set password from root@'127.0.0.1' = password("123456");
# 创建新用户并附权限
GRANT ALL PRIVILEGES ON *.* TO 'user01'@'localhost' IDENTIFIED BY '123456' WITH GRANT OPTION;
FLUSH PRIVILEGES;