Ubuntu 安装MySQL
1.
增加用户和组:
sudo groupadd mysql
sudo useradd -g mysql mysql
2.
我下载的MySQL版本是5.5.16,
由于新版的mysql采用了cmake编译,因此需要首先安装cmake:
sudo apt-get install cmake
之后再
cmake . \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DINSTALL_DATADIR=/usr/local/mysql/data \
-DEXTRA_CHARSETS=all #安装所有字符集
上面这句话等价与旧版本中的 ./configure --prefix=/usr/local/mysql --with--extra-charsets=all
之后再
make
sudo make install
注意:
如果之前cmake过了,那么重新cmake的时候必须手动删除旧的对象文件和缓存信息:
make clean
rm -f CMakeCache.txt
有关cmake和原来的configure之间的转换请参考:MySQL5.5编译工具configure向cmake过渡指南
3.
完成后:
cd /usr/local/mysql
chown -R mysql .
chgrp -R mysql .
scripts/mysql_install_db --user=mysql #初始化MySQL授权
上面语句执行后的输出内容(很有用!):
Installing MySQL system tables...
OK
Filling help tables...
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:
./bin/mysqladmin -u root password 'new-password'
./bin/mysqladmin -u root -h hutao-ubuntu password 'new-password'
Alternatively you can run:
./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 . ; ./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 ./bin/mysqlbug script!
之后再:
chown -R root . #更改目录的相关属性
4.
将mysql的配置文件拷贝到/etc,并改名:
cp support-files/my-medium.cnf /etc/my.cnf
5.
启动mysql服务:
#将mysql的启动服务添加到系统服务中
sudo cp support-files/mysql.server /etc/init.d/mysql.server
#启动mysql服务:
service mysql.server start
#停止mysql服务:
service mysql.server stop
#重启mysql服务:
service mysql.server restart
注意:如果mysql服务没有启动,那么运行 /usr/local/mysql/bin/mysql 时会提示:
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
6.
开机自动启动mysql服务:
在上面的基础上:
chkconfig --add mysql.server (可能需要安装chkconfig命令)
7.
我真的不知道这步是干什么用的???
启动mysql:
bin/mysqld_safe --user=mysql & #终端会在这里定格很长时间
(初始化并测试你的mysql,其中&为后台执行的意思???)
#在这一步可能出现的问题(注意:MySQL运行的时候最好经常查看日志,这是一个好习惯!!!,日志在/usr/local/mysql/data/下)
在日志中发现:
InnoDB: Check that you do not already have another mysqld process
InnoDB: using the same InnoDB data or log files.
InnoDB: Unable to lock ./ibdata1, error: 11
解决办法:
ps -a
kill -9 XXX #终止所有与mysql有关的进程
注意:
有的时候启动mysql服务的时候会出现如下错误:
The server quit without updating PID file (/usr/local/mysql/data/XXX.pid),解决办法同上!
8.
查看是否启动成功:
netstat -tnl|grep 3306
9.
最后终于可以使用mysql啦:
/bin/mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.16-log Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
10.
为root设置密码:
bin/mysqladmin -u root password 'your password'
11.
用root登录mysql:
bin/mysql -u root -p
提示输入密码。
12.
增加链接
sudo ln -s /usr/local/mysql/bin/mysql /usr/bin
以后就可以直接输入mysql -u root -p 就可以啦!
13.
在终端输入mysql后仍然可以进入mysql,使用的是匿名用户,为了更安全,我们要删除匿名用户,这样可以使得用户在终端输入mysql的时候必须提供用户名和密码才可以登录:
mysql -u root -p 进入mysql;
use mysql;
select * from user; //我们可以看到这里有好几个账户,删除不必要的账户
delete from user where user=""; //删除匿名账户
flush privileges; //更新权限
退出mysql,在终端再次输入mysql的时候就必须提供用户名和密码才可以登录了。
完成!