boxifeng 2015-02-06
Linux服务器安装Mysql的步骤
1.检查软件是否已经安装
rpm -qa|grep -i mysql
2.强制卸载软件
rpm -e --nodeps MySQL-server-5.0.22-0
3.安装Server端
rpm -ivh MySQL-server-5.0.22-0.i386.rpm
安装MySQL服务端的时候报错,报错信息如下:
[root@iZ23k6iktncZ ~]# rpm -ivh MySQL-server-5.0.22-0.i386.rpm
error: Failed dependencies:
ld-linux.so.2 is needed by MySQL-server-5.0.22-0.i386
libc.so.6 is needed by MySQL-server-5.0.22-0.i386
libc.so.6(GLIBC_2.0) is needed by MySQL-server-5.0.22-0.i386
libc.so.6(GLIBC_2.1) is needed by MySQL-server-5.0.22-0.i386
libc.so.6(GLIBC_2.1.2) is needed by MySQL-server-5.0.22-0.i386
libc.so.6(GLIBC_2.1.3) is needed by MySQL-server-5.0.22-0.i386
libc.so.6(GLIBC_2.2) is needed by MySQL-server-5.0.22-0.i386
libcrypt.so.1 is needed by MySQL-server-5.0.22-0.i386
libm.so.6 is needed by MySQL-server-5.0.22-0.i386
libnsl.so.1 is needed by MySQL-server-5.0.22-0.i386
libnss_dns.so.2 is needed by MySQL-server-5.0.22-0.i386
libnss_files.so.2 is needed by MySQL-server-5.0.22-0.i386
libpthread.so.0 is needed by MySQL-server-5.0.22-0.i386
libpthread.so.0(GLIBC_2.0) is needed by MySQL-server-5.0.22-0.i386
libpthread.so.0(GLIBC_2.1) is needed by MySQL-server-5.0.22-0.i386
libpthread.so.0(GLIBC_2.2) is needed by MySQL-server-5.0.22-0.i386
libresolv.so.2 is needed by MySQL-server-5.0.22-0.i386
解决方法(执行下面一行命令):
yum install -y gcc gcc-c++ gcc4-c++ pam-devel
4.如果你安装的机器此时可以连接到互联网,可以跳过第3步使用yum命令来简化安装过程:
yum install mysql-server mysql-devel mysql
yum将自动从centos的镜像站点查找你指明的软件的最新二进制包,并检查软件包依赖关系,安装软件的同时自动安装其依赖的软件包。
5.启动mysqld服务
service mysqld start
6.设置mysql帐号
以root帐号连接到mysql服务器:
mysql -u root
改变当前数据库为mysql:
use mysql
设置从本地主机登录的root帐号密码:
set password for root@localhost=password('your password');
或:
update user set password=password('your password') where user='root' and host='localhost';
删除匿名帐号:
delete from user where user='' ;
删除密码为空的帐号:
delete from user where password='';
删除允许非localhost主机登录的帐号:
delete from user where host<>'localhost' ;
执行下面的命令使更改生效:
flush privileges ;
执行下面的命令退出mysql命令行:
quit 或:exit
7.查看mysql的安装路径
whereis mysql
8.查看mysql版本号
mysql -V 或者 mysql --version
9.设置开机启动
设置开机自启动的方法有很多,例如使用chkconfig命令,
chkconfig mysqld on 设置mysql开机启动
chmod 755 /etc/rc.d/init.d/mysqld 修改mysqld执行权限
另外也可以在/etc/rc.local文件中加上如下MySQL的启动命令,例如:
/etc/rc.d/init.d/mysql start
设置完成后重启系统,使用netstat –nat命令可看到MySQL的3306端口:
[root@iZ23k6iktncZ init.d]# netstat -nat
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:8009 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:3690 0.0.0.0:* LISTEN
10.查看数据库目前的编码:show variables like '%char%';
+------------------------------------+-----------------------+
| Variable_name | Value |
+------------------------------------+-----------------------+
| character_set_client | latin1 |
| character_set_connection | latin1 |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | latin1 |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+-----------------------------------+----------------------------+
要将其编码修改为utf8,进行一下几步操作:
(1).查找MySQL的cnf文件的位置,执行命令
find / -iname '*.cnf' -print
结果如下:
/etc/my.cnf
/etc/pki/tls/openssl.cnf
/opt/rh/mysql55/root/etc/my.cnf
/opt/rh/mysql55/root/usr/share/doc/mysql55-mysql-server-5.5.36/my-innodb-heavy-4G.cnf
/opt/rh/mysql55/root/usr/share/doc/mysql55-mysql-server-5.5.36/my-huge.cnf
/opt/rh/mysql55/root/usr/share/doc/mysql55-mysql-server-5.5.36/my-large.cnf
/opt/rh/mysql55/root/usr/share/doc/mysql55-mysql-server-5.5.36/my-small.cnf
/opt/rh/mysql55/root/usr/share/doc/mysql55-mysql-server-5.5.36/my-medium.cnf
/opt/rh/mysql55/root/usr/share/mysql/my-innodb-heavy-4G.cnf
/opt/rh/mysql55/root/usr/share/mysql/my-huge.cnf
/opt/rh/mysql55/root/usr/share/mysql/my-large.cnf
/opt/rh/mysql55/root/usr/share/mysql/my-small.cnf
/opt/rh/mysql55/root/usr/share/mysql/my-medium.cnf
/usr/share/doc/mysql-server-5.0.95/my-innodb-heavy-4G.cnf
/usr/share/doc/mysql-server-5.0.95/my-huge.cnf
/usr/share/doc/mysql-server-5.0.95/my-large.cnf
/usr/share/doc/mysql-server-5.0.95/my-small.cnf
/usr/share/doc/mysql-server-5.0.95/my-medium.cnf
/usr/share/mysql/my-innodb-heavy-4G.cnf
/usr/share/mysql/my-huge.cnf
/usr/share/mysql/my-large.cnf
/usr/share/mysql/my-small.cnf
/usr/share/mysql/my-medium.cnf
(2).拷贝 small.cnf、my-medium.cnf、my-huge.cnf、my-innodb-heavy-4G.cnf其中的一个到/etc下,命名为my.cnf
cp /usr/share/mysql/my-medium.cnf /etc/my.cnf,如果出现是否覆盖提示,选择‘y’确定
(3).修改my.cnf
vi /etc/my.cnf
在[client]下添加
default-character-set=utf8
在[mysqld]下添加
default-character-set=utf8
(4).重新启动MySQL
service mysqld restart
(5).再次查看数据库目前的编码:show variables like '%char%';
+----------------------------------+----------------------------+
| Variable_name | Value |
+----------------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | /usr/share/mysql/charsets/ |
+--------------------------+--------------------------------------+
11.查看某个数据库的编码
use mysql
show variables like '%character%';
12.修改某个数据库的编码
alter database mysql character set utf8;
commit;