一、安装

本想参照《跟老男孩学Linux运维-Mysql入门与提高实践》安装一下mysql,操练一下主从集群备份,奈何书中是编译源码方式安装,安装各种报错,时间成本太高,故改用rpm方式安装。由于书中采用的mysql版本是5.6,所以我也安装5.6和书中保持一致。主要目的是操练主从集群备份,所以软件版本和书中保持一致,减少出错率。

1.Linux环境

  1. [root@mysql01 ~]# cat /etc/redhat-release <---操作系统版本
  2. CentOS Linux release 7.6.1810 (Core)
  3. [root@mysql01 ~]# uname -m <---64位系统
  4. x86_64

2.下载rmp包

163镜像网站:http://mirrors.163.com/mysql/Downloads/MySQL-5.6/
centos7就下载el7相关的包,centos6就下载el6相关的包,这里下载el7 。
四个安装包:
image.png

3.上传到虚拟机

家~目录:
[root@lhy ~]# ll
total 95260
-rw———-. 1 root root 1480 Feb 21 12:29 anaconda-ks.cfg
-rw-r—r—. 1 root root 21592940 Jun 26 18:23 MySQL-client-5.6.49-1.el7.x86_64.rpm
-rw-r—r—. 1 root root 3538400 Jun 26 18:23 MySQL-devel-5.6.49-1.el7.x86_64.rpm
-rw-r—r—. 1 root root 70068868 Jun 26 18:23 MySQL-server-5.6.49-1.el7.x86_64.rpm
-rw-r—r—. 1 root root 2337640 Jun 26 18:23 MySQL-shared-5.6.49-1.el7.x86_64.rpm

4.卸载mariadb 或者已有mysql

将mariaDB包和自带mysql包卸载干净,否则安装时出现错误,导致失败。
查询安装的MariaDB 或者mysql
[root@lhy ~]# rpm -qa|grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64
卸载系统已安装的mysql或者MariaDB 依赖包:
[root@lhy ~]# rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64 —nodeps

find / -iname mariadb或者whereis mariadb 查找是否有相关配置目录及文件,如有,则 rm -rf 删除

5.安装mysql 5.6

rpm -ivh MySQL-client-5.6.49-1.el7.x86_64.rpm
rpm -ivh MySQL-devel-5.6.49-1.el7.x86_64.rpm
rpm -ivh MySQL-shared-5.6.49-1.el7.x86_64.rpm
rpm -ivh MySQL-server-5.6.49-1.el7.x86_64.rpm

执行最后一条,我报错了:
warning: MySQL-server-5.6.49-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
error: Failed dependencies:
net-tools is needed by MySQL-server-5.6.49-1.el7.x86_64
缺少net-tools,(由于coentos是最小安装,缺少很多工具包)
安装一个便是:yum -y install net-tools
再执行一次最后一条命令,打印比较多:

[root@lhy ~]# rpm -ivh MySQL-server-5.6.49-1.el7.x86_64.rpm 
warning: MySQL-server-5.6.49-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:MySQL-server-5.6.49-1.el7        ################################# [100%]
warning: user mysql does not exist - using root
warning: group mysql does not exist - using root
warning: user mysql does not exist - using root
warning: group mysql does not exist - using root
2021-06-26 18:45:26 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-06-26 18:45:26 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2021-06-26 18:45:26 0 [Note] /usr/sbin/mysqld (mysqld 5.6.49) starting as process 9380 ...
2021-06-26 18:45:26 9380 [Note] InnoDB: Using atomics to ref count buffer pool pages
2021-06-26 18:45:26 9380 [Note] InnoDB: The InnoDB memory heap is disabled
2021-06-26 18:45:26 9380 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2021-06-26 18:45:26 9380 [Note] InnoDB: Memory barrier is not used
2021-06-26 18:45:26 9380 [Note] InnoDB: Compressed tables use zlib 1.2.11
2021-06-26 18:45:26 9380 [Note] InnoDB: Using Linux native AIO
2021-06-26 18:45:26 9380 [Note] InnoDB: Using CPU crc32 instructions
2021-06-26 18:45:26 9380 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2021-06-26 18:45:26 9380 [Note] InnoDB: Completed initialization of buffer pool
2021-06-26 18:45:26 9380 [Note] InnoDB: The first specified data file ./ibdata1 did not exist: a new database to be created!
2021-06-26 18:45:26 9380 [Note] InnoDB: Setting file ./ibdata1 size to 12 MB
2021-06-26 18:45:26 9380 [Note] InnoDB: Database physically writes the file full: wait...
2021-06-26 18:45:26 9380 [Note] InnoDB: Setting log file ./ib_logfile101 size to 48 MB
2021-06-26 18:45:27 9380 [Note] InnoDB: Setting log file ./ib_logfile1 size to 48 MB
2021-06-26 18:45:27 9380 [Note] InnoDB: Renaming log file ./ib_logfile101 to ./ib_logfile0
2021-06-26 18:45:27 9380 [Warning] InnoDB: New log files created, LSN=45781
2021-06-26 18:45:27 9380 [Note] InnoDB: Doublewrite buffer not found: creating new
2021-06-26 18:45:27 9380 [Note] InnoDB: Doublewrite buffer created
2021-06-26 18:45:27 9380 [Note] InnoDB: 128 rollback segment(s) are active.
2021-06-26 18:45:27 9380 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-06-26 18:45:27 9380 [Note] InnoDB: Foreign key constraint system tables created
2021-06-26 18:45:27 9380 [Note] InnoDB: Creating tablespace and datafile system tables.
2021-06-26 18:45:27 9380 [Note] InnoDB: Tablespace and datafile system tables created.
2021-06-26 18:45:27 9380 [Note] InnoDB: Waiting for purge to start
2021-06-26 18:45:27 9380 [Note] InnoDB: 5.6.49 started; log sequence number 0
2021-06-26 18:45:27 9380 [Note] RSA private key file not found: /var/lib/mysql//private_key.pem. Some authentication plugins will not work.
2021-06-26 18:45:27 9380 [Note] RSA public key file not found: /var/lib/mysql//public_key.pem. Some authentication plugins will not work.
A random root password has been set. You will find it in '/root/.mysql_secret'.
2021-06-26 18:45:27 9380 [Note] Binlog end
2021-06-26 18:45:27 9380 [Note] InnoDB: FTS optimize thread exiting.
2021-06-26 18:45:27 9380 [Note] InnoDB: Starting shutdown...
2021-06-26 18:45:29 9380 [Note] InnoDB: Shutdown completed; log sequence number 1625977


2021-06-26 18:45:29 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-06-26 18:45:29 0 [Note] Ignoring --secure-file-priv value as server is running with --bootstrap.
2021-06-26 18:45:29 0 [Note] /usr/sbin/mysqld (mysqld 5.6.49) starting as process 9403 ...
2021-06-26 18:45:29 9403 [Note] InnoDB: Using atomics to ref count buffer pool pages
2021-06-26 18:45:29 9403 [Note] InnoDB: The InnoDB memory heap is disabled
2021-06-26 18:45:29 9403 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
2021-06-26 18:45:29 9403 [Note] InnoDB: Memory barrier is not used
2021-06-26 18:45:29 9403 [Note] InnoDB: Compressed tables use zlib 1.2.11
2021-06-26 18:45:29 9403 [Note] InnoDB: Using Linux native AIO
2021-06-26 18:45:29 9403 [Note] InnoDB: Using CPU crc32 instructions
2021-06-26 18:45:29 9403 [Note] InnoDB: Initializing buffer pool, size = 128.0M
2021-06-26 18:45:29 9403 [Note] InnoDB: Completed initialization of buffer pool
2021-06-26 18:45:29 9403 [Note] InnoDB: Highest supported file format is Barracuda.
2021-06-26 18:45:29 9403 [Note] InnoDB: 128 rollback segment(s) are active.
2021-06-26 18:45:29 9403 [Note] InnoDB: Waiting for purge to start
2021-06-26 18:45:29 9403 [Note] InnoDB: 5.6.49 started; log sequence number 1625977
2021-06-26 18:45:29 9403 [Note] RSA private key file not found: /var/lib/mysql//private_key.pem. Some authentication plugins will not work.
2021-06-26 18:45:29 9403 [Note] RSA public key file not found: /var/lib/mysql//public_key.pem. Some authentication plugins will not work.
2021-06-26 18:45:29 9403 [Note] Binlog end
2021-06-26 18:45:29 9403 [Note] InnoDB: FTS optimize thread exiting.
2021-06-26 18:45:29 9403 [Note] InnoDB: Starting shutdown...
2021-06-26 18:45:30 9403 [Note] InnoDB: Shutdown completed; log sequence number 1625987




A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in '/root/.mysql_secret'.

You must change that password on your first connect,
no other statement but 'SET PASSWORD' will be accepted.
See the manual for the semantics of the 'password expired' flag.

Also, the account for the anonymous user has been removed.

In addition, you can run:

  /usr/bin/mysql_secure_installation

which will also give you the option of removing the test database.
This is strongly recommended for production servers.

See the manual for more instructions.

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

  http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

New default config file was created as /usr/my.cnf and
will be used by default by the server when you start it.
You may edit this file to change server settings

安装server后会自动初始化,其中就包含了数据库初始化和生成随机root密码
root用户的默认密码在这里:
A RANDOM PASSWORD HAS BEEN SET FOR THE MySQL root USER !
You will find that password in ‘/root/.mysql_secret’.
看看:
[root@lhy ~]# cat /root/.mysql_secret
# The random password set for the root user at Sat Jun 26 18:45:27 2021 (local time): vu9e9YeyeGvfFMgy

手动新增配置文件

cp /usr/share/mysql/my-default.cnf /etc/my.cnf    #如果安装5.7版本,会自动生成该配置文
_
至此,完成安装和初始化。

6.启动mysql 5.6

systemctl start mysql
systemctl stop mysql 关闭
查看状态:systemctl status mysql
image.png
如果要加入开机启动,执行如下:
~]# systemctl enable mysql    
  或
~]# chkconfig mysql on

7. 登录mysql并修改root密码

用登录mysql,如下:
mysql -uroot -p 或者
输入上面看到的默认密码
image.png
修改密码:
set password=password(‘123456’);
完成安装:
mysql> show databases
-> ;
+——————————+
| Database |
+——————————+
| information_schema |
| mysql |
| performance_schema |
| test |
+——————————+