Tag : mysql-2

Install Mysql On macbook with macports

First off we’ll need to install MySQL from Macports. Lets use following command in Terminal to do that:

{code}sudo port install mysql5 mysql5-server{/code}

The installation process can take a while and you’ll see something like that on your Terminal:

Looks like we were lucky and didn’t catch any errors on this one.

Add MySQL as a startup item

If you are like me then you want that MySQL would be always running. For me it’s just easier that way since I’ll use it on daily basis.

{code}sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist{/code}

Done. MySQL now starts every time you start your Mac.

Create initial MySQL databases

MySQL requires that you create the initial databases. On our case the MySQL databases are kept under /opt/local/var/db/mysql5/. If you take a look you’ll notice that this folder is empty.

---> Computing dependencies for mysql5
---> Fetching mysql5
---> Attempting to fetch mysql-5.0.85.tar.gz from http://ftp.plusline.de/mysql/Downloads/MySQL-5.0/
---> Verifying checksum(s) for mysql5
---> Extracting mysql5
---> Applying patches to mysql5
---> Configuring mysql5
---> Building mysql5
---> Staging mysql5 into destroot
---> Installing mysql5 @5.0.85_0+server
---> Activating mysql5 @5.0.85_0+server
---> Cleaning mysql5
---> Computing dependencies for mysql5-server
---> Fetching mysql5-server
---> Verifying checksum(s) for mysql5-server
---> Extracting mysql5-server
---> Configuring mysql5-server
---> Building mysql5-server
---> Staging mysql5-server into destroot
---> Creating launchd control script
###########################################################
# A startup item has been generated that will aid in
# starting mysql5-server with launchd. It is disabled
# by default. Execute the following command to start it,
# and to cause it to launch at startup:
#
# sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist
###########################################################
---> Installing mysql5-server @5.0.85_0
******************************************************
* In order to setup the database, you might want to run
* sudo -u mysql mysql_install_db5
* if this is a new install
******************************************************
---> Activating mysql5-server @5.0.85_0
---> Cleaning mysql5-server

Lets create the default databases by executing this command:

{code}sudo -u mysql mysql_install_db5{/code}

You should see something like that:

 Installing MySQL system tables...
091023 22:11:11 [Warning] Setting lower_case_table_names=2 because file system for /opt/local/var/db/mysql5/ is case insensitive
OK
Filling help tables...
091023 22:11:11 [Warning] Setting lower_case_table_names=2 because file system for /opt/local/var/db/mysql5/ is case insensitive
OK

To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
/opt/local/lib/mysql5/bin/mysqladmin -u root -h MacBook-Pro.local password 'new-password'

Alternatively you can run:
/opt/local/lib/mysql5/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the manual for more instructions.

You can start the MySQL daemon with:
cd /opt/local ; /opt/local/lib/mysql5/bin/mysqld_safe &

You can test the MySQL daemon with mysql-test-run.pl
cd mysql-test ; perl mysql-test-run.pl

Please report any problems with the /opt/local/lib/mysql5/bin/mysqlbug script!

The latest information about MySQL is available on the web at
http://www.mysql.com
Support MySQL by buying support/licenses at http://shop.mysql.com

Start MySQL for the first time

We have the default databases created and it’s time to start MySQL server for the first time.

{code}sudo /opt/local/lib/mysql5/bin/mysqld_safe &{/code}

You’ll get a response like this

Starting mysqld daemon with databases from /opt/local/var/db/mysql5

Once the server has started just press ctrl + c on your keyboard to exit.

Run MySQL from command line easily

I always tend to forget that I need to use mysql5 on command line instead of just typing in mysql.

Fortunately there’s an easy fix for that, just create symlinks for MySQL and MySQLDump

{code}
 sudo ln -s /opt/local/bin/mysql5 /opt/local/bin/mysql
 sudo ln -s /opt/local/bin/mysqldump5 /opt/local/bin/mysqldump
{/code}

Done. Now you can run MySQL with just typing mysql instead of mysql5.

Secure your MySQL server

It’s always a good idea to have password for your system services. To do that on MySQL just type in the following command.

{code}
/opt/local/lib/mysql5/bin/mysqladmin -u root password [your password goes here]
{/code}

That’s it. MySQL should be up and running for you now with no problems.

Tips and tricks

Some tips and tricks that I used to forget myself quite often.

Shut down MySQL server manually

{code}mysqladmin5 -u root -p shutdown{/code}

Remove MySQL from system start up.

{code}sudo launchctl unload -w /Library/LaunchDaemons/com.mysql.mysqld.plist{/code}

If you cant change password

PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:
/opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'
/opt/local/lib/mysql5/bin/mysqladmin -u root -h MacBook-Pro.local password 'new-password'Alternatively you can run:
/opt/local/lib/mysql5/bin/mysql_secure_installation

Sumber

 

Contoh Select Query MySql di PHP

Sekian lama berkutat dengan PHP framework dan Database kemarin ada yang tanya tentang php basic, lha ndelalah malah lupa cara bikin koneksi ke database. akhirnya cari-cari dan ketemu di tempat mas kurniawan.

{code type=php}
$host = “localhost”;
$user = “…”; # masukkan nama user database yang sebenarnya di sini
$pass = “…”; # masukkan password database yang sebenarnya di sini
$db = “test”; # masukkan nama database yang sebenarnya di sini

# pertama, konek. nama variabel $conn di sini singkatan dari
# “connection” (tentu saja anda bebas memakai nama lain)
$conn = mysql_connect($host, $user, $pass) or
die(“Gagal melakukan koneksi ke database!”);

# kedua, pilih DB. argumen kedua $conn umumnya tidak perlu disebutkan
# lagi kecuali jika ada lebih dari satu koneksi yang anda lakukan
mysql_select_db($db, $conn) or
die(“Gagal memilih database $db: “.mysql_error());

# ketiga, lakukan query. nama variabel $res di sini singkatan dari
# “result” (tentu saja anda bebas memakai nama lain). argumen kedua
# $conn umumnya tidak perlu disebutkan lagi kecuali jika ada lebih
# dari satu koneksi yang aktif
$res = mysql_query(“SELECT * FROM orang”, $conn) or
die(“Gagal query: “.mysql_error($conn));

# keempat, ambil baris-baris hasil
$rows = array();
while ($row = mysql_fetch_assoc($res)) $rows[] = $row;

# terakhir, tampilkan (atau proses hasil, dsb)
print_r($rows);
{/code}

atau

{code type=php}
$host = “:/tmp/mysql413.sock”;
$user = “root”; # masukkan nama user database yang sebenarnya di sini
$pass = “”; # masukkan password database yang sebenarnya di sini
$db = “test”; # masukkan nama database yang sebenarnya di sini

# pertama, konek. nama variabel $conn di sini singkatan dari
# “connection” (tentu saja anda bebas memakai nama lain)
$conn = mysql_connect($host, $user, $pass) or
die(“Gagal konek!”);

# kedua, pilih DB. argumen kedua $conn umumnya tidak perlu disebutkan
# lagi kecuali jika ada lebih dari satu koneksi yang anda lakukan
mysql_select_db($db, $conn) or
die(“Gagal memilih database $db: “.mysql_error());

# ketiga, lakukan query. nama variabel $res di sini singkatan dari
# “result” (tentu saja anda bebas memakai nama lain). argumen kedua
# $conn umumnya tidak perlu disebutkan lagi kecuali jika ada lebih
# dari satu koneksi yang aktif
$res = mysql_query(“SELECT * FROM orang”, $conn) or
die(“Gagal query: “.mysql_error($conn));

# keempat, ambil baris-baris hasil
$rows = array();
while ($row = mysql_fetch_assoc($res)) $rows[] = $row;

# terakhir, tampilkan (atau proses hasil, dsb)
echo “<table cellpadding=5 border=1>”;
echo “<tr bgcolor=#cccccc>
<th>ID</th>
<th>Nama depan</th>
<th>Nama belakang</th>
<th>Tanggal lahir</th>
</tr>”;

$i = 0;
foreach ($rows as $row) {
$bgcolor = $i++ % 2 ? “#f0f0f0” : “#ffffff”;
echo “<tr bgcolor=$bgcolor>
<td>$row[id]</td>
<td>$row[nama_depan]</td>
<td>$row[nama_belakang]</td>
<td>$row[tanggal_lahir]</td>
</tr>”;
}

echo “</table>”;
{/code}
sumber : milik kawan saya