Home Database Mysql Tutorial CentOS下重置MySQL的root密码的教程_MySQL

CentOS下重置MySQL的root密码的教程_MySQL

May 27, 2016 pm 01:45 PM

本人在CentOS6.4上安装万mysql后,无法通过root进入,因为安装的时候,并没有设置root密码,似乎有个初始随机密码,但是不记得了,太麻烦,直接重置root密码。
首先,你必须要有操作系统的root权限了。要是连系统的root权限都没有的话,先考虑root系统再走下面的步骤。

PS:赋予用于root权限
方法一: 修改 /etc/sudoers 文件,找到%wheel一行,把前面的注释(#)去掉


## Allows people in group wheel to run all commands
%wheel ALL=(ALL) ALL
Copy after login


然后修改用户,使其属于root组(wheel),命令如下:


#usermod -g root tommy
Copy after login


修改完毕,现在可以用tommy帐号登录,然后用命令 sudo su - ,即可获得root权限进行操作。

方法二: 修改 /etc/sudoers 文件,找到root一行,在root下面添加一行,如下所示:


## Allow root to run any commands anywhere
root ALL=(ALL)  ALL
tommy ALL=(ALL)  ALL
Copy after login


修改完毕,现在可以用tommy帐号登录,然后用命令 sudo su - ,即可获得root权限进行操作。

方法三: 修改 /etc/passwd 文件,找到如下行,把用户ID修改为 0 ,如下所示:


tommy:x:500:500:tommy:/home/tommy:/bin/bash
Copy after login


修改后如下


tommy:x:0:500:tommy:/home/tommy:/bin/bash
Copy after login


保存,用tommy账户登录后,直接获取的就是root帐号的权限。


root下类似于安全模式登录系统,有人建议说是pkill mysql,但是我不建议哈。因为当你执行了这个命令后,会导致这样的状况:


/etc/init.d/mysqld status
mysqld dead but subsys locked
Copy after login


这样即使你是在安全模式下启动mysql都未必会有用的,所以一般是这样/etc/init.d/mysqld stop,如果你不幸先用了pkill,那么就start一下再stop咯。
使用rpm包安装完mysql后,按照一下步骤重置root密码:
启动mysql:


#/etc/init.d/mysql start
Copy after login


启动成功后查看mysql进程信息,获取mysqld_safe的安装目录(非常关键):


#ps -ef | grep -i mysql
root  3466  1 0 01:45 pts/1 00:00:00 /bin/sh /usr/bin/mysqld_safe 
--datadir=/var/lib/mysql --pid-file=/var/lib/mysql/BSC.TEST.pid
mysql  3569 3466 16 01:45 pts/1 00:00:00 /usr/sbin/mysqld 
--basedir=/usr 
--datadir=/var/lib/mysql 
--plugin-dir=/usr/lib64/mysql/plugin 
--user=mysql 
--log-error=/var/lib/mysql/BSC.TEST.err 
--pid-file=/var/lib/mysql/BSC.TEST.pid
root  3597 3105 0 01:45 pts/1 00:00:00 grep -i mysql
Copy after login


可以看到mysqld_safe的安装位置(上面标蓝色部分):/usr/bin/
接着执行一下命令停止mysql:


/etc/init.d/mysql stop
Copy after login


以安全方式启动mysql:


#/usr/bin/mysqld_safe --skip-grant-tables >/dev/null 2>&1 &
Copy after login


稍等5秒钟,然后执行以下语句:


#/usr/bin/mysql -u root mysql
Copy after login


注意:mysql与mysql_safe目录一样,都是:/usr/bin/下,这个是通过“ps -ef | grep -i mysql”命令得到的。

出现“mysql>”提示符后输入:


mysql> update user set password = Password('root') where User = 'root';
Copy after login


回车后执行(刷新MySQL系统权限相关的表):


mysql> flush privileges;
Copy after login
Copy after login


再执行exit退出:


mysql> exit;
Copy after login


退出后,使用以下命令登陆mysql,试试是否成功:


#mysql -u root -p
Copy after login


按提示输入密码:


root
Copy after login


但是执行查看数据库命令报错:


mysql> show databases;
Copy after login
Copy after login



ERROR 1820 (HY000): You must SET PASSWORD before executing this statement
Copy after login


解决办法:


mysql> SET PASSWORD=PASSWORD('root');
Copy after login



Query OK, 0 rows affected (0.00 sec)
Copy after login
Copy after login



mysql> flush privileges;
Copy after login
Copy after login



Query OK, 0 rows affected (0.00 sec)
Copy after login
Copy after login



mysql> show databases;
Copy after login
Copy after login



+--------------------+
| Database   |
+--------------------+
| information_schema |
| mysql    |
| performance_schema |
| test    |
+--------------------+
4 rows in set (0.00 sec)
Copy after login


PS:如果在使用mysqladmin的话:


# mysqladmin -u root -p password "test123"
Copy after login



Enter password: 【输入原来的密码】
Copy after login

以上就是CentOS下重置MySQL的root密码的教程_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to optimize CentOS HDFS configuration How to optimize CentOS HDFS configuration Apr 14, 2025 pm 07:15 PM

Improve HDFS performance on CentOS: A comprehensive optimization guide to optimize HDFS (Hadoop distributed file system) on CentOS requires comprehensive consideration of hardware, system configuration and network settings. This article provides a series of optimization strategies to help you improve HDFS performance. 1. Hardware upgrade and selection resource expansion: Increase the CPU, memory and storage capacity of the server as much as possible. High-performance hardware: adopts high-performance network cards and switches to improve network throughput. 2. System configuration fine-tuning kernel parameter adjustment: Modify /etc/sysctl.conf file to optimize kernel parameters such as TCP connection number, file handle number and memory management. For example, adjust TCP connection status and buffer size

Centos stops maintenance 2024 Centos stops maintenance 2024 Apr 14, 2025 pm 08:39 PM

CentOS will be shut down in 2024 because its upstream distribution, RHEL 8, has been shut down. This shutdown will affect the CentOS 8 system, preventing it from continuing to receive updates. Users should plan for migration, and recommended options include CentOS Stream, AlmaLinux, and Rocky Linux to keep the system safe and stable.

How to check CentOS HDFS configuration How to check CentOS HDFS configuration Apr 14, 2025 pm 07:21 PM

Complete Guide to Checking HDFS Configuration in CentOS Systems This article will guide you how to effectively check the configuration and running status of HDFS on CentOS systems. The following steps will help you fully understand the setup and operation of HDFS. Verify Hadoop environment variable: First, make sure the Hadoop environment variable is set correctly. In the terminal, execute the following command to verify that Hadoop is installed and configured correctly: hadoopversion Check HDFS configuration file: The core configuration file of HDFS is located in the /etc/hadoop/conf/ directory, where core-site.xml and hdfs-site.xml are crucial. use

Centos shutdown command line Centos shutdown command line Apr 14, 2025 pm 09:12 PM

The CentOS shutdown command is shutdown, and the syntax is shutdown [Options] Time [Information]. Options include: -h Stop the system immediately; -P Turn off the power after shutdown; -r restart; -t Waiting time. Times can be specified as immediate (now), minutes ( minutes), or a specific time (hh:mm). Added information can be displayed in system messages.

Tips for using HDFS file system on CentOS Tips for using HDFS file system on CentOS Apr 14, 2025 pm 07:30 PM

The Installation, Configuration and Optimization Guide for HDFS File System under CentOS System This article will guide you how to install, configure and optimize Hadoop Distributed File System (HDFS) on CentOS System. HDFS installation and configuration Java environment installation: First, make sure that the appropriate Java environment is installed. Edit /etc/profile file, add the following, and replace /usr/lib/java-1.8.0/jdk1.8.0_144 with your actual Java installation path: exportJAVA_HOME=/usr/lib/java-1.8.0/jdk1.8.0_144exportPATH=$J

What steps are required to configure CentOS in HDFS What steps are required to configure CentOS in HDFS Apr 14, 2025 pm 06:42 PM

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

What files do you need to modify in HDFS configuration CentOS? What files do you need to modify in HDFS configuration CentOS? Apr 14, 2025 pm 07:27 PM

When configuring Hadoop Distributed File System (HDFS) on CentOS, the following key configuration files need to be modified: core-site.xml: fs.defaultFS: Specifies the default file system address of HDFS, such as hdfs://localhost:9000. hadoop.tmp.dir: Specifies the storage directory for Hadoop temporary files. hadoop.proxyuser.root.hosts and hadoop.proxyuser.ro

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

See all articles