CentOS MySql 安装

##YUM源安装

Installing MySQL on Linux Using the MySQL Yum Repository

Installing MySQL on Unix/Linux Using Generic Binaries

或者使用MariaDB Database替代MySQL(是MySQL的完整分支)

http://www.jianshu.com/p/15a1659bc38d

http://www.bkjia.com/Mysql/1115394.html

卸载mariadb

rpm -qa | grep mariadb # 是否安装了mariadb
rpm -e mariadb-libs # 卸载
# rpm -e --nodeps mariadb-libs ## 强力卸载,忽略依赖
yum install vim libaio net-tools # 安装mysql依赖,可先检查是否安装了依赖

下载rpm包

一般的yum源中可能没有mysql的rpm,故需要先下载mysql的repo源

wget http://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm

安装源

下面命令将 MySQL 的 Yum repository 添加到系统的仓库列表之中,并下载 GnuPG key 来检查软件包的完整性(integrity)(可以用yum repolist enabled | grep "mysql.*-community.*"检查)

sudo yum rpm -ivh mysql57-community-release-el7-9.noarch.rpm

查看子仓库,选择发行版

[houzw@MiWiFi-R1CM softwares]$ yum repolist all | grep mysql
mysql-connectors-community/x86_64 MySQL Connectors Community 启用: 24
mysql-connectors-community-source MySQL Connectors Community - Sour 禁用
mysql-tools-community/x86_64 MySQL Tools Community 启用: 38
mysql-tools-community-source MySQL Tools Community - Source 禁用
mysql-tools-preview/x86_64 MySQL Tools Preview 禁用
mysql-tools-preview-source MySQL Tools Preview - Source 禁用
mysql55-community/x86_64 MySQL 5.5 Community Server 禁用
mysql55-community-source MySQL 5.5 Community Server - Sour 禁用
mysql56-community/x86_64 MySQL 5.6 Community Server 禁用
mysql56-community-source MySQL 5.6 Community Server - Sour 禁用
mysql57-community/x86_64 MySQL 5.7 Community Server 启用: 146
mysql57-community-source MySQL 5.7 Community Server - Sour 禁用
mysql80-community/x86_64 MySQL 8.0 Community Server 禁用
mysql80-community-source MySQL 8.0 Community Server - Sour 禁用

安装最新发行版时,不需要任何的配置。如果要安装其他版本,必须禁用最新版本,启用相应的版本

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

或者手动编辑/etc/yum.repos.d/mysql-community.repo 文件

[mysql57-community]
name=MySQL 5.7 Community Server
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

如果想安装5.6

# Enable to use MySQL 5.6
[mysql56-community]
name=MySQL 5.6 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.6-community/el/6/$basearch/
enabled=1 # 启用 。 设置5.7 的enabled=0 表示不启用5.7
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

查看启用的版本

yum repolist enabled | grep mysql

安装mysql-server

sudo yum install mysql-server

然后会进行下载安装相应的软件

mysql-community-server x86_64 5.7.16-1.el7 mysql57-community
# 为依赖而安装:
mysql-community-client x86_64 5.7.16-1.el7 mysql57-community
mysql-community-common x86_64 5.7.16-1.el7 mysql57-community
mysql-community-libs

RPM二进制包安装

安装参考:

官方:http://dev.mysql.com/doc/refman/5.7/en/binary-installation.html

博客(主要参考):

linux 下安装mysql-5.7.16-1.el6.x86_64.rpm-bundle

CentOS6.6-64下rpm方式mysql-5.7.12安装实录

下载

wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar

解压

tar -xf mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar

得到一系列安装包

mysql-community-client-5.7.16-1.el7.x86_64.rpm
mysql-community-common-5.7.16-1.el7.x86_64.rpm
mysql-community-devel-5.7.16-1.el7.x86_64.rpm
mysql-community-embedded-5.7.16-1.el7.x86_64.rpm
mysql-community-embedded-compat-5.7.16-1.el7.x86_64.rpm
mysql-community-embedded-devel-5.7.16-1.el7.x86_64.rpm
mysql-community-libs-5.7.16-1.el7.x86_64.rpm
mysql-community-libs-compat-5.7.16-1.el7.x86_64.rpm
mysql-community-minimal-debuginfo-5.7.16-1.el7.x86_64.rpm
mysql-community-server-5.7.16-1.el7.x86_64.rpm
mysql-community-server-minimal-5.7.16-1.el7.x86_64.rpm
mysql-community-test-5.7.16-1.el7.x86_64.rpm

依次安装(官方说要安装依赖,不过一般都已经有了 yum install libaio)

sudo rpm -ivh mysql-community-common-5.7.16-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-libs-5.7.16-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-client-5.7.16-1.el7.x86_64.rpm
sudo rpm -ivh mysql-community-server-5.7.16-1.el7.x86_64.rpm

安装后可以查看

rpm -qa | grep mysql

MySQL使用

启动mysql

$ sudo service mysqld start
Redirecting to /bin/systemctl start mysqld.service

启动后查看日志获取随机密码

sudo cat /var/log/mysqld.log | moregrep "password" /var/log/mysqld.log直接在文件中查找

[Note] A temporary password is generated for root@localhost: h4aci)6GVpxw

停止:sudo service mysqld stop
重启:sudo service mysqld restart

登录mysql,修改初始密码

mysql -uroot -p

然后修改密码(注意:新密码必须包含有数字,大写字母,小写字母,特殊符号,否则报错,不能修改)

mysql> alter user 'root'@'localhost' identified by 'newpassword';
mysql> flush privileges;

或者mysql> set password= PASSWORD("newpassword");修改当前用户密码,加密保存

打开3306端口

/sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT

或使用firewall-cmd命令

若 FirewallD is not running可以执行 systemctl start firewalld 开启防火墙。

开启端口

firewall-cmd --zone=public --add-port=3306/tcp --permanent

MySQL管理

  • 查看用户

mysql> select distinct concat('User: ''',user,'''@''',host,''';') as usr from mysql.user;

  • 查看数据库中具体某个用户的权限

mysql> show grants for ['cactiuser'@'%'](mailto:'cactiuser'@'%');

  • 查看user表结构 需要具体的项可结合表结构来查询

mysql> desc mysql.user;

  • 修改密码(不用登录mysql)

mysqladmin -u用户名 -p旧密码 password 新密码

  • 修改密码(登录mysql之后)

mysql> update user set password=password(“newpwd”) where user=’root’;// 修改密码
mysql> flush privileges; //刷新数据库

  • 创建用户,用%可以代表任意主机,也可以用all privileges代表所有权限。

mysql> grant all privileges on 数据库.* to '用户名'@'登录主机' identified by '密码';

语法:GRANT privileges ON databasename.tablename TO 'username'@'host';

其中,host可以是localhost%(任意ip)或具体的ip地址

  • 或者
mysql> CREATE USER 'user'@'localhost' IDENTIFIED BY '123456'; 
mysql> SET PASSWORD FOR 'username'@'host' = PASSWORD('newpassword'); --修改密码,加密保存
       --如果是当前登陆用户用 SET PASSWORD = PASSWORD("newpassword");
  • 修改root可以在其他地址登录

mysql> grant all privileges on *.* to 'root'@'%' identified by '<pw>';

或使用

mysql> update user set host = '%' where user = 'root';
mysql> flush privileges;
  • 忘记密码时,可用如下方法重置(未测试)
service mysqld stop
mysqld_safe --user=root --skip-grant-tables --skip-networking &
mysql -u root 
mysql> use mysql;
mysql> update user set password=password("new_password") where user="root"; 
mysql> flush privileges;
文章目录
  1. 1. 卸载mariadb
  2. 2. 下载rpm包
    1. 2.1. 安装源
  3. 3. 安装mysql-server
  • RPM二进制包安装
  • MySQL使用
    1. 1. 启动mysql
    2. 2. 登录mysql,修改初始密码
    3. 3. 打开3306端口
    4. 4. MySQL管理
  • |