I created a database with MySQL Workbench, and now I need to access it. So I've written a php script to access it:
<?
$db = mysql_connect("127.0.0.1:3306","root", "");
if (!$db){
echo "Could not connect to database";
exit();
}
$db_name = "pfc_db";
if (!mysql_select_db($db_name, $db)){
die ("Could not select database");
}
$sql=mysql_query("select * from CAPAS");
while($row=mysql_fetch_assoc($sql)){
$output[]=$row;
if (isset($output)){
echo "yes";
echo $output[0];
}
else{echo "no";}
}
mysql_close();
?>
I have doubts about MAMP and MySQL Community Server and I need a push on the right path.
I've installed MAMP on my mac and switched the ports to the default mysql ones 3306. I've placed the PHP test script in a folder "Api" I've created under htdocs. So to try it out i type on my browser http://127.0.0.1/api/test.php and the result is "Could not connect to databse". Am I doing it right?
And what about Community Server? Is it better than MAMP? Should it be running for MAMP to work? Can I just use Community Server? If so, where should I place the PHP scripts? Which folder? Because when I try to do the same test with Community Server, instead of showing the error message, the browser starts downloading the php script to my downloads folder. Why happens that?
As you can see I have a bit of a mess in my mind with these server stuff and I need some help to figure it out.
You do not need to install MySQL Community Server. MAMP already comes with MySQL.
You need to ouput more specific errors within your code by using the mysql_error() function.
if (!$db){
echo "Could not connect to database" . mysql_error();
exit();
}
$db_name = "pfc_db";
if (!mysql_select_db($db_name, $db)){
die ("Could not select database" . mysql_error());
}
mysql_error will return the error message(s) from MySQL.
Following the asnwer I was giving to user775263, i get this table of users:
+
--------------------+------+----------+
| Host | User | Password |
+--------------------+------+----------+
| localhost | root | |
| SquirrellJoe.local | root | |
| 127.0.0.1 | root | |
| ::1 | root | |
| localhost | | |
| SquirrellJoe.local | | |
+--------------------+------+----------+
As you can see, any of the users has any password set. But then I found out there's a config.inc.php script under bin/phpmyadmin where there are these sentences:
$cfg['Servers'][$i]['auth_type'] = 'config'; // Authentication method (config, http or cookie based)?
$cfg['Servers'][$i]['user'] = 'root'; // MySQL user
$cfg['Servers'][$i]['password'] = 'root'; // MySQL password (only needed
I tried again to connect with $db = mysql_connect("127.0.0.1:3306","root", "root"); and the connexion was successful. Why the users table in mysql command line doesn't reflect this? Is it normal? Should I change it?
And then I run into another problem. It can't select the database 'pfc_db' I created on MySQL Workbench. I get the error message Unknown database 'pfc_db'. However, when I type mysql> show databases; the mentioned db does appear on the table:
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| pfc_db |
| test |
+--------------------+
So what might be the problem here?
Well, I'm gonna answer myself so that if someone runs into the same problem.
The error of password happened because I had two installations of mysql in the same computer, so that I was checking the passwords in one of them and the browser was looking at the other.
Therefore, if you have problems with password access, check that you don't have two mysql installed and also check the file config.inc.php
I also changed TCP/IP by a socket connexion, which improves the speed if you're using your localhost as server.
This worked for me
sudo mkdir /var/mysql
sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock
I ran into this problem and fixed it by using the default TCP/IP port instead of the socket port.
So in my case instead of 3306, I used 8889
$db = mysql_connect("127.0.0.1:8889","root", "");
Related
I am trying to connect from a local server to my remote server with mysql command...
Result after long delay:
[root#local ~] mysqlshow -u test -p*** -h XXX.XXX.XXX.XXX
ERROR 2013 (HY000): Lost connection to MySQL server at 'reading authorization packet', system error: 0
(The connection is done through my second remote server without any error or delay)
Update 1
Retry connecting from local server after add connect_timeout=500 to the remote server's /etc/my.cnf:
[root#local ~] time mysqlshow -u test -p*** -h XXX.XXX.XXX.XXX
+---------------------+
| Databases |
+---------------------+
| foo |
| bar |
+---------------------+
real 3m21.174s <======== (3m!)
user 0m0.004s
sys 0m0.015s
Successful connection but with more than 3 minutes delay to display the result!
The interesting thing is that when the connection is established, other mysql requests (e.g. mysql> SELECT) take effect immediately!
Update 2
Same result after add skip-host-cache and skip-name-resolve to remote server's /etc/my.cnf...
Update 3
Same request through my second remote server: (Everything seems good)
[root#remote2 ~] time mysqlshow -u test -p*** -h XXX.XXX.XXX.XXX
+---------------------+
| Databases |
+---------------------+
| foo |
| bar |
+---------------------+
real 0m0.016s <======== (0.016s)
user 0m0.007s
sys 0m0.002s
Update 4
Check MySQL Connection from local server with telnet: (Everything seems good!)
[root#local ~] time echo X | telnet -e X XXX.XXX.XXX.XXX 3306
Telnet escape character is 'X'.
Trying XXX.XXX.XXX.XXX...
Connected to XXX.XXX.XXX.XXX.
Escape character is 'X'.
telnet> Connection closed.
real 0m0.136s <======== (0.136s)
user 0m0.000s
sys 0m0.005s
Update 5
Try to connect through PHP mysqli from local server:
<?php
$servername = "XXX.XXX.XXX.XXX";
$username = "test";
$password = "***";
$dbname = "dbname";
$conn = new mysqli( $servername, $username, $password, $dbname );
if( $conn->connect_error ) {
die($conn->connect_error);
}
?>
Result:
Warning: mysqli::__construct(): (HY000/2002): Permission denied in /var/www/html/test.php on line 7
Permission denied
(The PHP mysqli connection is done through the second remote server without any error or delay)
Update 6
Retry with disabled SELinux on the local server solves the PHP connection error but the connection still takes more than 3 minutes!
When the connection is established, other requests (e.g. SELECT) take effect immediately.
Update 7
After trying to connect from local server, On the server's phpMyAdmin (or mysqladmin proc command), new connection added with unauthenticated user...
Update 8
Connection try through XAMPP on windows (from different PC and network) to the remote server:
Same result (connecting with unauthenticated user and more than 3 minutes delay to display results) ...
I'm about to go crazy!!
I am running into what I think is a very easy issue to fix, I am just out of possible ideas.
I have a brand new Ubuntu 14.04 x64 server. I just installed MySQL. Not Apache, php or phpMyAdmin, just plain MySQL.
I have run through mysql_secure_installation and created a password for my root user.
I then put my root password in the /etc/mysql/my.cnf file under the [client] section.
I can run mysql -u root and get to the MySQL console just fine.
However, if I run sqitch deploy I get:
Access denied for user 'root'#'localhost' (using password: NO)
Sqitch is pointing to:
[target "database name_v1"]
uri = db:mysql://root#/databasename_v1
[engine "mysql"]
target = db:mysql://root#/databasename_v1
EDIT
It turns out the problem was with Sqitch and my configuration. Sqitch is a Perl application and needed the perl module MySQL Config in order to read the my.cnf file and access the database.
It turns out the problem was with Sqitch and my configuration. Sqitch is a Perl application and needed the perl module MySQL::Config in order to read the my.cnf file and access the database.
If you are using -v to get MySQL version, here's your answer : mysql -V
-v is for verbose output.
See this thread for more details.
"mysql -v" command line error(linux/ubuntu)
There are multiple root users in mysql. They are in the format user#remote.
You can check if the user password is set for all of those by running the query select Host, User, Password from mysql.user where User="root";
The output should show something like:
+--------------+----------+-------------------------------------------+
| Host | User | Password |
+--------------+----------+-------------------------------------------+
| localhost | root | *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| testvm | root | *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
| 127.0.0.1 | root | *XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
If the password is empty, set it via mysqladmin.
From here on, your connection to mysql, whether coming from localhost, or coming from the hostname will be allowed to login.
The problem I have is when I get into the commandline for mysql I enter as ''#'localhost' and have no access to anything useful, I'm trying at the moment to get data back to a php page so I need a valid username and password. Is there a way I can create a user account with my feeble resources? Is there a way I can enter the MySQL commandline as root?
Any help appreciated.
If you user is root without any password (like a default MySQL setup), you should be able to connect using:
mysql --user=root
If you need to specify pwd as password:
mysql --user=root -ppwd
Check MySQL command line guide for other details.
When you install MySQL, it asks you to enter credentials for the root user.
If you had not done something like that, I recommend you to reinstall.
Moreover, I would recommend you to use a good package like PHPMyadmin to simplify your operations with databases.
http://www.phpmyadmin.net/home_page/news.php
You could also try Xampp, which has everything in a package - PHP, Tomcat, Mercury , Filezilla, PHPMyadmin and more if you'd like. You will spend almost 0 time configuring anything.
http://www.apachefriends.org/en/xampp.html
Previously, I was getting Access denied... errors for every command, but I was able to resolve the issue after reading RobbieE's suggestion and this documentation:
First, start mysql from the command line as the root user; this is the solution to your original question:
mysql -u root
Now if you'd like to password-protect root...
To see which accounts exist and check their passwords, execute:
SELECT User, Host, Password FROM mysql.user;
You should see an ASCII table, something like this:
+------+--------------------+----------+
| User | Host | Password |
+------+--------------------+----------+
| root | localhost | |
| root | 127.0.0.1 | |
| | localhost | |
+------+--------------------+----------+
Finally, set the password for each root user a la:
SET PASSWORD FOR 'root'#'localhost' = PASSWORD('plaintext-password');
Re-execute statement 2 to verify the password was set correctly. You now have a useful and protected MySQL user for localhost. Do this again for 127.0.0.1 and any other hosts you may have.
I can't create a database after logging in mysql under my root account. Do I have to make an admin account to do so? Also, for some reason, my StartUp file didn't install (there was an error). I'm not sure if that will affect anything else since mySQL DOES start up when I type "mysql" into my terminal.
Also when I type in
mysql> SELECT Host, User FROM mysql.user;
+---------------------+------+
| Host | User |
+---------------------+------+
| 127.0.0.1 | root |
| ::1 | root |
| myname-mac.att.net | |
| myname-mac.att.net | root |
| localhost | |
| localhost | root |
+---------------------+------+
Which I don't get. I seem to have multiple root users and I don't know what ::1 means.
EDIT: My databases currently look like this.
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
+--------------------+
1 row in set (0.02 sec)
And it doesn't matter what I type in as my database name. I even tried calling it 'apple'.
It might be problem with space.
Follow this
Check .err logs at /var/lib/mysql
if the log says something like
"[ERROR] Can't start server: can't create PID file: No space left on device"
Check /var size by df -hk /var
if used is 100% , then u have to find the files which is geting filled.
find large file in /var by
find /var/ -type f -size +100000k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'
see which file you can delete and then restart the mysql process by
/etc/init.d/mysql restart
let me know if that worked :)
If you're on macosx and if you've system preference pane installed, that should show a message like
the following directory is not owned by _mysql user - "/usr/local/msyql/data"
Once you know that path you can do the following:
sudo chown -R mysql:mysql /usr/local/mysql/data
sudo chown -R mysql:mysql <path>
You have one root user for several domains. Meaning you can connect and run queries on that database FROM the specified domains.
If you want to only show one, give it '%' for the domain and remove all others, although that is not advised. Save the root user for run rights only from localhost, and create limited users for running queries from outside.
As for test database error, it happens on fresh installs. Just reboot the mysql server(stop/start process) or the computer.
Also, make sure you have full rights by doing
GRANT ALL PRIVILEGES ON *.* TO 'root'#'thedomainyourunfrom/localhost/%' WITH GRANT OPTION;
this will give your root user full rights across all databases in the server
osx manual http://dev.mysql.com/doc/mysql-macosx-excerpt/5.0/en/macosx-installation.html
As an additional resource, you can try two other things:
Find out the data folder for your MySQL and 'chown' it so that mysql can write properly to it. For example, if your MySQL's data folder is /usr/local/mysql/data/, you can 'chown' it by typing up the command chown -R mysql:mysql /usr/local/mysql/data/
If you have just installed your MySQL server, try restarting your computer. Sometimes the installer fails on giving proper file access to the program
I hope that helps!
I have installed MySql on windows 7 ... issue is i'm unable to get multiple connection to MySql .
If I connect to MySql through command line and at the same time open an other MySql command line client it goes into wait state, as soon as I disconnect the first one later one gets connected.
Because of above issues I'm unable to run tomcat in debug mode as it tries to get more than one connection to MySql in debug mode.
Previously I was using same version of MySql i.e. 5.1 on vista and it was working fine.
when connected with only one MySql Command line "show processlist" results
| 4 | root | localhost:49487 | NULL | Query | 0 | NULL | show processlist
1 row in set (0.00 sec)
and after connnecting with 2nd command line which hangs "show processlist" on the 1st window results
| 4 | root | localhost:49487 | NULL | Query | 0 | NULL | show processlist
| 5 | root | localhost:49518 | NULL | Sleep | 0 | NULL | NULL
2 rows in set (0.00 sec)
I entered following command through command line.
mysql -u root -h localhost -P 3306 -p
it asked me for password and got connected. Then I opened an other command prompt entered the same command it asked for password and hanged. I went back to the previous command line and closed it and the current one got connected. max_connection is 100 in my.ini file and show processlist reutns same result as above.
What is your 'max_connections' setting (show variables like '%max_connections%') and how many connections are currently 'live' on the server (show processlist)?
I'm guessing it's set very low (1 or 2) and between tomcat and your monitor connections you're exceeding the limit.
Raising it would be done via the mysql.ini/mysql.cnf file, wherever it's kept on Windows.
Are you connecting over the network? or a local file socket? You may be locking on the windows equivalent of mysql.sock - not sure if that behavior changed in Win7. Something like:
mysql -u root -h localhost -p 3306
and make sure that my.ini/my.cnf have networking enabled
After too many re installation of Windows I guess i have identified the root cause ... On every fresh installation MySql use to work fine but after a while I use to get stuck with this issue.
The cause was my voip messenger "Wizton" after installing it MySql work fine but when i restart my machine ... same Connection issue.
But wizton was working perfectly fine with Vista Business .. don't no what happens in Windows 7.