首先我们应该先用下面的命令安装MySQL:
yum install mysql mysql-server
然后我们需要创建MySQL系统的启动键链接启动MySQL服务器,这样以便于MySQL在系统启动时自动启动
chkconfig --levels 235 mysqld on
/etc/init.d/mysqld start
为MySQL root帐户设置密码:
mysql_secure_installation
会出现下面的一系列提示:
root@server1 ~]# mysql_secure_installation
NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MySQL
SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY!
In order to log into MySQL to secure it, we'll need the current
password for the root user. If you've just installed MySQL, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.
Enter current password for root (enter for none):
OK, successfully used password, moving on...
Setting the root password ensures that nobody can log into the MySQL
root user without the proper authorisation.
Set root password? [Y/n] <-- ENTER
New password: <-- yourrootsqlpassword
Re-enter new password: <-- yourrootsqlpassword
Password updated successfully!
Reloading privilege tables..
... Success!
By default, a MySQL installation has an anonymous user, allowing anyone
to log into MySQL without having to have a user account created for
them. This is intended only for testing, and to make the installation
go a bit smoother. You should remove them before moving into a
production environment.
Remove anonymous users? [Y/n] <-- ENTER
... Success!
Normally, root should only be allowed to connect from 'localhost'. This
ensures that someone cannot guess at the root password from the network.
Disallow root login remotely? [Y/n] <-- ENTER
... Success!
By default, MySQL comes with a database named 'test' that anyone can
access. This is also intended only for testing, and should be removed
before moving into a production environment.
Remove test database and access to it? [Y/n] <-- ENTER
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.
Reload privilege tables now? [Y/n] <-- ENTER
... Success!
Cleaning up...
All done! If you've completed all of the above steps, your MySQL
installation should now be secure.
Thanks for using MySQL!
[root@server1 ~]#
1.添加用户(有两种形式):
A.mysql> grant all on *.* to yushan@"%" identified by "123" ;
mysql>flush privileges; (刷新系统权限表)
(执行完会在mysql.user表插入一条记录,all表示所有权限(包括增删改查等权限), *.* 表示所有数据库,yushan为添加的用户名,123为密码,%为匹配的所有主机,上面的信息都可以指定如grant select,update on db.* to yushan@localhost identified by '123";)
B.直接对mysql.user添加一条记录
mysql> insert into mysql.user(Host,User,Password) values("localhost","yusuhan",password("123"));
mysql>flush privileges;
这样就创建了一个名为:yushan 密码为:123 (密码是经过加密的 ) 的用户,不过这样没有权限因为只添加了三个字段,也可通过grant添加权限:
mysql>grant all on *.* to yushan@localhost identified by '123";
mysql>flush privileges;(刷新系统权限表)
修改字符集:
查看字符集
SHOW VARIABLES LIKE 'character%';
1.查找/etc目录下是否有my.cnf文件;
#ls -l | grep my.cnf (在/etc下查找是否有my.cnf文件存在)
2.如果没有就要从/usr/share/mysql,拷贝一个到/etc 下,在/usr/share/mysql目录下有五个后缀为.cnf的文件,分别是 my-huge.cnf my-innodb-heavy-4G.cnf my-large.cnf my-medium.cnf my-small.cnf ;从中随便拷贝一个到/etc目录下并将其改为my.cnf文件,我选择的是my-medium.cnf :
#cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
3.修改my.cnf文件,在该文件中的三个地方加上 default-character-set=utf8([client] [mysqld] [mysql])
#vi /etc/my.cnf
修改如下:(红色为添加部分)
[client]
#password = your_password
port = 3306
socket = /var/lib/mysql/mysql.sock
default-character-set=utf8 (经过验证好像这一步不用设置也可以达到效果)
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
skip-locking
key_buffer = 16M
max_allowed_packet = 1M
table_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
default-character-set=utf8
init_connect='SET NAMES utf8'
[mysql]
no-auto-rehash
default-character-set=utf8
保存退出;
4.重起MySQL服务器,使其设置的内容生效
#/etc/init.d/mysql restart
5. 重新登入mysql;
#mysql -u root - p
#(输入密码)
mysql> show variables like 'character_set%' ;
+--------------------------+----------------------------+
| Variable_name | Value |
+--------------------------+----------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |(都生成了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/ |
+--------------------------+----------------------------+
大小写敏感解决
MySQL在Linux下数据库名、表名、列名、别名大小写规则:
1、数据库名与表名是严格区分大小写
2、表的别名是严格区分大小写
3、列名与列的别名在所有的情况下均是忽略大小写的
4、变量名也是严格区分大小写的
注意:
A、Linux下MySQL安装完后默认:区分表名的大小写,不区分列名的大小写
B、改变表名的大小写区分规则的方法:用root帐号登录,在/etc/my.cnf 或 /etc/mysql/my.cnf 中的[mysqld]下面添加添加lower_case_table_names=1
# The MySQL server
[mysqld]
port = 3306
socket = /var/lib/mysql/mysql.sock
skip-external-locking
key_buffer_size = 16M
max_allowed_packet = 1M
table_open_cache = 64
sort_buffer_size = 512K
net_buffer_length = 8K
read_buffer_size = 256K
read_rnd_buffer_size = 512K
myisam_sort_buffer_size = 8M
# add here
lower_case_table_names=1
重启MySQL服务,若设置成功,则不再区分表名大小写。
注意:如果在/etc或/etc/mysql找不到my.cnf ,需要从其他地方拷贝过来,因为使用rpm安装mysql时,需要手工拷贝my.cnf。具体操作:
到/usr/share/mysql/下找到*.cnf文件,拷贝其中一个到/etc/并改名为my.cnf)中。命令如下:
[root@test1 mysql]# cp /usr/share/mysql/my-medium.cnf /etc/my.cnf
其中:
my-small.cnf是为了小型数据库而设计的。不应该把这个模型用于含有一些常用项目的数据库。
my-medium.cnf是为中等规模的数据库而设计的。
my-large.cnf是为专用于一个SQL数据库的计算机而设计的。
my-huge.cnf是为企业中的数据库而设计的。
[ Windows ]
MySQL在Windows下数据库名、表名、列名、别名都不区分大小写。
如果想大小写区分则在my.ini 里面的mysqld部分
加入 lower_case_table_names=0
注意:Windows中即使改了这个设置,在查询时还是不会区分大小写。只是在导入导出时会对大小写有区别。
mysql启动和停止操作 :
可以通过 #/etc/init.d/mysql [start | stop | restart](实现启动,停止,重启)
也可以通过 #service mysql [start | stop | restart](实现启动,停止,重启)