Failed to find valid data directory. MySQL generic binary installion - mysql

Im going to install mysql to linux server. But I dont have root access to that server. So I created two folders called mysql and mysqldata. mysql folder holds binary files. mysqldata folder holds data and the logs.
my.cnf
[mysqld]
user = mysql
port = 3306
bind-address = localhost
basedir = /home/nwn/mysql/mysql-8.0
socket = /home/nwn/mysqldata/instA/socket/mysql.sock
datadir = /home/nwn/mysqldata/instA/data
tmpdir = /home/nwn/mysqldata/instA/tmp
secure_file_priv = /home/nwn/mysqldata/instA/mysql-files
max_connections = 150
# Logging
log-bin = /home/nwn/mysqldata/instA/logs/instA-binlog
log-error = /home/nwn/mysqldata/instA/logs/instA-errorlog.err
slow_query_log = 1
slow_query_log_file = /home/nwn/mysqldata/instA/logs/instA-slowquery.log
long_query_time = 0.5
# InnoDB
innodb_data_home_dir = /home/nwn/mysqldata/instA/innodb/data
innodb_data_file_path = ibdata1:50M;ibdata2:12M:autoextend:max:500M
innodb_log_group_home_dir = /home/nwn/mysqldata/instA/innodb/log
innodb_buffer_pool_size = 32M
# MyISAM
key_buffer_size = 16M
server_id = 1
I did all the other configurations.
when I run following command
mysql-8.0]$ bin/mysqld --defaults-file=~/mysqldata/instA/my.cnf --initialize-insercure
I have following logs in the error_log
cat ~/mysqldata/instA/logs/instA-errorlog.err
2018-10-09T10:39:51.127424Z 0 [Warning] [MY-010139] [Server] Changed limits: max_open_files: 1024 (requested 8160)
2018-10-09T10:39:51.127523Z 0 [Warning] [MY-010142] [Server] Changed limits: table_open_cache: 432 (requested 4000)
2018-10-09T10:39:51.383986Z 0 [Warning] [MY-010101] [Server] Insecure configuration for --secure-file-priv: Location is accessible to all OS users. Consider choosing a different directory.
2018-10-09T10:39:51.384043Z 0 [System] [MY-010116] [Server] /home/nwn/mysql/mysql-8.0/bin/mysqld (mysqld 8.0.12) starting as process 32654
2018-10-09T10:39:51.386625Z 0 [Warning] [MY-010122] [Server] One can only use the --user switch if running as root
2018-10-09T10:39:51.394675Z 1 [ERROR] [MY-011011] [Server] Failed to find valid data directory.
2018-10-09T10:39:51.394817Z 0 [ERROR] [MY-010020] [Server] Data Dictionary initialization failed.
2018-10-09T10:39:51.394831Z 0 [ERROR] [MY-010119] [Server] Aborting
2018-10-09T10:39:51.395363Z 0 [System] [MY-010910] [Server] /home/nwn/mysql/mysql-8.0/bin/mysqld: Shutdown complete (mysqld 8.0.12) MySQL Community Server - GPL.

Even though the post is very old, but posting my solution as it took me around 2 hours to figure it out and this can help someone save that time.
You can try doing :
shell> mysqld --initialize
This shall initialse the data directory in the path where you have installed MySql server. Generally placed at C:\Program Files\MySQL\MySQL Server 8.0.
Hope it helps someone.

Please follow these steps to reset your MySQL Password on Windows:
Stop MySQL Service # services.msc
Create file change_mysql_pwd.txt with following content (replase YourNewPassword with desired new password you want to have for root user:
ALTER USER 'root'#'localhost' IDENTIFIED BY 'YourNewPassword';
Save the file under C:\ change_mysql_pwd.txt
Run CMD.exe as Admin (Start->Cmd->Right Click->Run as Administrator)
Type in cmd: cd "C:\Program Files\MySQL\MySQL Server 8.0\bin"
Create “Data” Folder under "C:\Program Files\MySQL\MySQL Server 8.0\ (if already exists delete its contents!)
Type in cmd: mysqld --install
Type in cmd: mysqld --initialize
Type in cmd: mysqld --init-file=C:\\change_mysql_pwd.txt
Login with root user account and the password set above.
Delete C:\ change_mysql_pwd.txt file
Enjoy...

(context: new setup, mysql 8 on windows server 2016)
What really helped me to get rid of "Failed to find valid data directory" was:
mysqld --initialize --console
The --console part tells you straight away on the command line what is wrong and what to do. Because I had a clean install, i lacked the "data" directory. However if you have settings in your my.ini that are not supported in the newly installed version, a "data" directory will be generated nevertheless with the command listed above. If you have more then one faulty setting in my.ini, you'll need to delete the newly created "data" directory first.
CAUTION: check first if you have databases in the data directory, else you'll loose all your databases!! So only use this on fresh installs.
In steps:
run mysqld --initialize --console from a cmd as administrator
fix the my.ini faulty line, delete the data directory (ONLY IF YOU HAVE A NEW INSTALLATION: else you'll loose all your databases!)
rerun mysqld --initialize --console
repeat until all errors are fixed.
Now MySql should start.

As far as I understand from your console output you try to initialize the database while there exist files in your data directory. First you should remove all files (please be careful here, you may loss your data) in the data directory of your MySQL, and then you should run the command below
shell> mysqld --initialize --console
as stated by #Alex Karshin, #Lazycoder_0071 and #Freeze_H. MySQL should initialize the database seamlessly if there isn't another problem.

What Kevin Kopf and Lazycoder_007 indicate in https://stackoverflow.com/a/62003723/7733418 worked for me fine, along with
deleting the files within the "data" folder before initialization (thanks to P D)
running mysqld --initialize in cmd as administrator

Related

MySQL NOFILE limits on WSL2

I am running MySQL on WSL2 for Windows. At WSL2 startup, MySQL does not start and I get the following error:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)
I then run this command to get it going:
sudo service mysql start
And get the following output:
* Starting MySQL database server mysqld
su: warning: cannot change directory to /nonexistent: No such file or directory
[ OK ]
When I check my MySQL error logs, I see this error regarding max_open_files and table_open_cache:
2022-07-28T08:36:34.074107Z 0 [Warning] [MY-010139] [Server] Changed limits: max_open_files: 1024 (requested 8161)
2022-07-28T08:36:34.074111Z 0 [Warning] [MY-010142] [Server] Changed limits: table_open_cache: 431 (requested 4000)
My understanding is I need to edit my /lib/systemd/system/mysql.service file to change this, but when I check that file I see that LimitNOFILE is already set to 10000
# MySQL systemd service file
[Unit]
Description=MySQL Community Server
After=network.target
[Install]
WantedBy=multi-user.target
[Service]
Type=notify
User=mysql
Group=mysql
PIDFile=/run/mysqld/mysqld.pid
PermissionsStartOnly=true
ExecStartPre=/usr/share/mysql/mysql-systemd-start pre
ExecStart=/usr/sbin/mysqld
TimeoutSec=infinity
Restart=on-failure
RuntimeDirectory=mysqld
RuntimeDirectoryMode=755
LimitNOFILE=10000
# Set enviroment variable MYSQLD_PARENT_PID. This is required for restart.
Environment=MYSQLD_PARENT_PID=1
It's almost as if that setting get's ignored at service startup? Does anyone know where the right place is to set it if this isn't?
You need to check information in /etc/security/limits.conf
You can add something like (assuming you run mysqld as mysql user)
mysql hard nofile 10000
mysql soft nofile 8192
and in my.cnf
[mysqld]
open_files_limit = 10000
table_open_cache=4096
then logout, login and restart mysql

Error on realpath() on '/var/lib/mysql-files' (Error 2 - No such file or directory)

I am using Ubuntu to configure Wordpress on WSL. Currently, I am trying to install MySQL as part of the LAMP Stack using the following as per this tutorial:
sudo usermod -d /var/lib/mysql/ mysql
sudo service mysql start
When I run the second command, I see "fail" in red appear (ie. mysql won't start). Here is what the Error Log looks like:
2021-09-05T08:17:59.936284Z mysqld_safe Logging to '/var/log/mysql/error.log'.
2021-09-05T08:17:59.951837Z mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
mysqld: Error on realpath() on '/var/lib/mysql-files' (Error 2 - No such file or directory)
2021-09-05T08:17:59.962000Z 0 [Warning] [MY-010139] [Server] Changed limits: max_open_files: 1024 (requested 8161)
2021-09-05T08:17:59.962006Z 0 [Warning] [MY-010142] [Server] Changed limits: table_open_cache: 431 (requested 4000)
2021-09-05T08:18:00.172509Z 0 [Warning] [MY-011068] [Server] The syntax 'expire-logs-days' is deprecated and will be removed in a future release. Please use binlog_expire_logs_seconds instead.
2021-09-05T08:18:00.172578Z 0 [ERROR] [MY-010095] [Server] Failed to access directory for --secure-file-priv. Please make sure that directory exists and is accessible by MySQL Server. Supplied value : /var/lib/mysql-files
2021-09-05T08:18:00.172626Z 0 [ERROR] [MY-010119] [Server] Aborting
2021-09-05T08:18:00.178880Z mysqld_safe mysqld from pid file /run/mysqld/mysqld.pid ended
Edit- Here is what I get when I run the following commands:
root#DESKTOP-P44T17P:~# stat /var/lib/mysql-files
stat: cannot stat '/var/lib/mysql-files': No such file or directory
root#DESKTOP-P44T17P:~# grep user /etc/mysql/mysql.conf.d/mysqld.cnf
user = mysql
It does not look the mysql-files directory exists.
Ubuntu 20.04.2
it might be your log folder deleted for some reason. make sure there's /var/log/mysql folder. if it's don't just make new one with mysql ownership.
Ensure the directory /var/lib/mysql-files exists and MySQL should start.
You can try:
sudo mkdir /var/lib/mysql-files

Server quit without running PID file when trying to start MySQL server?

So I created my .bash_profile, opened it in a text editor and added:
export PATH=$PATH:/usr/local/mysql/bin
and then I saved it. Next, I tried running
sudo /usr/local/mysql/support-files/mysql.server start
was told to enter my password (so I did), and then terminal said
Starting MySQL
and then it kept printing dots until it said:
ERROR! The server quit without updating PID file (/usr/local/mysql/data/Nicks-MacBook-Pro.local.pid).
The error logs look like:
2021-01-25T20:50:49.707062Z 0 [System] [MY-010116] [Server] /usr/local/mysql/bin/mysqld (mysqld 8.0.23) starting as process 15092
2021-01-25T20:50:49.710232Z 0 [Warning] [MY-010159] [Server] Setting lower_case_table_names=2 because file system for /usr/local/mysql/data/ is case insensitive
2021-01-25T20:50:49.834599Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
2021-01-25T20:50:49.943719Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
2021-01-25T20:50:50.037810Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /tmp/mysqlx.sock
2021-01-25T20:50:50.125494Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
2021-01-25T20:50:50.126128Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
2021-01-25T20:50:50.151069Z 0 [System] [MY-010931] [Server] /usr/local/mysql/bin/mysqld: ready for connections. Version: '8.0.23' socket: '/tmp/mysql.sock' port: 3306 MySQL Community Server - GPL.
~
~
~
~
~
(END)
How do I fix this?
You should try to restart MySQL service after performing each step to check if the error is solved.
But first, take a full backup of MySQL data directory /var/lib/mysql/ before you make any changes to MySQL service.
Connect to your server via SSH as root user and restart MySQL manually. Sometimes restarting the MySQL server may fix this issue. You can use either of the following command to restart MySQL server.
/etc/init.d/mysql restart
or
service mysql restart
You need to check whether the MySQL service is already running. If MySQL service is already running, you will have to kill them and start again. Use the following command to check if there is a MySQL service running already.
ps -aux | grep -i mysql
If MySQL service is already running, you will get list of MySQL processes with PIDs. The PID is the process ID of the MySQL processes. You will have to kill those processes.
kill -9 PID
PID – Process ID of the MySQL process.
Check the ownership of MySQL data directory /var/lib/mysql/. Use the following command to check ownership of MySQL service
ll -aF /var/lib/mysql/
If it’s owner is root, you should change the ownership to MySQL or your user by using the following command.
chown -R mysql /var/lib/mysql/
Take a backup of mysql.sock file and remove it. To achieve this, use the following commands:
cp /var/lib/mysql.sock /var/lib/mysql.sock_bkp
rm -rf /var/lib/mysql.sock
Remove MySQL configuration file /etc/my.cnf or just back it up for now and restart:
mv /etc/my.cnf /etc/my.cnf.bak
Remove the log file named ib_logfile in the MySQL data directory /var/lib/mysql/ or just back it up now and restart the MySQL service. Sometimes MySQL service fails to start when it faces difficulty in updating the log files. The log files will be created automatically once you restart the MySQL service. Use the following command to remove the log files and back it up.
Change the present working directory to MySQL data directory.
cd /var/lib/mysql
Backup and remove the log files by renaming them.
mv ib_logfile0 ib_logfile0.bak
mv ib_logfile1 ib_logfile1.bak
Move the backups to a temporary location.
mv /var/lib/mysql/ib_logfile* /tmp/folder/
Find the log files with suffix “.err” in the MySQL data directory /var/lib/mysql/. These files contain the actual error message. You can use any editor or command like cat, less to read the error message from the log files.
cat /var/lib/mysql/yourservername.err
In many cases, MySQL will run soon after removing this error files.
rm -rf /var/lib/mysql/yourservername.err

Can't create test file lower test start server mysql

I'm following this tutorial here on creating a JDBC programming. I've followed the steps up to the console command to start the server:
// For Windows
cd {path-to-mysql-bin} // Check your MySQL installed directory
mysqld --console
I get the following error message:
mysqld: Can't change dir to 'C:\Program Files\MySQL\MySQL Server 5.7\data\' (Errcode: 2 - No such file or directory)
2017-01-06T10:54:36.968210Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
2017-01-06T10:54:36.968210Z 0 [Note] --secure-file-priv is set to NULL. Operations related to importing and exporting data are disabled
2017-01-06T10:54:36.968210Z 0 [Note] mysqld (mysqld 5.7.17) starting as process 11248 ...
2017-01-06T10:54:36.974226Z 0 [Warning] Can't create test file C:\Program Files\MySQL\MySQL Server 5.7\data\DESKTOP-DEQ2IC2.lower-test
2017-01-06T10:54:36.975229Z 0 [Warning] Can't create test file C:\Program Files\MySQL\MySQL Server 5.7\data\DESKTOP-DEQ2IC2.lower-test
2017-01-06T10:54:36.975229Z 0 [ERROR] failed to set datadir to C:\Program Files\MySQL\MySQL Server 5.7\data\
2017-01-06T10:54:36.976231Z 0 [ERROR] Aborting
2017-01-06T10:54:36.977234Z 0 [Note] Binlog end
2017-01-06T10:54:36.978237Z 0 [Note] mysqld: Shutdown complete
I'm quite new to using MySql in this way but I can see that there is no data folder in the server directory and no my.ini file which I assumes holds the config information on where the data directory is. I've also insntalled MySql using the windows installer which I've read could be why there is no my.ini file and just a template my-default.ini file.
Does anyone know why this is happening?
Create a data folder in C:\Program Files\MySQL\MySQL Server 8.0.
Create a new file with
(ALTER USER 'root'#'localhost' IDENTIFIED BY 'MyNewPass';)
command for password reset (if required)
Make sure that your file is in C: drive. root folder.
Then execute the below command:
C:\> cd "C:\Program Files\MySQL\MySQL Server 8.0\bin"
C:\> mysqld --init-file=C:\\mysql-init.txt
As mentioned above, create a "data" folder, (for me after that it didn't show errors, but it wasn't stills starting).
After that, go to services.msc in windows and start it from there. Also set it to Automatic so it starts automatically whenever you start the windows.
It worked for me.
If you install MySQL using the MySQL installation wizard your executable path is where you installed. It's something like "C:\Program Files\MySQL\MySQL Server 8.0\bin". Your my.init file resides in ProgramData, and path for init file may be like "C:\ProgramData\MySQL\MySQL Server 8.0". If it's can't be viewed in the location, please check whether it's hide.
As in your error description "Can't create test file C:\Program Files\MySQL\MySQL Server 5.7\data...", It seems there is no folder named 'data' in MySQL installed location. Most probably It may reside where the 'my.init' file exists (ProgramData). Else may be the name of the folder not 'data' but 'Data'.
You can copy the Data folder from the location of init-file exist and paste to the executable location. Also, change the name of the folder to data if it's capitalized.
I ran the command bin\mysqld --initialize --console and everything was solved. In some cases the data folder should be created before.
There are different parameters for windows and the rest.
See the following links:
https://dev.mysql.com/doc/refman/8.0/en/data-directory-initialization.html
https://dev.mysql.com/doc/refman/8.0/en/data-directory-initialization.html#data-directory-initialization-password-assignment
https://dev.mysql.com/doc/refman/8.0/en/option-files.html
In my case, I installed MySQL with Windows installer on the D:/ drive and was set NOT to run as Windows service.
I managed to resolve the issue by moving my.ini from C:\ProgramData\MySQL\MySQL Server 8.0 to MySQL installation folder.
This error happens to me.
The solution was edit D:\Program Files\xampp\mysql\data\my.ini and change the directory to your D: path
[mysqld]
datadir=D:\program files\xampp\mysql\data
[client]
I could solve it; I had similar issues.
C:\Program Files\MySQL\MySQL Server 8.0\bin>**mysqld --console**
2021-02-27T23:46:09.919345Z 0 [System] [MY-010116] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe (mysqld 8.0.23) starting as process 7296
2021-02-27T23:46:09.926583Z 0 [Warning] [MY-010091] [Server] Can't create test file C:\Program Files\MySQL\MySQL Server 8.0\data\mysqld_tmp_file_case_insensitive_test.lower-test
2021-02-27T23:46:09.926798Z 0 [Warning] [MY-010091] [Server] Can't create test file C:\Program Files\MySQL\MySQL Server 8.0\data\mysqld_tmp_file_case_insensitive_test.lower-test
2021-02-27T23:46:09.927086Z 0 [ERROR] [MY-013276] [Server] Failed to set datadir to 'C:\Program Files\MySQL\MySQL Server 8.0\data\' (OS errno: 2 - No such file or directory)
2021-02-27T23:46:09.933751Z 0 [ERROR] [MY-010119] [Server] Aborting
2021-02-27T23:46:09.934110Z 0 [System] [MY-010910] [Server] C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe: Shutdown complete (mysqld 8.0.23) MySQL Community Server - GPL.
The solution was that I needed to recreate the my.ini file in C:\ProgramData\MySQL\MySQL Server 8.0 and add to this:
[mysqld]
datadir=C:\ProgramData\MySQL\MySQL Server 8.0\Data
Set encoding to ANSI and saved the file.
Then the service could start:
"C:\Program Files\MySQL\MySQL Server 8.0\bin\mysqld.exe" --defaults-file="C:\ProgramData\MySQL\MySQL Server 8.0\my.ini" MySQL80
Installing with a new MSI installer also didn't solve the issue, it kept hanging until I made the above mentioned adaptations.
In my case, the data folder did not exists, so I created one and things started working.
For xampp-portable-windows-x64-8.0.8-0-VS16
It worked by just putting at root of drive as mentioned in \ xampp \ readme_en.txt
NOTE: Unpack the package to your USB stick or a partition of your choice.
There it must be on the highest level like E:\ or W:\.
Hopes that helps
I stumbled on the same problem as mentioned at the beginning of this thread, and the trick for me was to run my console on Administrator mode (windows).
The error messages related to things like "can't find the folder" etc.., were due to the unability for the program to create, on demand, such folders and files.
Cheers
That happened to me when I try to launch the server without being in admin mode. Try relaunching console in admin mode and then navigate to the sql server 8.0 directory before trying again

Why mysqld_multi can't start mysql?

In my my.cnf, I write:
[mysqld_multi]
mysqld = /usr/local/Cellar/mysql/5.7.11/bin/mysqld_safe
mysqladmin = /usr/local/Cellar/mysql/5.7.11/bin/mysqladmin
user = root
password =
[mysqld1]
pid-file = /usr/local/etc/mysql/mysqld.pid
socket = /usr/local/etc/mysql/mysqld.sock
port = 3306
datadir = /usr/local/var/mysql
log-bin=mysql-bin
server-id=1
[mysqld2]
pid-file = /usr/local/etc/mysql/mysqld1.pid
socket = /usr/local/etc/mysql/mysqld1.sock
port = 3307
datadir = /usr/local/var/mysql1
server-id=2
language=/usr/local/Cellar/mysql/5.7.11/share/mysql/english
user=mysql
Then I execute mysqld_multi start 2, and I get the error info:
Installing new database in /usr/local/var/mysql1
2016-09-19 19:37:16 [WARNING] mysql_install_db is deprecated. Please consider switching to mysqld --initialize
2016-09-19 19:37:16 [ERROR] Can't locate the language directory.
FATAL ERROR: Tried to start mysqld under group [mysqld2],
but no data directory was found or could be created.
data directory used: /usr/local/var/mysql1
I can't start the mysql process, so what's the problem? It seems the directory /usr/local/var/mysql1 has some problems, but the directory exists, it's confused.
It seems like mysqld_multi wasn't fixed to use mysqld --initialize.
You will have to run mysqld --initialize along with all directory related options in my.cnf for that server.
(from comment)
You can also fix the mysqld_multi script following the instructions in Running two instances of mysql.