wget http://repo.mysql.com/mysql-community-release-el6-5.noarch.rpm
安装源,安装之后, /etc/yum.repos.d/目录下会产生两个文件:mysql-community-source.repo、mysql-community.repo。
sudo rpm -ivh mysql-community-release-el6-5.noarch.rpm
sudo yum update
因为默认安装的是5.6版本,如果需要安装其它版本,需要修改yum源,我这里安装的是5.7版本,所以我需要修改源的配置
sudo vim /etc/yum.repos.d/mysql-community.repo
里面会有3个源,分别为5.5、5.6、5.7的安装
通过修改enabled=1选项来设置默认安装的版本,如果需要安装5.7版本,则将5.7配置修改为
[mysql57-community-dmr]
name=MySQL 5.7 Community Server Development Milestone Release
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/6/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:/etc/pki/rpm-gpg/RPM-GPG-KEY-mysql
注意:同时只能有一个版本的enabled=1,否则会造成冲突。
将5.6的enabled属性修改为
enabled=0
sudo yum install mysql-server
使用mysqld --initialize初始化mysql
–initialize :初始化时,会生成一个root用户和一个随机的密码,并且密码设置为过期,需要立即修改密码。确保数据库目录与文件的所有者为mysql账户,以便在运行mysqld服务时对这些目录和文件有读取与写入权限。以root身份运行mysqld服务,需指定–user=mysql选项。
sudo mysqld --initialize --user=mysql
初始化后,会生成一个root用户(mysql的用户,非系统的root用户)和一个随机密码。
获取密码
grep 'temporary password' /var/log/mysqld.log
输出为
[hadoop@hnode1 yum.repos.d]$ grep 'temporary password' /var/log/mysqld.log
2019-02-22T01:25:35.730507Z 1 [Note] A temporary password is generated for root@localhost: uhUJwbj4Va:!
启动Mysql
sudo service mysqld start
查看mysql状态
sudo service mysqld status
输出为
[root@hnode1 ~]# service mysqld status
mysqld (pid 31682) is running...
修改root用户密码
编辑/etc/my.cnf文件, 在 [mysqld] 中加上一行跳过权限限制(skip-grant-tables)
vim /etc/my.cnf
新增
skip-grant-tables
重启mysql
service mysqld restart
登录mysql
mysql -uroot -p
提示输入密码时直接回车即可
在mysql命令行,执行下面命令
进入mysql数据库
use mysql
修改密码
update user set authentication_string=password('123456') where user='root';
刷新
flush privileges;
退出,然后编辑/etc/my.cnf文件, 在 [mysqld] 中删掉一行跳过权限限制(skip-grant-tables)
vim /etc/my.cnf
删除
skip-grant-tables
执行
mysql -uroot -p
输入错误密码,验证是否能登录
[root@hnode1 ~]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root:
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
Press y|Y for Yes, any other key for No: y
There are three levels of password validation policy:
LOW Length >= 8
MEDIUM Length >= 8, numeric, mixed case, and special characters
STRONG Length >= 8, numeric, mixed case, special characters and dictionary file
Please enter 0 = LOW, 1 = MEDIUM and 2 = STRONG: 0
Using existing password for root.
Estimated strength of the password: 25
Change the password for root ? ((Press y|Y for Yes, any other key for No) : n
######是否修改root用户密码
... skipping.
By default, a MySQL installation has an anonymous user,
allowing anyone to log into MySQL without having to have
a user account created for them. This is intended only for
testing, and to make the installation go a bit smoother.
You should remove them before moving into a production
environment.
######是否移除匿名用户
Remove anonymous users? (Press y|Y for Yes, any other key for No) : y
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
##############是否禁止mysql的root用户远程登录,测试环境,所以允许
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : n
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
########是否移除test数据库,测试环境,保留
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : n
... skipping.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
#######是否重新加载权限表
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y
Success.
All done!
sudo chkconfig mysqld on
mysql -uroot -p
在mysql命令行执行
use mysql
update user set host = '%' where user ='root';
flush privileges;