88

I'm trying to connect to my DB in Ubuntu remotely but I receive error message when trying to mysql -u root -p:

Found option without preceding group in config file: /etc/mysql/my.cnf at line: 1

my.cnf looks like:

[mysqld]
user        = mysql
socket      = /var/run/mysqld/mysqld.sock
port        = 3306
basedir     = /usr
datadir     = /var/lib/mysql
tmpdir      = /tmp
bind-address        =  0.0.0.0
key_buffer      = 16M
max_allowed_packet  = 16M
thread_stack        = 192K
thread_cache_size       = 8
myisam-recover         = BACKUP
query_cache_limit   = 1M
query_cache_size        = 16M
log_error                = /var/log/mysql/error.log
expire_logs_days    = 10
max_binlog_size         = 100M

[client]
port        = 3306
socket      = /var/run/mysqld/mysqld.sock

[mysqld_safe]
socket      = /var/run/mysqld/mysqld.sock
nice        = 0

[mysqldump]
quick
quote-names
max_allowed_packet  = 16M

[mysql]

[isamchk]
key_buffer      = 16M
p.campbell
  • 98,673
  • 67
  • 256
  • 322
Afra
  • 2,502
  • 3
  • 24
  • 27
  • 2
    This looks pretty different from the default my.cnf which is installed with mysql on Ubuntu. Have you tried starting from the default file and gradually adding in your customizations? – Noah Goodrich Nov 05 '11 at 13:00
  • I just removed comments and moved around the code – Afra Nov 05 '11 at 13:46
  • I think the default file looks a little different. – pensebien Nov 22 '17 at 22:03
  • Is this kind of setup version specific? I'm running into the same problem on Ubuntu 16 and mysql 5.7 or something like that... fresh install from apt. – Rick Henderson Jan 30 '18 at 18:44

5 Answers5

132

Missing config header

Just add [mysqld] as first line in the /etc/mysql/my.cnf file.

Example

[mysqld]
default-time-zone = "+08:00"

Afterwards, remember to restart your MySQL Service.

sudo mysqld stop
sudo mysqld start
dsapalo
  • 1,819
  • 2
  • 19
  • 37
vmishra
  • 1,682
  • 2
  • 12
  • 10
65

Charset encoding

Check the charset encoding of the file. Make sure that it is in ASCII.

Use the od command to see if there is a UTF-8 BOM at the beginning, for example.

dsapalo
  • 1,819
  • 2
  • 19
  • 37
aleroot
  • 71,077
  • 30
  • 176
  • 213
  • 56
    Additional note: Lack of [mysqld] causes the error too. – chanux Aug 03 '16 at 13:09
  • 1
    For some reason [mysqld] was missing from the beginning of my my.cnf. Adding it there solved the issue, mysql running as expected now. – webmaniacgr Dec 17 '16 at 17:59
  • This was my issue after editing the file with MySQL workbench on Windows. I solved it by opening it in WordPad and saving it as a "Plain Text Document" – JVE999 May 09 '20 at 17:50
15

What worked for me:

  • Open my.ini with Notepad++
  • Encoding --> convert to ANSI
  • save
MJL
  • 352
  • 3
  • 11
13

it is because of letters or digit infront of [mysqld] just check the leeters or digit anything is not required before [mysqld]

it may be something like

0[mysqld] then this error will occur

payal
  • 411
  • 5
  • 10
0

I had this problem when I installed MySQL 8.0.15 with the community installer. The my.ini file that came with the installer did not work correctly after it had been edited. I did a full manual install by downloading that zip folder. I was able to create my own my.ini file containing only the parameters that I was concerned about and it worked.

  1. download zip file from MySQL website
  2. unpack the folder into C:\program files\MySQL\MySQL8.0
  3. within the MySQL8.0 folder that you unpacked the zip folder into, create a text file and save it as my.ini
  4. include the parameters in that my.ini file that you are concerned about. so something like this(just ensure that there is already a folder created for the datadir or else initialization won't work):

    [mysqld]
    basedire=C:\program files\MySQL\MySQL8.0
    datadir=D:\MySQL\Data
    ....continue with whatever parameters you want to include
    
  5. initialize the data directory by running these two commands in the command prompt:

    cd C:\program files\MySQL\MySQL8.0\bin
    mysqld --default-file=C:\program files\MySQL\MySQL8.0\my.ini --initialize
    
  6. install the MySQL server as a service by running these two commands:

    cd C:\program files\MySQL\MySQL8.0\bin
    mysqld --install --default-file=C:\program files\MySQL\MySQL8.0\my.ini
    
  7. finally, start the server for the first time by running these two commands:

    cd C:\program files\MySQL\MySQL8.0\bin
    mysqld --console
    
Mitchell Leefers
  • 170
  • 1
  • 13