Home Database Mysql Tutorial mysql服务端与客户端的配置信息都在my.cnf文件中----- '/va

mysql服务端与客户端的配置信息都在my.cnf文件中----- '/va

Jun 07, 2016 pm 03:19 PM
mysql information client Serve Configuration

修改了mysql的数据文件目录为/home/mysql,安装时默认的数据文件目录为/var/lib/mysql,配置文件为/etc/my.cnf,更改/etc/init.d/mysql文件中的datadir=/home/mysql 。 我们要知道/etc/my.cnf配置文件中包含对mysql服务端和客户端的配置。 然后进行如下操作

修改了mysql的数据文件目录为/home/mysql,安装时默认的数据文件目录为/var/lib/mysql,配置文件为/etc/my.cnf,更改/etc/init.d/mysql文件中的datadir=/home/mysql 。

我们要知道/etc/my.cnf配置文件中包含对mysql服务端和客户端的配置。

然后进行如下操作,改名my.cnf为my.cnf-bak,更改/etc/init.d/mysql文件中的datadir为默认路径datadir=,然后启动mysql服务端/usr/bin/mysqld_safe --defaults-extra-file=/etc/my.cnf-bak,成功启动mysql服务端,查看mysqld、mysql进程信息ps -ef| grep -i mysql*,显示mysql的datadir为/home/mysql,sock为/home/mysql/mysql.sock,其他信息也显示正常,然后尝试登录mysql数据库,这时报错提示说/var/lib/mysql/mysql.sock不存在。为什么呢?是因为my.cnf配置文件不存在,mysql客户端没有找到配置文件,就会去读取安装mysql时rpm包编译时指定的默认路径,而/var/lib/mysql/mysql.sock不存在,所以报错。就是说这时mysql客户端从默认参数中读取的信息 与修改后的datadir=/home/mysql的信息不一致,所以无法登录mysql服务器


1)/etc/init.d/mysql里面的datadir=/home/mysql,在mysql启动文件里面设置了数据文件目录

2)查看/etc/、basedir、datadir、~./my.cnf下面是否存在my.cnf配置文件
3)更改/etc/my.cnf为/etc/my.cnf-bak,mysql启动时会读取编译时设置的默认参数
4)此时可以正常启动mysql服务,因为在/etc/init.d/mysql中显示指定了数据文件的目录(似乎与第三条有点矛盾?其实3指的默认参数就是这里)
  # service mysql start
  # mysqladmin -uroot -p variables |grep -i basedir
  # mysqladmin -uroot -p variables |grep -i datadir
5)现在修改/etc/init.d/mysql文件中的datadir,注释掉显示指定的datadir=/home/mysql
6)尝试关闭mysql数据库
  #service mysql stop
  #MySQL server PID file could not be found!                  [FAILED]
  这是因为修改了mysql的datadir=/home/mysql,这时mysql会根据mysql的rpm包里面编译的默认路径/var/lib/mysql文件夹来读取PID文件,而/var/lib/mysql下面没有PID文件,所以提示。
7)此时可以通过mysqladmin -uroot -p shutdown 来正常关闭mysql进程。
8)因为/etc/my.cnf、basedir、datadir、~/.my.cnf都不存在,无法启动mysql。
  可以通过/usr/bin/mysqld_safe --help查看mysqld_safe的参数,然后使用
  # /usr/bin/mysqld_safe --defaults-extra-file=/etc/my.cnf-bak
  指定mysql启动脚本,并启动mysqld进程。 这时mysqld_safe进程会占用终端(在当前终端,无法进行其他操作),使用Ctrl+Z停止mysqld进程(睡眠进程),再使用bg(fg将后台作业放到前台)把mysqld_safe进程设置为后台进程(会从睡眠状态恢复为运行状态,使用)。


###########
9)尝试连接mysql数据库,查看mysql的信息
  # mysqladmin -uroot -p variables | grep -i datadir
  mysqladmin: connect to server at 'localhost' failed 
  error: 'Can't connect to local MySQL server through socke'/var/lib/mysql/mysql.sock' (2)'
  Check that mysqld is running and that the socket: '/var/lib/mysql/mysql.sock' exists!
  提示无法通过/var/lib/mysql/mysql.sock连接mysql服务器
  尝试连接mysql数据库,
  #mysql -uroot -p
  Enter password: 
  ERROR 2002 (HY000): Can't connect to local MySQL server through socket  '/var/lib/mysql/mysql.sock' (2)
###########  
  查看mysql进程信息:
   [root@localhost ~]# ps -ef|grep -i mysql*
   root     18835  5614  0 04:10 pts/1    00:00:00 /bin/sh /usr/bin/mysqld_safe --defaults-extra-file=/etc/my.cnf-bak
   mysql    19133 18835  0 04:10 pts/1    00:00:00 /usr/sbin/mysqld --defaults-extra-file=/etc/my.cnf-bak --basedir=/usr --datadir=/home/mysql --plugin-dir=/usr/lib/mysql/plugin --user=mysql --log-error=/home/mysql/localhost.localdomain.err --pid-file=/home/mysql/localhost.localdomain.pid --socket=/home/mysql/mysql.sock --port=3306
   这时会发现,mysql的进程信息显示的socket路径为修改过的my.cnf-bak中指定的路径/home/mysql/mysql.sock,而连接mysql数据库时,却说无法通过默认的socket路径连接/var/lib/mysql/mysql.sock连接。
############
这是由于/etc/my.cnf、basedir/my.cnf、datadir/my.cnf、~/.my.cnf文件都不存在,而在启动mysql服务的时候,使用/usr/bin/mysqld_safe --defaults-extra-file=/etc/my.cnf-bad 指定了启动时mysql_server读取的配置文件,这样mysql服务器进程成功启动,而在mysql_client客户端使用mysqladmin客户端工具、或mysql通过客户端尝试登陆mysql服务器 时,mysql客户端会去寻找my.cnf配置文件(my.cnf中包含了mysql服务端[mysqld]和客户端[client]的设置),因为没有找到my.cnf配置文件,无法读取用户的设置,mysql客户端会使用mysql安装时设置的默认目录,而默认数据文件目录/var/lib/mysql下没有mysql.sock,所以会报错说找不到/var/lib/mysql/mysql.sock。


my.cnf中的配置信息分类:

mysqld_safe / mysqld / mysqld_multi 属于服务器端配置


mysqldump / mysql / client / mysqladmin之类的属于客户端配置

[client]用户告诉客户端工具mysql连接数据库服务器使用的port和socket文件的路径

[mysql]用于设置客户端工具的显示信息,如在my.cnf中设置

[mysql]

#prompt="(\\u:hostname:\D)[\\d]> "  

这样使用mysql -uroot -p连接到mysql服务器时,就会显示:

(root:hostname:Thu Feb  9 16:32:26 2012)[(none)]>

   
   
   
   
   

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)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

MySQL vs. Other Programming Languages: A Comparison MySQL vs. Other Programming Languages: A Comparison Apr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL for Beginners: Getting Started with Database Management MySQL for Beginners: Getting Started with Database Management Apr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Solve MySQL mode problem: The experience of using the TheliaMySQLModesChecker module Apr 18, 2025 am 08:42 AM

When developing an e-commerce website using Thelia, I encountered a tricky problem: MySQL mode is not set properly, causing some features to not function properly. After some exploration, I found a module called TheliaMySQLModesChecker, which is able to automatically fix the MySQL pattern required by Thelia, completely solving my troubles.

See all articles