MySQL-介绍+安装
概述
数据
-
数据是指对客观事件进行记录并可以鉴别的符号,是对客观事物的性质、状态以及相互关系
等进行记载的物理符号或这些物理符号的组合。它是可识别的、抽象的符号。
-
在计算机系统中,数据以二进制信息单元0,1的形式表示
-
数据可以是连续的值,比如声音、图像,称为模拟数据。也可以是离散的,如符号、文字,称为数字数据。
数据库&数据库实例
- 数据库(database): 操作系统或存储上的数据文件的集合。mysql数据库中,数据库文件可以是 .frm、.MYD、.MYI、.ibd等结尾的文件, 不同存储引擎文件类型不同。
- 数据库实例(instance): 由后台进程或者线程以及一个共享内存区组成。共享内存可以被运行的后台线程所共享。 数据库实例才是真正操作数据库的。
- 通常情况下,数据库实例和数据库是一一对应的关系,也就是一个数据库实例对应一个数据库; 但是,在集群环境中存在多个数据库实例共同使用一个数据库
数据库管理系统
DBMS(database management system),可分为关系型数据库(RDBMS)和非关系型数据库(NoSQL)
关系型数据库
-
RDBMS:Relational database management system
-
数据存取是通过SQL(Structured Query Language结构化查询语言)
-
以多张二维表的方式来存储数据,又给多张表建立了一定的关系
-
常见关系型数据库:Oracle、MySQL、DB2(IBM)、Sybase、SQL server
- Oracle:传统企业
- MySQL:中、大型互联网公司,互联网领域第一
- SQLserver:windows平台,三、四线小公司,传统行业在用
- DB2:市场占有量小,像国内银行、中国移动等企业会使用
-
最大的特点:事务的一致性,维护事务的一致性需要一定的开销,数据安全性方面强(ACID)
事务介绍
- 事务由一条或者多条sql语句组成
- 在事务中的操作,这些sql语句要么都成功执行,要么都不执行,这就是一个事务
事务特点
原子性(Atomicity):
事务中的全部操作在数据库中是不可分割的,要么全部完成,要么均不执行。
一致性(Consistency):
指事务必须使数据库从一个一致性状态变换到另一个一致性状态,也就是说一个事务执行之前和执行之后都必须处于一致性状态,不能破坏关系数据的完整性以及业务逻辑上的一致性。
隔离性(Isolation):
一个事务的执行不受其他事务的干扰,事务执行的中间结果对其他事务必须是透明的。隔离性是当多个用户并发访问数据库时,比如操作同一张表时,数据库为每一个用户开启的事务,不能被其他事务的操作所干扰,多个并发事务之间要相互隔离。
持久性(Durability):
持久性是指一个事务一旦被提交了,那么对数据库中的数据的改变就是永久性的,即便是在数据库系统遇到故障的情况下也不会丢失提交事务的操作。
非关系型数据库
-
NoSQL:Not only SQL
-
通过key/value键值对来存储数据
-
不是否定关系型数据库,而是做关系型数据库的补充
关系&非关系型数据库
比较项 | 关系型数据库 | 非关系型数据库 |
---|---|---|
强大的查询功能 | √ | × |
强一致性 | √ | × |
二级索引 | √ | × |
灵活模式 | × | √ |
扩展性 | × | √ |
性能 | × | √ |
MySQL介绍
MySQL发展史
-
1979年,报表工具Unireg出现。
-
1985年,以瑞典David Axmark为首,成立了一家公司(AB前身),ISAM引擎出现。
-
1990年,提供SQL支持。
-
1999年-2000年,MySQL AB公司成立,并公布源码,开源化。
-
2000年4月BDB引擎出现,支持事务。
-
2008年1月16日 MySQL被Sun公司收购。
-
2009年4月20日Oracle收购Sun公司,MySQL转入Oracle门下
MySQL版本
-
社区版:MySQL Community Edition (GPL)
-
未经各个专有系统平台的压力测试和性能测试
-
基于GPL协议发布,可以随意下载使用
-
没有任何官方技术支持服务
-
-
企业版:MySQL Enterprise Edition(commercial)
-
提供了比较全面的高级功能、管理工具及技术支持
-
安全性、稳定性、可扩展性比较好
-
-
集群版:MySQL Cluster CGE(commercial)
- 多台MySQL服务器共同服务
版本命名方式
版本 说明 α(Alpha)版 内测版,内部交流或者专业测试人员测试用。Bug较多,普通用户最好不要安装。 β(Beta)版 公测版,专业爱好者大规模测试用,存在一些缺陷,该版本也不适合一般用户安装。 γ ( Gamma )版 相当成熟的测试版,与即将发行的正式版相差无几 Final 正式版本 Free 自由版本 Release 发行版本 Standard 标准版本 Mini 迷你精简版本,只有最基本的功能 Upgrade 升级版本 GA(GenerallyAvailable) 开发团队认为该版本是稳定版,可以在较为关键的场合使用 Retail 零售版
MySQL安装
MySQL安装方式
- yum安装或rpm安装
- glibc版本安装(相当于Windows中的绿色软件)
- 源码包编译安装
安装方式 | 优点 | 缺点 |
---|---|---|
rpm | 安装卸载简单 | 可定制性差 |
glibc | 可定制性相比rpm包灵活些 | 安装相比rpm包复杂些,需要手动初始化数据库 |
源码安装 | 可定制性最强 | 安装麻烦,需要手动初始化数据库 |
在企业中,数据库的安装很少使用rpm方式,大部分都是基于源码安装以及glibc安装!
yum安装
安装
[root@server1 ~]# yum install -y mariadb mariadb-server.x86_64
开启
[root@server2 ~]# systemctl start mariadb.service
[root@server2 ~]# systemctl status mariadb.service
● mariadb.service - MariaDB database server
Loaded: loaded (/usr/lib/systemd/system/mariadb.service; disabled; vendor preset: disabled)
Active: active (running) since 六 2021-08-21 15:37:08 CST; 13s ago
初始化
[root@server2 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none): 当前root用户密码为空,所以直接 敲回车
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.
Set root password? [Y/n] Y 设置root密码
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] Y 删除匿名用户
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] n 禁止root远程登录
... skipping.
By default, MariaDB comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] Y 删除test数据库
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] Y 刷新授权表,让初始化生效
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MariaDB
installation should now be secure.
Thanks for using MariaDB!
登录
[root@server2 ~]# mysql -uroot -p1
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 9
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> quit
Bye
glibc版本安装
官方指导文档https://dev.mysql.com/doc/refman/5.7/en/binary-installation.html
$> groupadd mysql
$> useradd -r -g mysql -s /bin/false mysql
$> cd /usr/local
$> tar zxvf /path/to/mysql-VERSION-OS.tar.gz
$> ln -s full-path-to-mysql-VERSION-OS mysql
$> cd mysql
$> mkdir mysql-files
$> chown mysql:mysql mysql-files
$> chmod 750 mysql-files
$> bin/mysqld --initialize --user=mysql
$> bin/mysql_ssl_rsa_setup
$> bin/mysqld_safe --user=mysql &
# Next command is optional
$> cp support-files/mysql.server /etc/init.d/mysql.server1234567891011121314
安装步骤
安装依赖库
[root@mysql ~]# yum install libaio -y
上传软件包
[root@mysql ~]# rz
[root@mysql ~]# ls
anaconda-ks.cfg mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
解压
[root@mysql ~]# tar -xzf mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
[root@mysql ~]# ls
anaconda-ks.cfg mysql-5.7.31-linux-glibc2.12-x86_64 mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz
创建数据库专用账号mysql及同名组
[root@mysql ~]# groupadd mysql
[root@mysql ~]# useradd -r -g mysql -s /sbin/nologin mysql
[root@mysql ~]# id mysql
uid=997(mysql) gid=1001(mysql) 组=1001(mysql)
删除/etc/my.cnf,以防初始化失败
[root@mysql ~]# rm -rf /etc/my.cnf
#/etc/my.cnf是系统为mariadb预留的配置文件,数据初始化时会在安装目录下读取my.cnf配置文件,按配置信息初始化;如果没有该文件,会去读取/etc/my.cnf文件,这样会使初始化按照mariadb的配置进行
将软件包移动到工作目录/mysql_3306下
[root@mysql ~]# mv mysql-5.7.31-linux-glibc2.12-x86_64 /mysql_3306
工作目录中创建文件夹mysql_files
[root@mysql ~]# cd /mysql_3306/
[root@mysql mysql_3306]# mkdir mysql_files
[root@mysql mysql_3306]# chown mysql:mysql mysql_files/
[root@mysql mysql_3306]# chmod 750 mysql_files/
#mysql-files是导入与导出时,指定的默认目录,glibc属于二进制软件包,其很多配置已经默认了,包括mysql-files文件夹,如果没有这个文件夹可能导致mysql无法启动
初始化数据库(会随机产生一个密码)
[root@mysql mysql_3306]# bin/mysqld --initialize --user=mysql --basedir=/mysql_3306
2021-08-21T08:01:12.540433Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2021-08-21T08:01:13.688889Z 0 [Warning] InnoDB: New log files created, LSN=45790
2021-08-21T08:01:13.835862Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
2021-08-21T08:01:13.929434Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: f4823c11-0255-11ec-94ce-000c29179db3.
2021-08-21T08:01:13.931536Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
2021-08-21T08:01:14.633931Z 0 [Warning] CA certificate ca.pem is self signed.
2021-08-21T08:01:14.839398Z 1 [Note] A temporary password is generated for root@localhost: S5GFM:eQD.wf
#初始化成功后会产生data文件夹
[root@mysql mysql_3306]# ls
bin data docs include lib LICENSE man mysql_dir1 README share support-files
设置安全加密连接
[root@mysql mysql_3306]# bin/mysql_ssl_rsa_setup --datadir=/mysql_3306/data
编写启动脚本
[root@mysql mysql_3306]# cp support-files/mysql.server /etc/init.d/mysql_3306
[root@mysql mysql_3306]# vim /etc/init.d/mysql_3306
46 basedir=/mysql_3306
47 datadir=/mysql_3306/data
#/etc/init.d是CentOS6以及早期版本中的服务脚本目录,只要把Shell启动脚本放在这个目录下,就可以使用service进行管理。若启动脚本不放在该目录下,只能通过bin/mysqld_safe脚本启动数据库
启动mysql
[root@mysql mysql_3306]# service mysql_3306 start
Starting MySQL.Logging to '/mysql_3306/data/mysql.err'.
. [ 确定 ]
如果没有启动成功,可以去查看日志/mysql_3306/data/mysql.err
设置mysql开机自启
[root@mysql mysql_3306]# chkconfig --add mysql_3306
[root@mysql mysql_3306]# chkconfig --list
#注:该输出结果只显示 SysV 服务,并不包含
#原生 systemd 服务。SysV 配置数据
#可能被原生 systemd 配置覆盖。
# 要列出 systemd 服务,请执行 'systemctl list-unit-files'。
# 查看在具体 target 启用的服务请执行
# 'systemctl list-dependencies [target]'。
mysql_3306 0:关 1:关 2:开 3:开 4:开 5:开 6:关
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
把mysql客户端命令添加到环境变量中
[root@mysql mysql_3306]# echo 'export PATH=$PATH:/mysql_3306/bin' >> /etc/profile
[root@mysql mysql_3306]# source /etc/profile
更改mysql的管理员密码
#方法1
[root@mysql mysql_3306]# mysqladmin -uroot password '123' -p
Enter password: #这里复制初始化数据库产生的密码
mysqladmin: [Warning] Using a password on the command line interface can be insecure.
Warning: Since password will be sent to server in plain text, use ssl connection to ensure password safety.
#方法2
[root@mysql mysql_3306]# mysql -uroot -p123
mysql> set password='1';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> quit
Bye
定义mysql的配置文件
[root@mysql mysql_3306]# vim /my.cnf
[client]
port=3306
socket=/tmp/mysql.sock
[mysqld]
port=3306
socket=/tmp/mysql.sock
user=mysql
basedir=/mysql_3306
datadir=/mysql_3306/data
log_error=/mysql_3306/data/mysql.err
#####
[client] #客户端基本配置
port=3306 #客户端默认连接端口
socket=/tmp/mysql.sock #用于本地连接的socket套接字
[mysqld] #服务端基本配置
port=3306 #mysql监听端口
socket=/tmp/mysql.sock #套接字文件,专门为客户端与服务器端连接提供一个桥梁
user=mysql #mysql启动用户
basedir=/mysql_3306 #安装目录
datadir=/mysql_3306/data #数据库数据文件存放目录
log_error=/mysql_3306/data/mysql.err#记录错误日志文件
[root@mysql mysql_3306]# service mysql_3306 restart
Shutting down MySQL.. [ 确定 ]
Starting MySQL. [ 确定 ]
[root@mysql mysql_3306]# ll /tmp/mysql.sock
srwxrwxrwx 1 mysql mysql 0 8月 21 17:52 /tmp/mysql.sock
安全配置
[root@mysql ~]# mysql_secure_installation
Securing the MySQL server deployment.
Enter password for user root: #输入root密码
VALIDATE PASSWORD PLUGIN can be used to test passwords
and improve security. It checks the strength of password
and allows the users to set only those passwords which are
secure enough. Would you like to setup VALIDATE PASSWORD plugin?
#安装后,会使密码变得复杂,生产环境下建议安装;学习时,为避免麻烦,可不安装
Press y|Y for Yes, any other key for No: #是否启动密码校检器
Using existing password for root.
Change the password for root ? ((Press y|Y for Yes, any other key for No) :#是否改密码
Remove anonymous users? (Press y|Y for Yes, any other key for No) : Y#是否移除匿名用户
Success.
Normally, root should only be allowed to connect from
'localhost'. This ensures that someone cannot guess at
the root password from the network.
Disallow root login remotely? (Press y|Y for Yes, any other key for No) : #是否禁止root远程登录
... skipping.
By default, MySQL comes with a database named 'test' that
anyone can access. This is also intended only for testing,
and should be removed before moving into a production
environment.
Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y#是否移除测试数据库
- Dropping test database...
Success.
- Removing privileges on test database...
Success.
Reloading the privilege tables will ensure that all changes
made so far will take effect immediately.
Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y#是否刷新
Success.
All done!
如果不小心开启了密码校检器,可在配置文件/mysql_3306/my.cnf中添加以下行,进行关闭
validate_password=OFF
源码安装
官方文档https://dev.mysql.com/doc/refman/5.7/en/source-configuration-options.html
安装要求
- 同一个软件,只要占用端口号不同,可以安装在同一台虚拟机上
安装需求 | 具体配置 |
---|---|
安装目录(basedir) | /mysql_3307 |
数据目录(datadir) | /mysql_3307/data |
端口号 | 3307 |
socket文件位置 | $basedir/mysql.sock |
字符集 | utf8mb4 |
- 常用安装配置选项
配置选项 | 描述 | 默认值 | 建议值 |
---|---|---|---|
CMAKE_INSTALL_PREFIX | 安装基目录(basedir) | /usr/local/mysql | 根据需求 |
MYSQL_DATADIR | 数据目录(datadir) | $basedir/data | 根据需求 |
SYSCONFDIR | 默认配置文件my.cnf路径 | /etc | |
MYSQL_TCP_PORT | TCP/IP端口 | 3306 | 非默认端口 |
MYSQL_UNIX_ADDR | 套接字socket文件路径 | /tmp/mysql.sock | $basedir/ |
DEFAULT_CHARSET | 默认字符集 | latin1 | utf8mb4 |
DEFAULT_COLLATION | 默认校验规则 | latin1_swedish_ci | utf8mb4_general_ci |
WITH_EXTRA_CHARSETS | 扩展字符集 | all | all |
ENABLED_LOCAL_INFILE | 是否启用本地加载外部数据文件功能 | OFF | 开启 |
WITH_SSL | SSL支持类型 | system | 显式指定 |
WITH_BOOST | Boost库源代码的位置 | Boost库是构建MySQL所必需的,建议提前下载 |
- 存储引擎相关配置项
以下选项值均为布尔值,0或1;0代表不编译到服务器中,1代表编译
建议均静态编译到服务器中
配置选项 | 描述 |
---|---|
WITH_INNOBASE_STORAGE_ENGINE | 将InnoDB存储引擎插件构建为静态模块编译到服务器中;建议编译到服务器中 |
WITH_PARTITION_STORAGE_ENGINE | 是否支持分区 |
WITH_FEDERATED_STORAGE_ENGINE | 本地数据库是否可以访问远程mysql数据 |
WITH_BLACKHOLE_STORAGE_ENGINE | 黑洞存储引擎,接收数据,但不存储,直接丢弃 |
WITH_MYISAM_STORAGE_ENGINE | 将MYISAM存储引擎静态编译到服务器中 |
安装步骤
安装依赖库
[root@mysql ~]# yum -y install ncurses-devel cmake libaio-devel openssl-devel
#最小化安装,还需要下载gcc,gcc-c++,不然,可能会编译报错
传入软件并解压
[root@mysql ~]# rz
[root@mysql ~]# ls
anaconda-ks.cfg mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz mysql-boost-5.7.31.tar.gz
[root@mysql ~]# tar -xzf mysql-boost-5.7.31.tar.gz
[root@mysql ~]# ls
anaconda-ks.cfg mysql-5.7.31 mysql-5.7.31-linux-glibc2.12-x86_64.tar.gz mysql-boost-5.7.31.tar.gz
[root@mysql ~]# cd mysql-5.7.31/
编写配置文件
[root@mysql mysql-5.7.31]# vim myconfig.sh
cmake . \
-DCMAKE_INSTALL_PREFIX=/mysql_3307 \
-DMYSQL_DATADIR=/mysql_3307/data \
-DMYSQL_TCP_PORT=3307 \
-DMYSQL_UNIX_ADDR=/mysql_3307/mysql.sock \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_PARTITION_STORAGE_ENGINE=1 \
-DWITH_FEDERATED_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_MYISAM_STORAGE_ENGINE=1 \
-DENABLED_LOCAL_INFILE=1 \
-DEXTRA_CHARSETS=all \
-DDEFAULT_CHARSET=utf8mb4 \
-DDEFAULT_COLLATION=utf8mb4_general_ci \
-DWITH_SSL=system \
-DWITH_BOOST=boost
[root@mysql mysql-5.7.31]# chmod +x myconfig.sh
[root@mysql mysql-5.7.31]# ./myconfig.sh
[root@mysql mysql-5.7.31]# ./myconfig.sh
-- Running cmake version 2.8.12.2
-- Could NOT find Git (missing: GIT_EXECUTABLE)
-- Configuring with MAX_INDEXES = 64U
-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error: your C compiler: "CMAKE_C_COMPILER-NOTFOUND" was not found. Please set CMAKE_C_COMPILER to a valid compiler path or name.
CMake Error: your CXX compiler: "CMAKE_CXX_COMPILER-NOTFOUND" was not found. Please set CMAKE_CXX_COMPILER to a valid compiler path or name.
-- CMAKE_GENERATOR: Unix Makefiles
CMake Error at cmake/os/Linux.cmake:50 (MESSAGE):
Unsupported compiler!
Call Stack (most recent call first):
CMakeLists.txt:198 (INCLUDE)
-- Configuring incomplete, errors occurred!
See also "/root/mysql-5.7.31/CMakeFiles/CMakeOutput.log".
See also "/root/mysql-5.7.31/CMakeFiles/CMakeError.log".
报错:CMake Error: your C compiler: “CMAKE_C_COMPILER-NOTFOUND” was not found.
原因:缺少依赖软件gcc,gcc-c++
解决:yum install -y gcc gcc-c++
[root@mysql mysql-5.7.31]# yum install -y gcc gcc-c++
[root@mysql mysql-5.7.31]# ./myconfig.sh
编译安装
[root@mysql mysql-5.7.31]# make -j2 && make install
选项说明:
-j2 :代表同时开启多个线程共同实现编译操作
扩展:
编译安装中途出错,重新编译需要删除CMakeCache.txt文件
rm -rf CMakeCache.txt
make -j2 && make install
初始化
进入到/mysql_3307,创建mysql-files目录
[root@mysql mysql-5.7.31]# cd
[root@mysql ~]# cd /mysql_3307
[root@mysql mysql_3307]# mkdir mysql-files
[root@mysql mysql_3307]# chown -R mysql:mysql /mysql_3307
[root@mysql mysql_3307]# chmod 750 mysql-files/
初始化操作
[root@mysql mysql_3307]# bin/mysqld --initialize --user=mysql --basedir=/mysql_3307 --datadir=/mysql_3307/data
#获得随机密码
2021-08-21T14:14:21.554205Z 1 [Note] A temporary password is generated for root@localhost: tR)5d?aJTq)h
启动数据库
[root@mysql mysql_3307]# cp support-files/mysql.server /etc/init.d/mysql_3307
[root@mysql mysql_3307]# service mysql_3307 start
Starting MySQL.Logging to '/mysql_3307/data/mysql.err'.
[ 确定 ]
[root@mysql mysql_3307]# ss -tnalp |grep mysqld
LISTEN 0 80 [::]:3306 [::]:* users:(("mysqld",pid=12171,fd=28))
LISTEN 0 80 [::]:3307 [::]:* users:(("mysqld",pid=33463,fd=20))
编写MySQL配置文件,my.cnf
[root@mysql mysql_3307]# vim my.cnf
[client]
port=3307
socket=/mysql_3307/mysql.sock
[mysqld]
port=3307
socket=/mysql_3307/mysql.sock
user=mysql
basedir=/mysql_3307
datadir=/mysql_3307/data
log_error=/mysql_3307/data/mysql.err
[root@mysql mysql_3307]# service mysql_3307 restart
Shutting down MySQL.. [ 确定 ]
Starting MySQL. [ 确定 ]
设置管理员的密码
[root@mysql mysql_3307]# bin/mysqladmin -uroot password '1' -p
Enter password:
mysqladmin: connect to server at 'localhost' failed
error: 'Access denied for user 'root'@'localhost' (using password: YES)'
管理员密码忘记
关闭数据库 [root@mysql mysql_3307]# service mysql_3307 stop Shutting down MySQL.. [ 确定 ] [root@mysql mysql_3307]# ss -tnalp |grep mysqld LISTEN 0 80 [::]:3306 [::]:* users:(("mysqld",pid=12171,fd=28)) 跳过授权登录 [root@mysql mysql_3307]# /mysql_3307/bin/mysqld --defaults-file=/mysql_3307/my.cnf --skip-grant-tables --skip-networking=on --user=mysql & [1] 33884 登录mysql并修改root密码 [root@mysql mysql_3307]# /mysql_3307/bin/mysql -uroot -p Enter password: #回车即可 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.31 Source distribution Copyright (c) 2000, 2020, 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> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> alter user 'root'@'localhost' identified by '1'; Query OK, 0 rows affected (0.00 sec) mysql> quit Bye 重启数据库 [root@mysql mysql_3307]# jobs [1]+ 运行中 /mysql_3307/bin/mysqld --defaults-file=/mysql_3307/my.cnf --skip-grant-tables --skip-networking=on --user=mysql & [root@mysql mysql_3307]# kill %1 [root@mysql mysql_3307]# service mysql_3307 start Starting MySQL. [ 确定 ] [1]+ 完成 /mysql_3307/bin/mysqld --defaults-file=/mysql_3307/my.cnf --skip-grant-tables --skip-networking=on --user=mysql 测试 [root@mysql mysql_3307]# /mysql_3307/bin/mysql -uroot -p1 mysql> quit Bye
安全设置
[root@mysql mysql_3307]# bin/mysql_secure_installation
设置mysql_3307开机自启
[root@mysql mysql_3307]# chkconfig --add mysql_3307
[root@mysql mysql_3307]# chkconfig --list
注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。
要列出 systemd 服务,请执行 'systemctl list-unit-files'。
查看在具体 target 启用的服务请执行
'systemctl list-dependencies [target]'。
mysql_3306 0:关 1:关 2:开 3:开 4:开 5:开 6:关
mysql_3307 0:关 1:关 2:开 3:开 4:开 5:开 6:关
netconsole 0:关 1:关 2:关 3:关 4:关 5:关 6:关
network 0:关 1:关 2:开 3:开 4:开 5:开 6:关
在同一虚拟机上访问不同的数据库
访问端口号为3306的数据库
[root@mysql ~]# /mysql_3306/bin/mysql -uroot -p1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 5.7.31 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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> quit
Bye
访问端口号为3307的数据库
[root@mysql ~]# /mysql_3307/bin/mysql -uroot -p1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.31 Source distribution
Copyright (c) 2000, 2020, 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> quit
Bye
- 定义别名
[root@mysql ~]# vim /etc/bashrc
93 alias mysql_3306="/mysql_3306/bin/mysql"
94 alias mysql_3307="/mysql_3307/bin/mysql"
[root@mysql ~]# source /etc/bashrc
[root@mysql ~]# mysql_3306 -uroot -p1
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.7.31 MySQL Community Server (GPL)
Copyright (c) 2000, 2020, 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> quit
Bye