安装 MySQL
查询是否安装了 mysql
代码语言:javascript复制rpm -qa|grep mysql卸载 mysql (下面是卸载 mysql 的库,防止产生冲突,mysql 也是类似卸载方式)
代码语言:javascript复制rpm -e --nodeps mysql-libs-5.1.*卸载之后,记得:find / -name mysql删除查询出来的所有东西
安装 mysql:yum install mysql-server
启动 mysql:
- 启动方式 1:
service mysql start - 启动方式 2:
/etc/init.d/mysql start - 启动方式 3:
service mysqld start - 启动方式 4:
/etc/init.d/mysqld start
root 账户默认是没有密码的,修改 root 密码:
/usr/bin/mysqladmin -u root password 密码
例如:/usr/bin/mysqladmin -u root password pwd 这样就将 root 密码设置成 pwd 了
重置 root 密码(忘记 root 密码找回)
- 停止 MySQL 服务命令:
/etc/init.d/mysqld stop或/etc/init.d/mysql stop - 输入绕过密码认证命令:
mysqld_safe --user=mysql --skip-grant-tables --skip-networking & - 输入登录用户命令:
mysql -u root mysql - 输入修改 root 密码 SQL 语句:
update user set Password=password ('123456') where user='root'; - 输入数据刷新命令:
FLUSH PRIVILEGES; - 退出 MySQL 命令:
quit;
设置允许远程连接:grant all privileges on *.* to root@'%' identified by '123456789' with grant option;
开放端口 3306,否则依然无法过远程
- 打开防火墙配置文件:
vi /etc/sysconfig/iptables - 添加下面一行:
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT - 注意:开通 3306 端口的行必须在 icmp-host-prohibited 前,否则无效:以下为配置结果 图:

- 重启防火墙,使配置生效:
/etc/init.d/iptables restart
设置开机启动 mysql:
- 查看 MySQL 服务是否自动开启命令
chkconfig --list | grep mysqldchkconfig --list | grep mysql
- 开启 MySQL 服务自动开启命令
chkconfig mysqld onchkconfig mysql on
将 mysql 默认引擎设置为 InnoDB
- 修改 MySQL 配置文件 my.cnf:
cd /etcvi my.cnf
- 在[mysqld]一段加入:
default-storage-engine=InnoDB - 删除 ib_logfile0、ib_logfile1 两个文件
cd /var/lib/mysqlrm -rf ib_logfile*
重启 mysql
- 开启 mysql 的日志(监控执行的 sql 语句)
- 命令:
show global variables like ‘%general%’;该语句可以查看是否开启, 以及生成的位置set global general_log = on;// 打开set global general_log = off;// 关闭
centos7 安装 mysql
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-community-server
成功安装之后重启 mysql 服务:
service mysqld restart
初次安装 mysql 是 root 账户是没有密码的,设置密码的方法
mysql -uroot
mysql> set password for ‘root’@‘localhost’ = password('mypasswd');
mysql> exit


