Oracle官方版MySQL安装(CentOS/Redhat)

CentOS7/Redhat7系列

wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm
rpm -ivh mysql57-community-release-el7-11.noarch.rpm

CentOS6/Redhat6系列

wget https://dev.mysql.com/get/mysql57-community-release-el6-11.noarch.rpm
rpm -ivh mysql57-community-release-el6-11.noarch.rpm

切换repo的MySQL5.5/5.6/5.7版本,下面是关闭5.7并启用5.6,目前建议使用5.7,下面命令可以不执行

sudo yum-config-manager --disable mysql57-community
sudo yum-config-manager --enable mysql56-community

刷新repo

yum makecache

使用yum安装,网速比较慢要等等,如果提示失败可以强制打断后反复执行

yum install -y mysql-community-server

启动服务,备注:关闭服务systemctl stop mysqld

service mysqld start
systemctl start mysqld

查看服务

service mysqld status
systemctl status mysqld

设置服务开机启动,备注:取消开机启动systemctl disable mysqld

systemctl enable mysqld

5.7版本修改初始root密码

grep 'temporary password' /var/log/mysqld.log

可以看到初始安装过程的密码,然后用命令行登录Mysql修改root密码为MyNewPass4!

mysql -uroot -p

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY 'MyNewPass4!';

5.6版本修改初始密码(因为在安装过程中设置了),5.7仍然需要找到初始密码才能用

mysql_secure_installation

开启MySQL远程访问权限 允许远程连接

1、登录服务器,然后运行命令:mysql -u root –p ,然后输入密码,该步骤是进入数据库。
2、mysql>use mysql;
3、授权:
例如想root使用123456从任何主机连接到mysql服务器:

mysql>GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;

如果想允许用户abc从ip为10.10.50.127的主机连接到mysql服务器,并使用654321作为密码:

mysql>GRANT ALL PRIVILEGES ON *.* TO 'abc'@'10.10.50.127' IDENTIFIED BY '654321' WITH GRANT OPTION;

4、刷新权限:

mysql>flush privileges; 

5、关闭防火墙并取消防火墙开机启动

systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

PS:忘记密码怎么办

1.首先停止mysql服务器

service mysqld stop  

2.使用--skip-grant-tables选项跳过授权表验证,
--skip-grant-tables Start without grant tables. This gives all users FULL ACCESS to all tables.

使用--skip-grant-tables启动mysql服务器

mysqld --skip-grant-tables --user=mysql   

回车之后就不要动了,打开一个新的命令窗口,原来的不要关,查看启动情况

ps -ef | grep mysql  
mysql    10209 14240  4 13:52 pts/0    00:00:00 mysqld --skip-grant-tables --user=mysql  
root     10229 14240  0 13:53 pts/0    00:00:00 grep mysql  

3.我们连接并切换到mysql数据库下尝试
mysql -u root –p

root@localhost[(none)]> use mysql;     

4.尝试用password函数方式来更新authentication_string列 (mysql5.7以后要求密码大小写字母数字及符号)

root@localhost[mysql]> update user set authentication_string=password('MyNewPass4!') where user='root'; --此方式更新成功  
Query OK, 1 row affected (0.04 sec)  
Rows matched: 1  Changed: 1  Warnings: 0  

5.验证--可以看到密码已经变成了密文

root@localhost[mysql]> select host,user,authentication_string from user where user='root';    
+-----------+------+-------------------------------------------+  
| host      | user | password                                  |  
+-----------+------+-------------------------------------------+  
| localhost | root | *59C70DA2F3E3A5BDF46B68F5C8B8F25762BCCEF0 |  
+-----------+------+-------------------------------------------+  

6.刷新权限

root@localhost[mysql]> flush privileges;  
Query OK, 0 rows affected (0.00 sec)

7.最好重启mysql

service mysqld restart
systemctl restart mysqld.service

优化:修改最大连接数

1.修改系统文件打开限制/etc/security/limits.conf
mysql hard nofile 65535
mysql soft nofile 65535
2.修改my.cnf
在[mysqld_safe]部分加入
max_connections=5000
3.修改/lib/systemd/system/mysqld.service
在[Service]部分加入
LimitCORE=infinity
LimitNOFILE=100001
LimitNPROC=100001
4.重载
systemctl daemon-reload
systemctl restart mysqld.service

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容