Centos7将默认数据库mysql替换成了Mariadb,如果想继续使用mysql 需要卸载Mariadb 再安装mysql;
rpm -qa |grep -i mariadb
yum remove MariaDB-*
注:可再次上面两条命令,检查是否卸载干净
- 删除 /etc/my.cnf
rm /etc/my.cnf
https://dev.mysql.com/downloads/mysql/5.6.html
Red Hat Enterprise Linux 7 / Oracle Linux 7 (x86, 64-bit), RPM Bundle
MySQL-5.6.40-1.el7.x86_64.rpm-bundle.tar
如果是在windows平台下载的tar包,可通过WinSCP软件拷贝到CentOS系统下
- 解压tar安装包
将下载的压缩包放到/usr/local/目录下, 执行解压缩命令
tar -xvf MySQL-5.6.40-1.el7.x86_64.rpm-bundle.tar
su - root
rpm -ivh MySQL-*
systemctl start mysql
ps -ef | grep mysql
cat /root/.mysql_secret
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in ‘/root/.mysql_secret’.You must change that password on your first connect,
no other statement but ‘SET PASSWORD’ will be accepted.
See the manual for the semantics of the ‘password expired’ flag.Also, the account for the anonymous user has been removed.
In addition, you can run:
/usr/bin/mysql_secure_installation
which will also give you the option of removing the test database.
This is strongly recommended for production servers.为MySQL的root用户设置了一个随机密码!
可以在“/root/.mysqlsecret”中找到该密码。你必须在第一次连接上更改密码
- 使用随机密码登陆后,更改密码
mysql -uroot -p
SET PASSWORD = PASSWORD('root');
1.创建配置文件
touch /usr/lib/systemd/system/mysql.service
2.编辑 /usr/lib/systemd/system/mysql.service 文件
vim /usr/lib/systemd/system/mysql.service
3.在mysql.service文件中添加如下配置:
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=http://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000
ExecStart=/usr/local/mysql/bin/mysqld (此处请对应修改为MySQL程序所在的路径)
4.设置开机启动
systemctl enable mysql.service
注:MySQL的默认安装位置
/var/lib/mysql/ #数据库目录 /usr/share/mysql #配置文件目录 /usr/bin #相关命令目录 /etc/init.d/mysql #启动脚本
/usr/bin/mysql_secure_installation
vim /etc/my.cnf
[client]
password = root
port = 3306
default-character-set=utf8
[mysqld]
port = 3306
character_set_server=utf8
character_set_client=utf8
collation-server=utf8_general_ci
#(注意linux下mysql安装完后是默认:表名区分大小写,列名不区分大小写; 0:区分大小写,1:不区分大小写)
lower_case_table_names=1
#(设置最大连接数,默认为 151,MySQL服务器允许的最大连接数16384; )
max_connections=1000
[mysql]
default-character-set = utf8
systemctl restart mysql
show variables like "%character%";
show variables like "%collation%";
此时要想远程登录数据库,还需要先设置权限
grant 权限 on 数据库.* to 用户名@登录主机 identified by “密码”;
使用grant 方式创建用户更完美:
当数据库存在用户的时候GRANT会对用户进行授权,但当数据库不存在该用户的时候,就会创建相应的用户并进行授权。
- 创建本地登录用户
create user username@localhost identified by 'password';
# 允许用户名为`username`的用户从任意ip以密码为password访问所有数据库
grant all privileges on *.* to username@'%' identified by 'password';
注意:
username@’%’ ,% 代表允许所有IP的username登录
username@localhost,localhost 代表允许本地username登录
[email protected],127.0.0.1 可以设置为单个IP,即允许单个远程主机登录
all privileges,代表所有权限
权限列表可被替换为一下任意组合:
select,insert,update,delete,create,drop,
index,alter,grant,references,reload,shutdown,process,file
查询所有用户权限列表
select Host,User from mysql.user;
查询user用户的权限
show grants for user;
单独授权user用户(查增改删)权限
GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,DROP,ALTER ON 数据库.* TO user@'%' IDENTIFIED BY '密码';
授权user用户凭密码本地访问所有数据库
grant all privileges on *.* to user@localhost identified by '密码';
授权user用户凭密码远程访问指定数据库
grant all privileges on 数据库.* to user@'%' identified by '密码';
授权user用户凭密码远程访问所有数据库
grant all privileges on *.* to user@'%' identified by '密码';
use mysql
SELECT User, Host, password FROM user;
show grants for user;
systemctl stop mysql
mysqld_safe --skip-grant-tables &
mysql -u root
update mysql.user set password=PASSWORD('newpassword') where User='root’;
flush privileges;
systemctl start mysql
MySQL创建用户**