CentOS7的yum源中默认好像是没有mysql的。为了解决这个问题,我们要先下载mysql的repo源。
1. 下载mysql的repo源
$ wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
2. 安装mysql-community-release-el7-5.noarch.rpm包
$ sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
安装这个包后,会获得两个mysql的yum repo源:/etc/yum.repos.d/mysql-community.repo,/etc/yum.repos.d/mysql-community-source.repo。
3. 安装mysql
$ sudo yum install mysql-server
根据步骤安装就可以了,不过安装完成后,没有密码,需要重置密码。
4. 重置密码
重置密码前,首先要登录
$ mysql -u root
登录时有可能报这样的错:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2),原因是/var/lib/mysql的访问权限问题。下面的命令把/var/lib/mysql的拥有者改为当前用户:
$ sudo chown -R openscanner:openscanner /var/lib/mysql
然后,重启服务:
$ service mysqld restart
接下来登录重置密码:
$ mysql -u root
mysql >use mysql
mysql >update user set password=password('123456') where user='root'
mysql >exit
5. 开放3306端口
$ sudo vim /etc/sysconfig/iptables
添加以下内容:
-A INPUT -p tcp -m state --state NEW -m tcp --dport 3306 -j ACCEPT
保存后重启防火墙:
$ sudo service iptables restart
6. 创建普通用户并授权
示例(使用root用户登录,并假定已经创建了openscannerstore数据库):
mysql >use mysql
#创建openscanner用户与密码并设置为从安装mysql服务的机器本地访问
mysql >grant all on openscannerstore.* to 'openscanner'@'localhost'
identified by 'scanner888'
#设置openscanner用户与密码,并从任何机器都可以访问mysql
mysql >grant all on openscannerstore.* to 'openscanner'@'%' identified by 'scanner888'
mysql >flush privileges #刷新才会生效
创建新的用户名和密码create
user
username
identified
by
password
给用户分配权限
GRANT
privileges
ON
databasename.tablename
TO
'username'@'host'
grant
all
privileges
on
*.*
to
'username'@'localhost'
identified
by
'password'
删除权限
REVOKE
ALL
PRIVILEGES
ON
*.*
FROM
'username'@'localhost'
分配指定库的权限
GRANT
ALL
PRIVILEGES
ON
database1.*
TO
'username'@'localhost'
IDENTIFIED
BY
'password'
分配指定 *** 作的权限
GRANT
SELECT,
UPDATE
ON
database1.*
TO
'username'@'localhost'
IDENTIFIED
BY
'password'
刷新权限
FLUSH
PRIVILEGES
删除用户
DROP
USER
username@localhost
设置默认的编码
修改/etc/my.cnf配置文件,在[mysqld]下添加编码配置,如下所示
character_set_server=utf8
init_connect='SET
NAMES
utf8'
重启mysql
systemctl
restart
mysqld
更新用户的密码
SET
PASSWORD
FOR
'username'@'localhost'
=
PASSWORD("123456")
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)