centos下安装mysql服务器的方法_Mysql_脚本之家

项目需要就在现有的服务器上面重新安装了个mysql服务器,还挺费劲儿呢,因为之前都是在我的笔记本上面试验的,它的系统是Ubuntu的,什么路径啊,启动方式啊。都不一样所以这次还是让我纠结了一把:下面把我安装过程中遇到的问题给传上来,首先rpm格式安装就不多说了,主要是mysql的配置文件在:/etc/my.cnf这里,需要修改:复制代码
代码如下:[mysqld]datadir=/var/lib/mysqlsocket=/var/lib/mysql/mysql.sock#
Default to using old password format for compatibility with mysql 3.x#
clients (those using the mysqlclient10 compatibility
package).old_passwords=1 ←
找到这一行,在这一行的下面添加新的规则,让MySQL的默认编码为UTF-8default-character-set
= utf8 ←

linux安装mysql的glibc包

下载mysql:

选择一个mysql的版本,之后一定要看好,下glibc的。如:mysql-5.0.90-linux-i686-glibc23.tar.gz

本例中下载到了/media目录下,这个不是好习惯…

▲安装mysql:

下面是linux命令

[plain] view plaincopy

  1. :$ sudo groupadd mysql
  2. :$ sudo useradd -g mysql mysql
  3. :$ cd /usr/local
  4. :$ tar zvxf /media/mysql-5.0.90-linux-i686-glibc23.tar.gz
    [plain] view plaincopy

    1. :$ mv mysql-5.0.90-linux-i686-glibc23 mysql [plain] view
      plaincopy

      1. :$ cd mysql
      2. :$ sudo chown -R mysql .
      3. :$ sudo chgrp -R mysql .
      4. :$ scripts/mysql_install_db –user=mysql
        –basedir=/usr/local/mysql
      5. :& cd ..
      6. :$ sudo chown -R root mysql .
      7. :$ cd mysql
      8. :$ sudo chown -R mysql data
      9. :$ bin/mysqld_safe –basedir=/usr/local/mysql –user=mysql
        &

        至此,mysql安装成功。

        因为在运行状态,我没有ctrl-c,只好再开一个ssh窗口…

        ▲为mysql的root用户添加密码

        下面是linux命令

        **[c-sharp] view plaincopy**

        1. cd /usr/local/mysql/bin ./mysql -u root

          进入mysql后:

          [c-sharp] view plaincopy

          1. mysql> GRANT ALL PRIVILEGES ON *.* TO
            [email protected]
            IDENTIFIED BY “chang”;

            其实是设置了root的localhost的密码为chang
            显示执行成功,然后exit退出mysql。

            之后,再次登录mysql,这次要用密码了:

            [c-sharp] view plaincopy

            1. cd /usr/local/mysql/bin./mysql -u root -p

              输入密码chang之后,可以正常登录,如下:

              Welcome to the MySQL monitor. Commands end with
              ; or /g.
              Your MySQL connection id is 1
              Server version: 5.0.90 MySQL Community Server
              (GPL)

              Type ‘help;’ or ‘/h’ for help. Type ‘/c’ to
              clear the current input statement.

              查看一下用户信息:

              [c-sharp] view plaincopy

              1. 1. mysql> select user,host,password from
                mysql.user;

                结果如下:

                +——+———–+———-+
                | user | host | password |
                +——+———–+———-+
                | root | localhost |
                *F05D019BA3BEC01CA9FBD4141E4EA57A28EF3EDF |
                ← (root密码为chang)
                | root | linux | | ← (root密码为空)
                | root | 127.0.0.1 | | ← (root密码为空)
                | | localhost | |
                +——+———–+———-+

                分别更改它们的密码:

                [c-sharp] view plaincopy

                1. mysql> set password for
                [email protected]=password(‘chang’);
                [c-sharp] view plaincopy
                1. mysql> set password for
                [email protected]=password(‘chang’); 
                [c-sharp] view plaincopy
                1. mysql> set password for
                [email protected]=password(‘chang’); 

                再次查看用户信息会发现已经更改过来。

                然后退出mysql。

                ▲把mysql做成服务

                [c-sharp] view plaincopy

                1. sudo cp
                /usr/local/mysql/support-files/mysql.server
                /etc/init.d/mysql

                启动mysql服务

                [c-sharp] view
                plaincopy

                1. sudo /etc/init.d/mysql
                start

                这时候就可以重启机器试试了
                重启后再登陆mysql,发现可以登陆。服务制作成功!

                ▲配置mysql

                [c-sharp] view
                plaincopy

                1. vi /etc/my.cnf

                (注释:如果没有自动生成my.cnf文件,那么:安装完的mysql包下有个support-files文件夹,其中有my-huge.cnf等,将my-huge
                copy一份,改名为my.cnf,将其适当地修改(当然是根据你的数据库配置)然后copy至/etc/my.cnf)

                打开my.cnf后

                找到[client]
                添加:
                default-character-set
                = utf8 #
                默认字符集为utf8

                找到[mysqld]
                添加:
                default-character-set
                = utf8
                #默认字符集为utf8
                init_connect = ‘SET
                NAMES utf8’
                #设定连接mysql数据库时使用utf8编码,以让mysql数据库为utf8运行

                修改好后,重新启动即可.
                我这里是重启了mysql的服务:

                [c-sharp] view
                plaincopy

                1. sudo
                /etc/init.d/mysql
                restart
                (有一次找不到sock,这样重启两次服务之后居然可以了!!!汗。)

                之后进入mysql,查一下是否更改了字符集:

                [c-sharp]
                view plaincopy

                1. cd
                /usr/local/mysql/bin./mysql
                -u root -p
                [c-sharp]
                view
                plaincopy
                1. mysql>
                show
                variables
                like
                ‘character%’;

                出现下面的画面:

                +————————–+—————————————-+
                |
                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/local/mysql/share/mysql/charsets/
                |
                +————————–+—————————————-+

                好,数据库语言完毕。

                ▲打开mysql的远程访问
                mysql默认是不允许远程访问的。
                用密码登陆mysql,可以正常登陆,但是换台机器用工具连,就报错:
                ERROR
                1130:
                Host
                192.168.1.6
                is not
                allowed
                to
                connect
                to this
                MySQL
                server

                方法:
                改表法。mysql默认是不允许远程访问的,只能在localhost访问。这个时候只要在localhost的那台电脑,登入mysql后,更改
                “mysql”
                数据库里的
                “user”
                表里的
                “host”
                项,从”localhost”改成”%”

                mysql -u
                root
                -p
                Enter
                password:
                chang
                mysql>use
                mysql;
                mysql>update
                user set
                host =
                ‘%’
                where
                user =
                ‘root’;
                //可能会报错
                mysql>flush
                privileges;
                mysql>select
                host,user
                from
                user
                where
                user=’root’;

                执行完上面的,就可以远程连接了!

                注释:

                update
                user set
                host =
                ‘%’
                where
                user =
                ‘root’;
                //这个命令执行错误时,可能会报错:
                ERROR
                1062
                (23000):
                Duplicate
                entry
                ‘%-root’
                for key
                1;
                解决方法:
                1,不用管它。呵呵。
                2,改成这样执行
                update
                user set
                host=’%’
                where
                user=’root’
                and
                host=’localhost’;
                也就是把localhost改成了所有主机。

                —————————————————
                之后运行app程序,报错:
                ImportError:
                libmysqlclient_r.so.15:
                cannot
                open
                shared
                object
                file: No
                such
                file or
                directory

                解决办法是把/usr/local/mysql/lib下的
                libmysqlclient_r.so.15
                拷贝到/usr/lib解决。

                至此,mysql安装配置完毕!

                =================
                我是华丽的分割线
                ========================

                —————————————————-
                ■附注:
                附注1:
                重启和关闭mysql服务
                重启mysql服务
                :$ sudo
                /etc/init.d/mysql
                restart
                关闭mysql服务
                :$ sudo
                /etc/init.d/mysql
                stop
                —————————————————-
                附注2:
                非服务状态下,启动和停止mysql
                启动mysql
                代码:
                :& cd
                /usr/local/mysql
                :&
                bin/mysqld_safe
                –basedir=/usr/local/mysql
                –user=mysql
                &

                停止mysql
                代码:
                :& cd
                /usr/local/mysql
                :$
                bin/mysqladmin
                -uroot
                -ppassw0rd
                shutdown

                —————————————————-
                附注3:
                mysql命令行中文显示?号
                mysql>
                set
                names
                utf8;

                —————————————————
                附注4:
                mysql的数据库存放路径
                /var/lib/mysql

                —————————————————
                附注5:
                从mysql中导出和导入数据
                mysqldump
                数据库名 >
                文件名
                #导出数据库
                mysqladmin
                create
                数据库名
                #建立数据库
                mysql
                数据库名
                <
                文件名
                #导入数据库

                —————————————————
                附注6:
                修改mysql的root口令
                sudo
                mysqladmin
                -u root
                -p
                password
                ‘你的新密码’

                或者:括号里是新密码
                use
                mysql;
                update
                user set
                Password=password(‘chang’)
                where
                User=’root’;
                flush
                privileges;

                —————————————————
                附注7:
                忘了mysql的root口令怎么办
                sudo
                /etc/init.d/mysql
                stop
                sudo
                mysqld_safe
                –skip-grant-tables
                &
                sudo
                mysqladmin
                -u user
                password
                ‘newpassword
                sudo
                mysqladmin
                flush-privileges

                —————————————————
                附注8:
                输入要登录的mysql主机
                ./mysql
                -u root
                -h
                127.0.0.1
                -p

                                                            执行安全设置

                                                                #bin/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):<---输入现在的root密码,因为我们还没设置,直接回车
                                                                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] Y   <---是否设定root密码,当然设置了,输入Y回车
                                                                New password: <---输入root密码,并回车,输入的过程中不会有任何显示
                                                                Re-enter new password: <---再次输入root密码,并回车,输入的过程中不会有任何显示
                                                                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] Y <---是否删除匿名用户,删除,输入Y回车
                                                                 ... 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] Y <---是否删禁止root用户远程登录,当然禁止,输入Y回车
                                                                 ... 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] <---是否删除测试数据库test,删除,输入Y回车
                                                                 - 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] Y <---刷新权限,输入Y回车
                                                                 ... 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!




                                                            http://www.bkjia.com/Mysql/985802.htmlwww.bkjia.comtruehttp://www.bkjia.com/Mysql/985802.htmlTechArticlelinux安装mysql的glibc包
                                                            下载mysql:
                                                            http://downloads.mysql.com/archives.php
                                                            选择一个mysql的版本,之后一定要看好,下glibc的。如:mysql-5.0.90-linux-i686-...
You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图