centos中mysql 安装以及配置,建库

1.检测系统内部有没有安装其他的mysql数据库

rpm -qa | grep mysql

然后如果有的话删除这些mysql yum remove 查出来的所有名字

2.彻底删除系统中mysql的目录

find / -name mysql

将查出的所有目录删掉 rm -rf 查到的路径

3.下载mysql的rpm包

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

4.安装mysql源

yum localinstall mysql57-community-release-el7-8.noarch.rpm
  • 1

5.重要一步

先尝试安装

yum install -y mysql-community-server
  • 1

如果可以安装那么安装之后查询mysql版本 mysql -V

显示5.7版本则安装成功

如果安装成立5.6

那么修改mqsql源里面的一些 vi /etc/yum.repos.d/mysql-community.repo :比如要安装5.6版本,将5.7源的enabled=1改成enabled=0。然后再将5.6源的enabled=0改成enabled=1即可。

这里是将5.7的修改为1 5.6的修改为0

# Enable to use MySQL 5.5
[mysql55-community]
name=MySQL 5.5 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.5-community/el/7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

# 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/7/$basearch/
enabled=0 #####################################
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/$basearch/
enabled=1 #######################################
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-mysql

[mysql-tools-preview]
name=MySQL Tools Preview
baseurl=http://repo.mysql.com/yum/mysql-tools-preview/el/7/$basearch/
enabled=0
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

再次安装 yum install mysql-community-server

6.启动mysql服务

systemctl start mysqld
  • 1

7.mysql开启启动

systemctl enable mysqld
  • 1

8.查看mysql密码

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

9.如果你什么都没有查到

那么默认密码是没有的

直接进入mysql

mysql -uroot -p
  • 1

10.修改密码

set password = passowowd('你的密码');
  • 1

如果你在这一步发生错误,那么请退出mysql后使用命令先更新一下数据库

mysql_upgrade -uroot -p
  • 1

然后再次进入mysql设置root密码即可

如果还是不行的话,就使用下面方法

查看root密码

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

查看后打开mysql并修改密码

mysql -uroot -p

首次修改密码会提示密码安全等级的错误,因此要先修改安全等级,再次修改才会生效

1、设置安全级别
set global validate_password_policy=0;
2、默认密码为8,可以设置为其他值,最小4位
set global validate_password_length=4;
3、设置root密码
set password for root@localhost = password('新密码');
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

11.开启mysql的root账户远程访问

  1. 进入mysql
mysql -uroot -p  输入密码登录
  • 1
  1. 选择mysql数据库
use mysql;
  • 1
  1. 修改root远程访问
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
  • 1

4.更新数据库

FLUSH PRIVILEGES; 
  • 1

注意这个时候防火墙要关闭,或者开放3306端口

二、导入数据库

1、首先建空数据库

mysql>create database abc;

2、导入数据库

方法一:

(1)选择数据库

mysql>use abc;

(2)设置数据库编码

mysql>set names utf8;

(3)导入数据(注意sql文件的路径)

mysql>source /home/abc/abc.sql;

方法二:

mysql -u用户名 -p密码 数据库名 < 数据库名.sql

mysql -uabc_f -p abc < abc.sql

如果安装成功报错 

ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this st

修改my.cnf配置 sed -i ‘/mysqld/a\skip-grant-tables’ /etc/my.cnf 

centos中mysql root用户密码修改方法
  1. 重启mysql

    systemctl restart mysqld

    centos中mysql root用户密码修改方法
  2. 进入mysql

    这次就不需要root密码了

    centos中mysql root用户密码修改方法
  3.  use mysql;

    centos中mysql root用户密码修改方法
  4. UPDATE user SET Password = password('newpassword') where User = 'roo

    centos中mysql root用户密码修改方法
  5. 刷新权限

    flush privileges; 

    centos中mysql root用户密码修改方法
  6. 退出

    quit

    centos中mysql root用户密码修改方法
  7. 还原my.cnf配置sed -i 's/skip-grant-tables/#skip-grant-tables/g' /etc/my.cnf

    centos中mysql root用户密码修改方法
  8. 9

    再次重启mysql服务

    systemctl restart mysqld

    centos中mysql root用户密码修改方法
1
mysql> update user  set  password=password(“新密码”) where user=”用户名”;

执行后报错  ERROR 1054(42S22) Unknown column 'password' in ‘field list’

错误的原因是 5.7版本下的mysql数据库下已经没有password这个字段了,password字段改成了authentication_string

所以请使用一下命令:

复制代码
>mysql -u root -p
Enter password: ********
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 12
Server version: 5.7.18-log MySQL Community Server (GPL)

Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use mysql;
Database changed
mysql> select User from user;  #此处为查询用户命令
+-----------+
| User      |
+-----------+
| *******  |
| mysql.sys |
| root      |
+-----------+
3 rows in set (0.00 sec)

mysql> update user set password=password("*******") where user="*******";  #修改密码报错
ERROR 1054 (42S22): Unknown column 'password' in 'field list'
mysql> update mysql.user set authentication_string=password('*******') where user='*******';  #修改密码成功
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

mysql> flush privileges;  #立即生效
Query OK, 0 rows affected (0.00 sec)

mysql> quit
Bye

n>mysql -u ******* -p #以该用户登录成功.
Enter password: ********
…………………………
mysql>

猜你喜欢

转载自www.cnblogs.com/123hll/p/9633422.html
今日推荐