Centos系统上安装并配置mysql的教程?

Centos下以rpm方式安装MySQL步骤如下:

一、mysql-5.7.12 rpm bundel包下载

本人喜欢把下载的文件放在/root/software路径下,便于统一管理

[root@localhost software]#cd /root/software
[root@localhost software]# wget http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar
--2018-06-03 18:34:23--  http://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar
正在解析主机 dev.mysql.com (dev.mysql.com)... 137.254.60.11
正在连接 dev.mysql.com (dev.mysql.com)|137.254.60.11|:80... 已连接。
已发出 HTTP 请求,正在等待回应... 301 Moved Permanently
位置:https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar [跟随至新的 URL]
--2018-06-03 18:34:24--  https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar
正在连接 dev.mysql.com (dev.mysql.com)|137.254.60.11|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Found
位置:https://cdn.mysql.com//archives/mysql-5.7/mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar [跟随至新的 URL]
--2018-06-03 18:34:29--  https://cdn.mysql.com//archives/mysql-5.7/mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar
正在解析主机 cdn.mysql.com (cdn.mysql.com)... 23.41.87.110
正在连接 cdn.mysql.com (cdn.mysql.com)|23.41.87.110|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:457492480 (436M) [application/x-tar]
正在保存至: “mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar”


100%[======================================>] 457,492,480 5.03MB/s 用时 83s    


2018-06-03 18:35:54 (5.26 MB/s) - 已保存 “mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar” [457492480/457492480])


二、解压缩mysql-bundle包

[root@localhost software]# tar -xvf mysql-5.7.12-1.el6.x86_64.rpm-bundle.tar 
mysql-community-libs-5.7.12-1.el6.x86_64.rpm
mysql-community-test-5.7.12-1.el6.x86_64.rpm
mysql-community-common-5.7.12-1.el6.x86_64.rpm
mysql-community-devel-5.7.12-1.el6.x86_64.rpm
mysql-community-libs-compat-5.7.12-1.el6.x86_64.rpm
mysql-community-embedded-5.7.12-1.el6.x86_64.rpm
mysql-community-server-5.7.12-1.el6.x86_64.rpm
mysql-community-embedded-devel-5.7.12-1.el6.x86_64.rpm
mysql-community-client-5.7.12-1.el6.x86_64.rpm

三、mysql的rpm安装

步骤:安装mysql-community-server之前,必须安装mysql-community-client和mysql-community-common的rpm包。而安装community-client和community-common包之前,必须删除mysql-lib(因为系统自带的版本过低)


3.1、删除mysql-lib(系统自带的版本过低)

[root@localhost software]# yum remove mysql-libs

3.2、依次安装如下软件包(common、libs、client、server)

[root@localhost software]# rpm -ivh mysql-community-common-5.7.12-1.el6.x86_64.rpm
警告:mysql-community-common-5.7.12-1.el6.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-common-5.7.12-1.e################################# [100%]
[root@localhost software]# rpm -ivh mysql-community-libs-5.7.12-1.el6.x86_64.rpm
警告:mysql-community-libs-5.7.12-1.el6.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-libs-5.7.12-1.el6################################# [100%]
[root@localhost software]# rpm -ivh mysql-community-client-5.7.12-1.el6.x86_64.rpm 
警告:mysql-community-client-5.7.12-1.el6.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-client-5.7.12-1.e################################# [100%]
[root@localhost software]#  rpm -ivh mysql-community-server-5.7.12-1.el6.x86_64.rpm 
警告:mysql-community-server-5.7.12-1.el6.x86_64.rpm: 头V3 DSA/SHA1 Signature, 密钥 ID 5072e1f5: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:mysql-community-server-5.7.12-1.e################################# [100%]

四、启动mysql 服务

[root@localhost software]# service mysqld start
Starting mysqld (via systemctl):                           [  确定  ]

查看端口:(注意第十一行)

[root@localhost software]# netstat -tlunp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      1397/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1013/sshd           
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN      1014/cupsd          
tcp6       0      0 :::111                  :::*                    LISTEN      1/systemd           
tcp6       0      0 :::22                   :::*                    LISTEN      1013/sshd           
tcp6       0      0 ::1:631                 :::*                    LISTEN      1014/cupsd          
tcp6       0      0 :::3306                 :::*                    LISTEN      22771/mysqld        
udp        0      0 0.0.0.0:16290           0.0.0.0:*                           20879/dhclient      
udp        0      0 192.168.122.1:53        0.0.0.0:*                           1397/dnsmasq        
udp        0      0 0.0.0.0:67              0.0.0.0:*                           1397/dnsmasq        
udp        0      0 0.0.0.0:68              0.0.0.0:*                           20879/dhclient      
udp        0      0 0.0.0.0:42198           0.0.0.0:*                           587/avahi-daemon: r 
udp        0      0 0.0.0.0:5353            0.0.0.0:*                           587/avahi-daemon: r 
udp        0      0 127.0.0.1:323           0.0.0.0:*                           607/chronyd         
udp6       0      0 :::13829                :::*                                20879/dhclient      
udp6       0      0 ::1:323                 :::*                                607/chronyd   

五、初始密码修改及登录 

5.1、rpm安装mysql后,会自动初始化一个密码,即下面第十四行localhost:后面的那一串字符。复制此密码,一会登陆数据库用。

[root@localhost software]# cat /var/log/mysqld.log | more
2018-06-03T10:43:37.166285Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is
 deprecated. Please use --explicit_defaults_for_timestamp server option (see doc
umentation for more details).
2018-06-03T10:43:38.297369Z 0 [Warning] InnoDB: New log files created, LSN=45790
2018-06-03T10:43:38.661954Z 0 [Warning] InnoDB: Creating foreign key constraint 
system tables.
2018-06-03T10:43:38.729812Z 0 [Warning] No existing UUID has been found, so we a
ssume that this is the first time that this server has been started. Generating 
a new UUID: f97c84d0-671a-11e8-b38c-000c2990d7e4.
2018-06-03T10:43:38.731522Z 0 [Warning] Gtid table is not ready to be used. Tabl
e 'mysql.gtid_executed' cannot be opened.
2018-06-03T10:43:38.732410Z 1 [Note] A temporary password is generated for root@
localhost: 3u>8i.tl1*gJ
2018-06-03T10:43:41.262817Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is
 deprecated. Please use --explicit_defaults_for_timestamp server option (see doc
umentation for more details).
2018-06-03T10:43:41.264158Z 0 [Note] /usr/sbin/mysqld (mysqld 5.7.12) starting a
s process 22516 ...
2018-06-03T10:43:41.293167Z 0 [Note] InnoDB: PUNCH HOLE support available
2018-06-03T10:43:41.293370Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic
 builtins
2018-06-03T10:43:41.293390Z 0 [Note] InnoDB: Uses event mutexes
2018-06-03T10:43:41.293399Z 0 [Note] InnoDB: GCC builtin __sync_synchronize() is
 used for memory barrier
2018-06-03T10:43:41.293406Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.3
2018-06-03T10:43:41.293417Z 0 [Note] InnoDB: Using Linux native AIO
2018-06-03T10:43:41.293841Z 0 [Note] InnoDB: Number of pools: 1
2018-06-03T10:43:41.294338Z 0 [Note] InnoDB: Using CPU crc32 instructions
2018-06-03T10:43:41.310619Z 0 [Note] InnoDB: Initializing buffer pool, total siz
e = 128M, instances = 1, chunk size = 128M
2018-06-03T10:43:41.327291Z 0 [Note] InnoDB: Completed initialization of buffer 
pool......
2018-06-03T10:43:45.937339Z 0 [Note] /usr/sbin/mysqld: ready for connections.
Version: '5.7.12'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MySQL Commun
ity Server (GPL)
2018-06-03T10:43:45.959314Z 2 [Note] Access denied for user 'UNKNOWN_MYSQL_USER'
@'localhost' (using password: NO)
2018-06-03T10:46:55.741014Z 3 [Note] Access denied for user 'root'@'localhost' (
using password: YES)
[root@localhost software]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.7.12

Copyright (c) 2000, 2016, 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.


5.2、修改 mysql root密码

由于最新的mysql版本对密码策略有要求,所以必须增加复杂程度才能通过。本人测试了一个不符合策略的例子;

注意,用刚才5.1中复制的随机密码登陆mysql。

[root@localhost software]# mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.12 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, 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> set password='sh123456';
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
mysql> set password='[email protected]';
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye

附加:在Centos系统上导入sql文件到Mysql数据库

1、登录mysql

[root@iZq7t5ykp3n691Z admin]# mysql -u root -p

2、打开要导入的数据库:

mysql> use hkj_database;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

3、导入sql文件(source  +  sql文件的绝对路径)

mysql> source /usr/local/tomcat/hkj_database.sql;
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 1 row affected (0.00 sec)

Database changed
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.02 sec)

Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)
注:Mysql的安装方式有多种,采用这种rpm安装的方式,不需要另外再进行其他设置,即可实现开机启动。

猜你喜欢

转载自blog.csdn.net/qq_37896194/article/details/80560107