yum安装mysql、数据库tab自动补全,快来浅了解下 ?

本文详细指导如何在CentOS7上安装MySQL/Mariadb(默认为Mariadb),包括使用yum安装、配置、修改初始密码,以及安装mycli客户端工具以提升命令行体验。同时强调了初始化设置时的安全措施,如设置root用户密码和禁用远程登录等。
摘要由CSDN通过智能技术生成

目录

一、使用yum安装mysql

  手写配置文件官方源,5.7.3版本,有初始的随机密码

Mysql数据库修改密码:

 数据库的创建与使用

二、安装mycli 插件 客户端工具

三、进入配置文件 vim /etc/my.cnf 修改


centos7上面没有mysql,它的数据库名字叫做mariadb

[root@localhost ~]#yum install mariadb-server -y
[root@localhost ~]#systemctl start  mariadb.service 
[root@localhost ~]#systemctl stop firewalld
[root@localhost ~]#setenforce 0
[root@localhost ~]#ss -natp |grep 3306    #数据库端口3306
LISTEN     0      50           *:3306                     *:*                   users:(("mysqld",pid=2478,fd=14))
[root@localhost ~]#mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 2
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)]>
[root@localhost ~]#mysql_secure_installation  #初始化设置  先输入密码 一路回车 

mysql_secure_installation

安装完mysql-server 会提示可以运行mysql_secure_installation。运行mysql_secure_installation会执行几个设置:
--为root用户设置密码
--删除匿名账号
--取消root用户远程登录
--删除test库和对test库的访问权限
--刷新授权表使修改生效

通过这几项的设置能够提高mysql库的安全。

建议生产环境中mysql安装这完成后一定要运行一次mysql_secure_installation,相关操作如下:

[root@localhost ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, 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):<–初次运行直接回车
OK, successfully used password, moving on…
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n]    #是否设置root用户密码,输入y并回车或直接回车
New password:               #设置root用户的密码
Re-enter new password:      #再输入一次你设置的密码
Password updated successfully!
Reloading privilege tables..
… Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL 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]   #是否删除匿名用户,生产环境建议删除,所以直接回车
… 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] #是否禁止root远程登录,根据自己的需求选择Y/n并回车,建议禁止
… Success!
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? [Y/n] #是否删除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] #是否重新加载权限表,直接回车
… Success!
Cleaning up…
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
[root@localhost ~]#

mariadb一般不使用

一、使用yum安装mysql

  手写配置文件官方源,5.7.3版本,有初始的随机密码

tee /etc/yum.repos.d/mysql.repo <<EOF
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/
enabled=1
gpgcheck=0
EOF


cat >/etc/yum.repos.d/mysql.repo <<EOF
[mysql57-community]
name=MySQL 5.7 Community Server
baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/
enabled=1
gpgcheck=0
EOF

[root@localhost ~]#cat >/etc/yum.repos.d/mysql.repo <<EOF
> [mysql57-community]
> name=MySQL 5.7 Community Server
> baseurl=http://repo.mysql.com/yum/mysql-5.7-community/el/7/x86_64/
> enabled=1
> gpgcheck=0
> EOF              
#多行重定向写mysql.repo配置官网mysql的yum源
[root@localhost ~]#systemctl stop firewalld
[root@localhost ~]#setenforce 0
setenforce: SELinux is disabled
[root@localhost ~]#yum -y install mysql-community-server  
#安装mysql数据库
[root@localhost ~]#systemctl start mysqld
[root@localhost ~]#ss -natp|grep 3306
LISTEN     0      80          :::3306                    :::*                   users:(("mysqld",pid=40798,fd=21))
[root@localhost ~]#
[root@localhost ~]#grep password /var/log/mysqld.log
#在/var/log/mysqld.log日志文件中过滤password关键字查看初始的登录密码,注意无前面的空格
2024-03-13T07:56:54.672419Z 1 [Note] A temporary password is generated for root@localhost: yinYQQZV=9,l
[root@localhost ~]#mysql -uroot -p'yinYQQZV=9,l'
#mysql -uroot -p'查到的随机密码'
#登录数据库,注意密码有特殊符号是使用引号


#修改密码策略
set global validate_password_policy=0;
set global validate_password_length=1;
alter user root@'localhost' identified by 'abc123'; #修改数据库密码

Mysql数据库修改密码:

set global validate_password_policy=0;

set global validate_password_length=1;   #修改密码策略
alter user root@localhost identified  by 'abc123';
#修改用户root@localhost的密码为abc123

注意数据库的mysql语句需要以 ; 结尾才能执行成功
root@localhost和root@10.0.0.1是俩个用户。
quit;   #退出数据库
mysql -uroot -p'abc123'
#使用新密码登录数据库成功,密码修改成功

 数据库的创建与使用

①登录数据库后输入 show databases;  (注意分号结尾)表示查看所有数据库,如上是默认库截图

②show  create database 库名;查看某个库信息,如下查看默认库sys信息

③create database  库名; 创建数据库XX,如下创建class库

④ use 库名;进入某个库,如下进入class库

我们看到使用数据库,不能显示当前位置,还没有补全,打错命令还需要重新打,接下来我们就来优化mysql数据库吧

二、安装mycli 插件 客户端工具

mycli 和 mysql 都是客户端工具

[root@localhost ~]#hostname zzzcentos2
[root@localhost ~]su
[root@zzzcentos2 ~]#yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
[root@zzzcentos2 ~]#mkdir /data/
[root@zzzcentos2 ~]#cd /data/
[root@zzzcentos2 data]#ls
[root@zzzcentos2 data]#rz -E
rz waiting to receive.
[root@zzzcentos2 data]#ls
Python-3.7.7_.tgz
[root@zzzcentos2 data]#tar xf Python-3.7.7_.tgz 
[root@zzzcentos2 data]#ls
Python-3.7.7  Python-3.7.7_.tgz
[root@zzzcentos2 data]#cd Python-3.7.7/
[root@zzzcentos2 Python-3.7.7]#./configure --prefix=/usr/local/Python-3.7.7/
[root@zzzcentos2 Python-3.7.7]#yum install gcc -y
[root@zzzcentos2 Python-3.7.7]#./configure --prefix=/usr/local/Python-3.7.7/
[root@zzzcentos2 Python-3.7.7]#make
[root@zzzcentos2 Python-3.7.7]#make install
[root@zzzcentos2 Python-3.7.7]#ln -s  /usr/local/Python-3.7.7/bin/python3.7  /usr/bin/python37
[root@zzzcentos2 Python-3.7.7]#ln -s /usr/local/Python-3.7.7/bin/pip3.7 /usr/bin/pip37
[root@zzzcentos2 Python-3.7.7]#pip37 install mycli -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
[root@zzzcentos2 Python-3.7.7]#ln -s /usr/local/Python-3.7.7/bin/mycli /usr/bin/mycli
[root@zzzcentos2 Python-3.7.7]#
[root@zzzcentos2 Python-3.7.7]#mycli -uroot -pabc123
Connecting to socket /var/lib/mysql/mysql.sock, owned by user mysql
MySQL 
mycli 1.27.0
Home: http://mycli.net
Bug tracker: https://github.com/dbcli/mycli/issues
Thanks to the contributor - François Pietka
MySQL root@(none):(none)> quit
Goodbye!
[root@zzzcentos2 Python-3.7.7]#

接下来见证奇迹,打命令就可以补全了

mycli   官网  点击就可进入

MyCLI is a command line interface for MySQL, MariaDB, and Percona with auto-completion and syntax highlighting.

三、进入配置文件 vim /etc/my.cnf 修改

[mysql]
prompt=(\\u@\\h) [\\d]>\\_
auto-rehash

但是,我们可以看到打命令的时候,有时候会补全,有时候并没有作用


 

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值